Skip to content

Commit

Permalink
Pass the HttpRequest to the token getter
Browse files Browse the repository at this point in the history
  • Loading branch information
Sambego committed May 6, 2020
1 parent 07e880b commit 3bc2be4
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 19 deletions.
37 changes: 22 additions & 15 deletions projects/angular-jwt/src/lib/angular-jwt.module.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { NgModule, ModuleWithProviders, Optional, SkipSelf, Provider } from '@angular/core';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import {JwtInterceptor} from './jwt.interceptor';
import {JWT_OPTIONS} from './jwtoptions.token';
import {JwtHelperService} from './jwthelper.service';

import {
NgModule,
ModuleWithProviders,
Optional,
SkipSelf,
Provider,
} from "@angular/core";
import { HttpRequest, HTTP_INTERCEPTORS } from "@angular/common/http";
import { JwtInterceptor } from "./jwt.interceptor";
import { JWT_OPTIONS } from "./jwtoptions.token";
import { JwtHelperService } from "./jwthelper.service";

export interface JwtModuleOptions {
jwtOptionsProvider?: Provider;
config?: {
tokenGetter?: () => string | null | Promise<string | null>;
tokenGetter?: (
request?: HttpRequest<any>
) => string | null | Promise<string | null>;
headerName?: string;
authScheme?: string;
whitelistedDomains?: Array<string | RegExp>;
Expand All @@ -20,10 +27,11 @@ export interface JwtModuleOptions {

@NgModule()
export class JwtModule {

constructor(@Optional() @SkipSelf() parentModule: JwtModule) {
if (parentModule) {
throw new Error('JwtModule is already loaded. It should only be imported in your application\'s main module.');
throw new Error(
"JwtModule is already loaded. It should only be imported in your application's main module."
);
}
}
static forRoot(options: JwtModuleOptions): ModuleWithProviders<JwtModule> {
Expand All @@ -33,15 +41,14 @@ export class JwtModule {
{
provide: HTTP_INTERCEPTORS,
useClass: JwtInterceptor,
multi: true
multi: true,
},
options.jwtOptionsProvider ||
{
options.jwtOptionsProvider || {
provide: JWT_OPTIONS,
useValue: options.config
useValue: options.config,
},
JwtHelperService
]
JwtHelperService,
],
};
}
}
6 changes: 4 additions & 2 deletions projects/angular-jwt/src/lib/jwt.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import { from, Observable } from "rxjs";

@Injectable()
export class JwtInterceptor implements HttpInterceptor {
tokenGetter: () => string | null | Promise<string | null>;
tokenGetter: (
request?: HttpRequest<any>
) => string | null | Promise<string | null>;
headerName: string;
authScheme: string;
whitelistedDomains: Array<string | RegExp>;
Expand Down Expand Up @@ -112,7 +114,7 @@ export class JwtInterceptor implements HttpInterceptor {
) {
return next.handle(request);
}
const token = this.tokenGetter();
const token = this.tokenGetter(request);

if (token instanceof Promise) {
return from(token).pipe(
Expand Down
65 changes: 63 additions & 2 deletions src/app/services/example-http.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@ import {
import { JwtModule } from "angular-jwt";

export function tokenGetter() {
return "SOME_TEST_TOKEN";
return "TEST_TOKEN";
}

describe("ExampleHttpService", () => {
export function tokenGetterWithRequest(request) {
if (request.url.includes("1")) {
return "TEST_TOKEN_1";
}

if (request.url.includes("2")) {
return "TEST_TOKEN_2";
}

return "TEST_TOKEN";
}

describe("Example HttpService: with simple tokken getter", () => {
let service: ExampleHttpService;
let httpMock: HttpTestingController;

Expand Down Expand Up @@ -82,3 +94,52 @@ describe("ExampleHttpService", () => {
})
);
});

describe("Example HttpService: with request based tokken getter", () => {
let service: ExampleHttpService;
let httpMock: HttpTestingController;

const routes = [
`http://example-1.com/api/`,
`http://example-2.com/api/`,
`http://example-3.com/api/`,
];

beforeEach(() => {
TestBed.configureTestingModule({
imports: [
HttpClientTestingModule,
JwtModule.forRoot({
config: {
tokenGetter: tokenGetterWithRequest,
whitelistedDomains: [
"example-1.com",
"example-2.com",
"example-3.com",
],
},
}),
],
});
service = TestBed.get(ExampleHttpService);
httpMock = TestBed.get(HttpTestingController);
});

it("should add Authorisation header", () => {
expect(service).toBeTruthy();
});

routes.forEach((route) =>
it(`should set the correct auth token for a domain: ${route}`, () => {
service.testRequest(route).subscribe((response) => {
expect(response).toBeTruthy();
});

const httpRequest = httpMock.expectOne(route);
expect(httpRequest.request.headers.has("Authorization")).toEqual(true);
expect(httpRequest.request.headers.get("Authorization")).toEqual(
`Bearer ${tokenGetterWithRequest({ url: route })}`
);
})
);
});

0 comments on commit 3bc2be4

Please sign in to comment.