1
- import { Injectable , Inject , Optional } from '@angular/core' ;
1
+ import { Injectable , Optional } from '@angular/core' ;
2
2
import { OAuthService } from '../oauth-service' ;
3
- import { OAuthStorage } from '../types' ;
4
3
import {
5
- HttpEvent ,
6
- HttpHandler ,
7
- HttpInterceptor ,
8
- HttpRequest ,
9
- HttpResponse ,
10
- HttpErrorResponse
4
+ HttpEvent ,
5
+ HttpHandler ,
6
+ HttpInterceptor ,
7
+ HttpRequest ,
11
8
} from '@angular/common/http' ;
12
- import { Observable } from 'rxjs' ;
13
- import { catchError } from 'rxjs/operators' ;
9
+ import { Observable , of , merge } from 'rxjs' ;
10
+ import { catchError , filter , map , take , mergeMap , timeout } from 'rxjs/operators' ;
14
11
import { OAuthResourceServerErrorHandler } from './resource-server-error-handler' ;
15
12
import { OAuthModuleConfig } from '../oauth-module.config' ;
16
13
import { isPlatformBrowser } from '@angular/common' ;
17
14
15
+ const WAIT_FOR_TOKEN_RECEIVED = 1000 ;
16
+
18
17
@Injectable ( )
19
18
export class DefaultOAuthInterceptor implements HttpInterceptor {
19
+
20
20
constructor (
21
21
private authStorage : OAuthStorage ,
22
22
private errorHandler : OAuthResourceServerErrorHandler ,
@@ -35,35 +35,59 @@ export class DefaultOAuthInterceptor implements HttpInterceptor {
35
35
return true ;
36
36
}
37
37
38
- public intercept (
39
- req : HttpRequest < any > ,
40
- next : HttpHandler
41
- ) : Observable < HttpEvent < any > > {
42
- const url = req . url . toLowerCase ( ) ;
43
38
44
- if ( ! this . moduleConfig ) {
45
- return next . handle ( req ) ;
46
- }
47
- if ( ! this . moduleConfig . resourceServer ) {
48
- return next . handle ( req ) ;
49
- }
50
- if ( ! this . checkUrl ( url ) ) {
51
- return next . handle ( req ) ;
52
- }
39
+ private checkUrl ( url : string ) : boolean {
40
+ const found = this . moduleConfig . resourceServer . allowedUrls . find ( u => url . startsWith ( u ) ) ;
41
+ return ! ! found ;
42
+ }
43
+
44
+ public intercept (
45
+ req : HttpRequest < any > ,
46
+ next : HttpHandler
47
+ ) : Observable < HttpEvent < any > > {
48
+ const url = req . url . toLowerCase ( ) ;
53
49
54
- const sendAccessToken = this . moduleConfig . resourceServer . sendAccessToken ;
55
50
56
- if ( sendAccessToken && this . authStorage . getItem ( 'access_token' ) ) {
57
- const token = this . authStorage . getItem ( 'access_token' ) ;
58
- const header = 'Bearer ' + token ;
51
+ if ( ! this . moduleConfig ) {
52
+ return next . handle ( req ) ;
53
+ }
54
+ if ( ! this . moduleConfig . resourceServer ) {
55
+ return next . handle ( req ) ;
56
+ }
57
+ if ( this . moduleConfig . resourceServer . allowedUrls && ! this . checkUrl ( url ) ) {
58
+ return next . handle ( req ) ;
59
+ }
59
60
60
- const headers = req . headers . set ( 'Authorization' , header ) ;
61
+ const sendAccessToken = this . moduleConfig . resourceServer . sendAccessToken ;
61
62
62
- req = req . clone ( { headers } ) ;
63
+ if ( ! sendAccessToken ) {
64
+ return next
65
+ . handle ( req )
66
+ . pipe ( catchError ( err => this . errorHandler . handleError ( err ) ) ) ;
67
+ }
68
+
69
+ return merge (
70
+ of ( this . oAuthService . getAccessToken ( ) ) . pipe (
71
+ filter ( token => token ? true : false ) ,
72
+ ) ,
73
+ this . oAuthService . events . pipe (
74
+ filter ( e => e . type === 'token_received' ) ,
75
+ timeout ( WAIT_FOR_TOKEN_RECEIVED ) ,
76
+ map ( _ => this . oAuthService . getAccessToken ( ) ) ,
77
+ ) ,
78
+ ) . pipe (
79
+ take ( 1 ) ,
80
+ mergeMap ( token => {
81
+ if ( token ) {
82
+ const header = 'Bearer ' + token ;
83
+ const headers = req . headers . set ( 'Authorization' , header ) ;
84
+ req = req . clone ( { headers } ) ;
63
85
}
64
86
65
87
return next
66
- . handle ( req )
67
- . pipe ( catchError ( err => this . errorHandler . handleError ( err ) ) ) ;
68
- }
88
+ . handle ( req )
89
+ . pipe ( catchError ( err => this . errorHandler . handleError ( err ) ) ) ;
90
+ } ) ,
91
+ ) ;
92
+ }
69
93
}
0 commit comments