1
1
import { Injectable , OnInit , OnDestroy , Inject } from '@angular/core' ;
2
- import { OidcSecurityService , OpenIdConfiguration , AuthWellKnownEndpoints } from 'angular-auth-oidc-client' ;
2
+ import { OidcSecurityService , OpenIdConfiguration , AuthWellKnownEndpoints , AuthorizationResult , AuthorizationState } from 'angular-auth-oidc-client' ;
3
3
import { Observable , Subscription } from 'rxjs' ;
4
4
import { HttpHeaders , HttpClient } from '@angular/common/http' ;
5
+ import { Router } from '@angular/router' ;
5
6
6
7
@Injectable ( )
7
8
export class AuthService implements OnDestroy {
@@ -11,6 +12,7 @@ export class AuthService implements OnDestroy {
11
12
constructor (
12
13
private oidcSecurityService : OidcSecurityService ,
13
14
private http : HttpClient ,
15
+ private router : Router ,
14
16
@Inject ( 'BASE_URL' ) private originUrl : string ,
15
17
@Inject ( 'AUTH_URL' ) private authUrl : string ,
16
18
) {
@@ -28,12 +30,16 @@ export class AuthService implements OnDestroy {
28
30
const openIdImplicitFlowConfiguration : OpenIdConfiguration = {
29
31
stsServer : this . authUrl ,
30
32
redirect_url : this . originUrl + 'callback' ,
31
- client_id : 'spaClient ' ,
32
- response_type : 'id_token token ' ,
33
+ client_id : 'spaCodeClient ' ,
34
+ response_type : 'code ' ,
33
35
scope : 'openid profile resourceApi' ,
34
36
post_logout_redirect_uri : this . originUrl ,
35
37
forbidden_route : '/forbidden' ,
36
38
unauthorized_route : '/unauthorized' ,
39
+ start_checksession : true ,
40
+ silent_renew : true ,
41
+ silent_renew_url : this . originUrl + '/silent-renew.html' ,
42
+ history_cleanup_off : true ,
37
43
auto_userinfo : true ,
38
44
log_console_warning_active : true ,
39
45
log_console_debug_active : true ,
@@ -64,22 +70,32 @@ export class AuthService implements OnDestroy {
64
70
this . isAuthorizedSubscription = this . oidcSecurityService . getIsAuthorized ( ) . subscribe ( ( isAuthorized => {
65
71
this . isAuthorized = isAuthorized ;
66
72
} ) ) ;
73
+
74
+ this . oidcSecurityService . onAuthorizationResult . subscribe (
75
+ ( authorizationResult : AuthorizationResult ) => {
76
+ this . onAuthorizationResultComplete ( authorizationResult ) ;
77
+ } ) ;
67
78
}
68
79
80
+ private onAuthorizationResultComplete ( authorizationResult : AuthorizationResult ) {
81
+
82
+ console . log ( 'Auth result received AuthorizationState:'
83
+ + authorizationResult . authorizationState
84
+ + ' validationResult:' + authorizationResult . validationResult ) ;
85
+
86
+ if ( authorizationResult . authorizationState === AuthorizationState . unauthorized ) {
87
+ if ( window . parent ) {
88
+ // sent from the child iframe, for example the silent renew
89
+ this . router . navigate ( [ '/unauthorized' ] ) ;
90
+ } else {
91
+ window . location . href = '/unauthorized' ;
92
+ }
93
+ }
94
+ }
69
95
70
96
private doCallbackLogicIfRequired ( ) {
71
97
72
98
this . oidcSecurityService . authorizedCallbackWithCode ( window . location . toString ( ) ) ;
73
- // if (window.location.hash) {
74
- // window.location.hash = decodeURIComponent(window.location.hash);
75
- // // authorizedCallback returns wrong result when hash is URI encoded
76
- // } else {
77
-
78
- // this.oidcSecurityService.authorize();
79
- // }
80
- // if (typeof location !== "undefined") {
81
- // this.oidcSecurityService.authorizedCallback();
82
- // }
83
99
}
84
100
85
101
getIsAuthorized ( ) : Observable < boolean > {
0 commit comments