Skip to content
Snippets Groups Projects
Commit cad9997a authored by Homada.Boumedane's avatar Homada.Boumedane
Browse files

feat(dlcm-portal): add refresh token flow

Use refresh token to get new access_token for the expire one.

Closes #DLCM-595
parent 2cecbac8
No related branches found
No related tags found
1 merge request!8feat(dlcm-portal): add refresh token flow
......@@ -40,6 +40,7 @@ export class AppState {
constructor(private translate: TranslateService,
private oauthService: OAuthService) {
this.oauthService.configure(authConfig);
this.oauthService.setupAutomaticRefreshToken();
}
......
import { Injectable, Inject, Optional } from "@angular/core";
import { OAuthService } from "../oauth-service";
import { OAuthStorage } from "../types";
import {
HttpEvent,
HttpHandler,
HttpInterceptor,
HttpRequest,
HttpResponse,
HttpErrorResponse
} from "@angular/common/http";
import { Observable } from "rxjs";
import { catchError } from "rxjs/operators";
import { OAuthResourceServerErrorHandler } from "./resource-server-error-handler";
import { OAuthModuleConfig } from "../oauth-module.config";
import {Injectable, Optional} from "@angular/core";
import {OAuthStorage} from "../types";
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from "@angular/common/http";
import {Observable} from "rxjs";
import {catchError} from "rxjs/operators";
import {OAuthResourceServerErrorHandler} from "./resource-server-error-handler";
import {OAuthModuleConfig} from "../oauth-module.config";
@Injectable()
export class DefaultOAuthInterceptor implements HttpInterceptor {
constructor(
private authStorage: OAuthStorage,
private errorHandler: OAuthResourceServerErrorHandler,
@Optional() private moduleConfig: OAuthModuleConfig) { }
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>> {
console.log(req);
const url = req.url.toLowerCase();
constructor(private authStorage: OAuthStorage,
private errorHandler: OAuthResourceServerErrorHandler,
@Optional() private moduleConfig: OAuthModuleConfig) {
}
private checkUrl(url: string): boolean {
const found = this.moduleConfig.resourceServer.allowedUrls.find(u => url.startsWith(u));
return !!found;
}
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);
}
public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const url = req.url.toLowerCase();
const sendAccessToken = this.moduleConfig.resourceServer.sendAccessToken;
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);
}
if (sendAccessToken && this.authStorage.getItem("access_token")) {
const token = this.authStorage.getItem("access_token");
const header = "Bearer " + token;
const sendAccessToken = this.moduleConfig.resourceServer.sendAccessToken;
const headers = req.headers.set("Authorization", header);
if (sendAccessToken && this.authStorage.getItem("access_token")) {
const token = this.authStorage.getItem("access_token");
const header = "Bearer " + token;
req = req.clone({ headers });
}
const headers = req.headers.set("Authorization", header);
return next
.handle(req)
.pipe(catchError(err => this.errorHandler.handleError(err)));
req = req.clone({headers});
}
return next
.handle(req)
.pipe(catchError(err => this.errorHandler.handleError(err)));
}
}
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment