@@ -2,12 +2,12 @@ import { Injectable, Inject, Optional } from '@angular/core';
22import { OAuthService } from '../oauth-service' ;
33import { OAuthStorage } from '../types' ;
44import {
5- HttpEvent ,
6- HttpHandler ,
7- HttpInterceptor ,
8- HttpRequest ,
9- HttpResponse ,
10- HttpErrorResponse
5+ HttpEvent ,
6+ HttpHandler ,
7+ HttpInterceptor ,
8+ HttpRequest ,
9+ HttpResponse ,
10+ HttpErrorResponse
1111} from '@angular/common/http' ;
1212import { Observable } from 'rxjs' ;
1313import { catchError } from 'rxjs/operators' ;
@@ -16,51 +16,46 @@ import { OAuthModuleConfig } from '../oauth-module.config';
1616
1717@Injectable ( )
1818export class DefaultOAuthInterceptor implements HttpInterceptor {
19- constructor (
20- private authStorage : OAuthStorage ,
21- private errorHandler : OAuthResourceServerErrorHandler ,
22- @Optional ( ) private moduleConfig : OAuthModuleConfig
23- ) { }
19+ constructor (
20+ private authStorage : OAuthStorage ,
21+ private errorHandler : OAuthResourceServerErrorHandler ,
22+ @Optional ( ) private moduleConfig : OAuthModuleConfig
23+ ) { }
24+
25+ private checkUrl ( url : string ) : boolean {
26+ const found = this . moduleConfig . resourceServer . allowedUrls . find ( u => url . startsWith ( u ) ) ;
27+ return ! ! found ;
28+ }
2429
25- private checkUrl ( url : string ) : boolean {
26- const found = this . moduleConfig . resourceServer . allowedUrls . find ( u =>
27- url . startsWith ( u )
28- ) ;
29- return ! ! found ;
30- }
30+ public intercept (
31+ req : HttpRequest < any > ,
32+ next : HttpHandler
33+ ) : Observable < HttpEvent < any > > {
34+ const url = req . url . toLowerCase ( ) ;
3135
32- public intercept (
33- req : HttpRequest < any > ,
34- next : HttpHandler
35- ) : Observable < HttpEvent < any > > {
36- const url = req . url . toLowerCase ( ) ;
36+ if ( ! this . moduleConfig ) {
37+ return next . handle ( req ) ;
38+ }
39+ if ( ! this . moduleConfig . resourceServer ) {
40+ return next . handle ( req ) ;
41+ }
42+ if ( this . moduleConfig . resourceServer . allowedUrls && ! this . checkUrl ( url ) ) {
43+ return next . handle ( req ) ;
44+ }
3745
38- if ( ! this . moduleConfig ) {
39- return next . handle ( req ) ;
40- }
41- if ( ! this . moduleConfig . resourceServer ) {
42- return next . handle ( req ) ;
43- }
44- if ( ! this . moduleConfig . resourceServer . allowedUrls ) {
45- return next . handle ( req ) ;
46- }
47- if ( ! this . checkUrl ( url ) ) {
48- return next . handle ( req ) ;
49- }
46+ const sendAccessToken = this . moduleConfig . resourceServer . sendAccessToken ;
5047
51- const sendAccessToken = this . moduleConfig . resourceServer . sendAccessToken ;
48+ if ( sendAccessToken && this . authStorage . getItem ( 'access_token' ) ) {
49+ const token = this . authStorage . getItem ( 'access_token' ) ;
50+ const header = 'Bearer ' + token ;
5251
53- if ( sendAccessToken && this . authStorage . getItem ( 'access_token' ) ) {
54- const token = this . authStorage . getItem ( 'access_token' ) ;
55- const header = 'Bearer ' + token ;
52+ const headers = req . headers . set ( 'Authorization' , header ) ;
5653
57- const headers = req . headers . set ( 'Authorization' , header ) ;
54+ req = req . clone ( { headers } ) ;
55+ }
5856
59- req = req . clone ( { headers } ) ;
57+ return next
58+ . handle ( req )
59+ . pipe ( catchError ( err => this . errorHandler . handleError ( err ) ) ) ;
6060 }
61-
62- return next
63- . handle ( req )
64- . pipe ( catchError ( err => this . errorHandler . handleError ( err ) ) ) ;
65- }
6661}
0 commit comments