Skip to content

Commit

Permalink
feat(auth): ability to add additional request headers (#2825)
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
`NbOAuth2AuthStrategy.buildAuthHeader` return type changed from `any` to `HttpHeaders | undefined`. If the method is overridden in a derived class, update its return type as well.
  • Loading branch information
evtkhvch committed Nov 16, 2021
1 parent d56a492 commit 3959848
Show file tree
Hide file tree
Showing 4 changed files with 255 additions and 270 deletions.
2 changes: 2 additions & 0 deletions src/framework/auth/strategies/auth-strategy-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright Akveo. All Rights Reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/
import { HttpHeaders } from '@angular/common/http';
import { NbAuthTokenClass } from '../services/token/token';

export interface NbStrategyToken {
Expand All @@ -13,4 +14,5 @@ export interface NbStrategyToken {
export class NbAuthStrategyOptions {
name: string;
token?: NbStrategyToken;
headers?: HttpHeaders | { [header: string]: string | string[]; };
}
24 changes: 16 additions & 8 deletions src/framework/auth/strategies/auth-strategy.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { HttpResponse } from '@angular/common/http';
import { HttpHeaders, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { NbAuthResult } from '../services/auth-result';
import { NbAuthStrategyOptions } from './auth-strategy-options';
import { deepExtend, getDeepFromObject } from '../helpers';
import {
NbAuthToken,
nbAuthCreateToken,
NbAuthIllegalTokenError,
} from '../services/token/token';
import { NbAuthToken, nbAuthCreateToken, NbAuthIllegalTokenError } from '../services/token/token';

export abstract class NbAuthStrategy {

protected defaultOptions: NbAuthStrategyOptions;
protected options: NbAuthStrategyOptions;

Expand All @@ -25,7 +20,7 @@ export abstract class NbAuthStrategy {
}

createToken<T extends NbAuthToken>(value: any, failWhenInvalidToken?: boolean): T {
const token = nbAuthCreateToken<T>(this.getOption('token.class'), value, this.getName());
const token = nbAuthCreateToken<T>(this.getOption('token.class'), value, this.getName());
// At this point, nbAuthCreateToken failed with NbAuthIllegalTokenError which MUST be intercepted by strategies
// Or token is created. It MAY be created even if backend did not return any token, in this case it is !Valid
if (failWhenInvalidToken && !token.isValid()) {
Expand Down Expand Up @@ -65,4 +60,17 @@ export abstract class NbAuthStrategy {
const baseEndpoint: string = this.getOption('baseEndpoint');
return actionEndpoint ? baseEndpoint + actionEndpoint : '';
}

protected getHeaders(): HttpHeaders {
const customHeaders: NbAuthStrategyOptions['headers'] = this.getOption('headers') ?? {};
if (customHeaders instanceof HttpHeaders) {
return customHeaders;
}

let headers = new HttpHeaders();
Object.entries(customHeaders).forEach(([key, value]) => {
headers = headers.append(key, value);
});
return headers;
}
}
Loading

0 comments on commit 3959848

Please sign in to comment.