1- import { Injectable , Inject , Optional } from '@angular/core' ;
1+ import { Injectable , Optional } from '@angular/core' ;
22import { OAuthService } from '../oauth-service' ;
3- import { OAuthStorage } from '../types' ;
43import {
5- HttpEvent ,
6- HttpHandler ,
7- HttpInterceptor ,
8- HttpRequest ,
9- HttpResponse ,
10- HttpErrorResponse
4+ HttpEvent ,
5+ HttpHandler ,
6+ HttpInterceptor ,
7+ HttpRequest ,
118} 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' ;
1411import { OAuthResourceServerErrorHandler } from './resource-server-error-handler' ;
1512import { OAuthModuleConfig } from '../oauth-module.config' ;
1613import { isPlatformBrowser } from '@angular/common' ;
1714
15+ const WAIT_FOR_TOKEN_RECEIVED = 1000 ;
16+
1817@Injectable ( )
1918export class DefaultOAuthInterceptor implements HttpInterceptor {
19+
2020 constructor (
2121 private authStorage : OAuthStorage ,
2222 private errorHandler : OAuthResourceServerErrorHandler ,
@@ -35,35 +35,59 @@ export class DefaultOAuthInterceptor implements HttpInterceptor {
3535 return true ;
3636 }
3737
38- public intercept (
39- req : HttpRequest < any > ,
40- next : HttpHandler
41- ) : Observable < HttpEvent < any > > {
42- const url = req . url . toLowerCase ( ) ;
4338
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 ( ) ;
5349
54- const sendAccessToken = this . moduleConfig . resourceServer . sendAccessToken ;
5550
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+ }
5960
60- const headers = req . headers . set ( 'Authorization' , header ) ;
61+ const sendAccessToken = this . moduleConfig . resourceServer . sendAccessToken ;
6162
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 } ) ;
6385 }
6486
6587 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+ }
6993}
0 commit comments