@@ -2,12 +2,12 @@ import { Injectable, Inject, Optional } from '@angular/core';
2
2
import { OAuthService } from '../oauth-service' ;
3
3
import { OAuthStorage } from '../types' ;
4
4
import {
5
- HttpEvent ,
6
- HttpHandler ,
7
- HttpInterceptor ,
8
- HttpRequest ,
9
- HttpResponse ,
10
- HttpErrorResponse
5
+ HttpEvent ,
6
+ HttpHandler ,
7
+ HttpInterceptor ,
8
+ HttpRequest ,
9
+ HttpResponse ,
10
+ HttpErrorResponse
11
11
} from '@angular/common/http' ;
12
12
import { Observable } from 'rxjs' ;
13
13
import { catchError } from 'rxjs/operators' ;
@@ -16,51 +16,46 @@ import { OAuthModuleConfig } from '../oauth-module.config';
16
16
17
17
@Injectable ( )
18
18
export 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
+ }
24
29
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 ( ) ;
31
35
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
+ }
37
45
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 ;
50
47
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 ;
52
51
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 ) ;
56
53
57
- const headers = req . headers . set ( 'Authorization' , header ) ;
54
+ req = req . clone ( { headers } ) ;
55
+ }
58
56
59
- req = req . clone ( { headers } ) ;
57
+ return next
58
+ . handle ( req )
59
+ . pipe ( catchError ( err => this . errorHandler . handleError ( err ) ) ) ;
60
60
}
61
-
62
- return next
63
- . handle ( req )
64
- . pipe ( catchError ( err => this . errorHandler . handleError ( err ) ) ) ;
65
- }
66
61
}
0 commit comments