Skip to content

Commit

Permalink
feat(admin-ui): Support bearer token auth method
Browse files Browse the repository at this point in the history
Relates to #138
  • Loading branch information
michaelbromley committed Aug 20, 2019
1 parent 574d70b commit c31a383
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Location } from '@angular/common';
import { Injectable } from '@angular/core';

export type LocalStorageKey = 'activeChannelToken';
export type LocalStorageKey = 'activeChannelToken' | 'authToken';
export type LocalStorageLocationBasedKey = 'shippingTestOrder' | 'shippingTestAddress';
const PREFIX = 'vnd_';

Expand Down
14 changes: 13 additions & 1 deletion admin-ui/src/app/data/data.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function createApollo(
localStorageService: LocalStorageService,
fetchAdapter: FetchAdapter,
): ApolloClientOptions<any> {
const { apiHost, apiPort, adminApiPath } = getAppConfig();
const { apiHost, apiPort, adminApiPath, tokenMethod } = getAppConfig();
const host = apiHost === 'auto' ? `${location.protocol}//${location.hostname}` : apiHost;
const port = apiPort === 'auto' ? (location.port === '' ? '' : `:${location.port}`) : `:${apiPort}`;
const apolloCache = new InMemoryCache({
Expand Down Expand Up @@ -54,6 +54,18 @@ export function createApollo(
};
}
}),
setContext(() => {
if (tokenMethod === 'bearer') {
const authToken = localStorageService.get('authToken');
if (authToken) {
return {
headers: {
authorization: `Bearer ${authToken}`,
},
};
}
}
}),
createUploadLink({
uri: `${host}${port}/${adminApiPath}`,
fetch: fetchAdapter.fetch,
Expand Down
24 changes: 23 additions & 1 deletion admin-ui/src/app/data/providers/interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { Injectable, Injector } from '@angular/core';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { DEFAULT_AUTH_TOKEN_HEADER_KEY } from 'shared/shared-constants';
import { AdminUiConfig } from 'shared/shared-types';

import { getAppConfig } from '../../app.config';
import { AuthService } from '../../core/providers/auth/auth.service';
Expand All @@ -27,20 +29,27 @@ export const AUTH_REDIRECT_PARAM = 'redirectTo';
*/
@Injectable()
export class DefaultInterceptor implements HttpInterceptor {
private readonly tokenMethod: AdminUiConfig['tokenMethod'] = 'cookie';
private readonly authTokenHeaderKey: string;

constructor(
private dataService: DataService,
private injector: Injector,
private authService: AuthService,
private router: Router,
private localStorageService: LocalStorageService,
) {}
) {
this.tokenMethod = getAppConfig().tokenMethod;
this.authTokenHeaderKey = getAppConfig().authTokenHeaderKey || DEFAULT_AUTH_TOKEN_HEADER_KEY;
}

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.dataService.client.startRequest().subscribe();
return next.handle(req).pipe(
tap(
event => {
if (event instanceof HttpResponse) {
this.checkForAuthToken(event);
this.notifyOnError(event);
this.dataService.client.completeRequest().subscribe();
}
Expand Down Expand Up @@ -105,4 +114,17 @@ export class DefaultInterceptor implements HttpInterceptor {
const notificationService = this.injector.get<NotificationService>(NotificationService);
notificationService.error(message, vars);
}

/**
* If the server is configured to use the "bearer" tokenMethod, each response should be checked
* for the existence of an auth token.
*/
private checkForAuthToken(response: HttpResponse<any>) {
if (this.tokenMethod === 'bearer') {
const authToken = response.headers.get(this.authTokenHeaderKey);
if (authToken) {
this.localStorageService.set('authToken', authToken);
}
}
}
}
2 changes: 2 additions & 0 deletions packages/common/src/shared-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,6 @@ export interface AdminUiConfig {
apiHost: string | 'auto';
apiPort: number | 'auto';
adminApiPath: string;
tokenMethod: 'cookie' | 'bearer';
authTokenHeaderKey: string;
}

0 comments on commit c31a383

Please sign in to comment.