Skip to content

Commit

Permalink
Merge pull request #515 from simonmulser/improve-default-oauth-interc…
Browse files Browse the repository at this point in the history
…eptor

Improve default oauth interceptor
  • Loading branch information
manfredsteyer authored Jul 18, 2019
2 parents 01b4f20 + 4780734 commit 4ee15b9
Showing 1 changed file with 57 additions and 33 deletions.
90 changes: 57 additions & 33 deletions projects/lib/src/interceptors/default-oauth.interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { Injectable, Inject, Optional } from '@angular/core';
import { Injectable, Optional } from '@angular/core';
import { OAuthService } from '../oauth-service';
import { OAuthStorage } from '../types';
import {
HttpEvent,
HttpHandler,
HttpInterceptor,
HttpRequest,
HttpResponse,
HttpErrorResponse
HttpEvent,
HttpHandler,
HttpInterceptor,
HttpRequest,
} from '@angular/common/http';
import { Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { Observable, of, merge } from 'rxjs';
import { catchError, filter, map, take, mergeMap, timeout } from 'rxjs/operators';
import { OAuthResourceServerErrorHandler } from './resource-server-error-handler';
import { OAuthModuleConfig } from '../oauth-module.config';
import { isPlatformBrowser } from '@angular/common';

const WAIT_FOR_TOKEN_RECEIVED = 1000;

@Injectable()
export class DefaultOAuthInterceptor implements HttpInterceptor {

constructor(
private authStorage: OAuthStorage,
private errorHandler: OAuthResourceServerErrorHandler,
Expand All @@ -35,35 +35,59 @@ export class DefaultOAuthInterceptor implements HttpInterceptor {
return true;
}

public intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
const url = req.url.toLowerCase();

if (!this.moduleConfig) {
return next.handle(req);
}
if (!this.moduleConfig.resourceServer) {
return next.handle(req);
}
if (!this.checkUrl(url)) {
return next.handle(req);
}
private checkUrl(url: string): boolean {
const found = this.moduleConfig.resourceServer.allowedUrls.find(u => url.startsWith(u));
return !!found;
}

public intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
const url = req.url.toLowerCase();

const sendAccessToken = this.moduleConfig.resourceServer.sendAccessToken;

if (sendAccessToken && this.authStorage.getItem('access_token')) {
const token = this.authStorage.getItem('access_token');
const header = 'Bearer ' + token;
if (!this.moduleConfig) {
return next.handle(req);
}
if (!this.moduleConfig.resourceServer) {
return next.handle(req);
}
if (this.moduleConfig.resourceServer.allowedUrls && !this.checkUrl(url)) {
return next.handle(req);
}

const headers = req.headers.set('Authorization', header);
const sendAccessToken = this.moduleConfig.resourceServer.sendAccessToken;

req = req.clone({ headers });
if (!sendAccessToken) {
return next
.handle(req)
.pipe(catchError(err => this.errorHandler.handleError(err)));
}

return merge(
of(this.oAuthService.getAccessToken()).pipe(
filter(token => token ? true : false),
),
this.oAuthService.events.pipe(
filter(e => e.type === 'token_received'),
timeout(WAIT_FOR_TOKEN_RECEIVED),
map(_ => this.oAuthService.getAccessToken()),
),
).pipe(
take(1),
mergeMap(token => {
if (token) {
const header = 'Bearer ' + token;
const headers = req.headers.set('Authorization', header);
req = req.clone({ headers });
}

return next
.handle(req)
.pipe(catchError(err => this.errorHandler.handleError(err)));
}
.handle(req)
.pipe(catchError(err => this.errorHandler.handleError(err)));
}),
);
}
}

0 comments on commit 4ee15b9

Please sign in to comment.