Skip to content

Commit 58aba0b

Browse files
authored
Merge pull request #155 from ioet/148-send-jwt
fix: #148 adding interceptor to inject Authorization headers
2 parents c490d9d + fa8b88e commit 58aba0b

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

src/app/app.module.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { CommonModule } from '@angular/common';
22
import { BrowserModule } from '@angular/platform-browser';
33
import { NgModule } from '@angular/core';
44
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
5-
import { HttpClientModule } from '@angular/common/http';
5+
import { HttpClientModule, HTTP_INTERCEPTORS } from '@angular/common/http';
66
import { StoreModule } from '@ngrx/store';
77
import { EffectsModule } from '@ngrx/effects';
88
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
@@ -50,6 +50,7 @@ import { ProjectTypeListComponent } from './modules/customer-management/componen
5050
// tslint:disable-next-line: max-line-length
5151
import { CreateProjectTypeComponent } from './modules/customer-management/components/projects-type/components/create-project-type/create-project-type.component';
5252
import { CustomerEffects } from './modules/customer-management/store/customer-management.effects';
53+
import { InjectTokenInterceptor } from './modules/shared/interceptors/inject.token.interceptor';
5354

5455
@NgModule({
5556
declarations: [
@@ -103,7 +104,11 @@ import { CustomerEffects } from './modules/customer-management/store/customer-ma
103104
: [],
104105
EffectsModule.forRoot([ProjectEffects, ActivityEffects, CustomerEffects, TechnologyEffects]),
105106
],
106-
providers: [],
107+
providers: [{
108+
provide: HTTP_INTERCEPTORS,
109+
useClass: InjectTokenInterceptor,
110+
multi: true,
111+
}],
107112
bootstrap: [AppComponent],
108113
})
109114
export class AppModule {}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { HttpHandler, HttpRequest, HttpResponse, HttpEvent } from '@angular/common/http';
2+
import { Observable, of } from 'rxjs';
3+
4+
import { AzureAdB2CService } from 'src/app/modules/login/services/azure.ad.b2c.service';
5+
import { environment } from '../../../../environments/environment';
6+
import { InjectTokenInterceptor } from './inject.token.interceptor';
7+
8+
describe('InjectTokenInterceptor test', () => {
9+
10+
class MockHttpHandler extends HttpHandler {
11+
handle(req: HttpRequest<any>): Observable<HttpEvent<any>> {
12+
return of(new HttpResponse());
13+
}
14+
}
15+
16+
const azureAdB2CService: AzureAdB2CService = new AzureAdB2CService();
17+
azureAdB2CService.getBearerToken = () => {
18+
return 'XYZ';
19+
};
20+
const handler: HttpHandler = new MockHttpHandler();
21+
22+
it('if request.url is not part of time-tracker-api, then next.handle', () => {
23+
const interceptor = new InjectTokenInterceptor(azureAdB2CService);
24+
const request = new HttpRequest('GET', '/foo');
25+
spyOn(handler, 'handle');
26+
27+
interceptor.intercept(request, handler);
28+
29+
expect(handler.handle).toHaveBeenCalledWith(request);
30+
});
31+
32+
it('if request.url is part of time-tracker-api, then Authorization header is injected', () => {
33+
const interceptor = new InjectTokenInterceptor(azureAdB2CService);
34+
const request = new HttpRequest('GET', environment.timeTrackerApiUrl);
35+
spyOn(handler, 'handle');
36+
const requestWithHeaders = request.clone(
37+
{
38+
headers: request.headers.set('Authorization', 'Bearer XYZ')
39+
});
40+
41+
interceptor.intercept(request, handler);
42+
43+
expect(handler.handle).toHaveBeenCalledWith(requestWithHeaders);
44+
});
45+
46+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Injectable } from '@angular/core';
2+
import {
3+
HttpEvent,
4+
HttpInterceptor,
5+
HttpHandler,
6+
HttpRequest,
7+
} from '@angular/common/http';
8+
import { Observable } from 'rxjs';
9+
10+
import { AzureAdB2CService } from 'src/app/modules/login/services/azure.ad.b2c.service';
11+
import { environment } from './../../../../environments/environment';
12+
13+
@Injectable()
14+
export class InjectTokenInterceptor implements HttpInterceptor {
15+
16+
constructor(private azureAdB2CService: AzureAdB2CService) { }
17+
18+
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
19+
if (request.url.startsWith(environment.timeTrackerApiUrl)) {
20+
const requestWithHeaders = request.clone(
21+
{
22+
headers: request.headers.set('Authorization',
23+
'Bearer ' + this.azureAdB2CService.getBearerToken())
24+
});
25+
return next.handle(requestWithHeaders);
26+
} else {
27+
return next.handle(request);
28+
}
29+
}
30+
31+
}

0 commit comments

Comments
 (0)