Skip to content

Commit

Permalink
feat(auth): secure token with trust hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarbeau committed Mar 15, 2018
1 parent 4313fd9 commit 89bd0f2
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 21 deletions.
10 changes: 2 additions & 8 deletions src/demo-app/environments/environment.prod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,11 @@
// `ng build --env=prod` then `environment.prod.ts` will be used instead.
// The list of which env maps to which file can be found in `.angular-cli.json`.

import { SearchSourcesOptions, LanguageOptions, AuthOptions,
ContextServiceOptions } from '../../lib';
import { IgoEnvironment } from '../../lib';

interface Environment {
production: boolean;
igo: {
searchSources?: SearchSourcesOptions;
language?: LanguageOptions;
auth?: AuthOptions;
context?: ContextServiceOptions;
};
igo: IgoEnvironment;
};

export const environment: Environment = {
Expand Down
10 changes: 2 additions & 8 deletions src/demo-app/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,11 @@
// `ng build --env=prod` then `environment.prod.ts` will be used instead.
// The list of which env maps to which file can be found in `.angular-cli.json`.

import { SearchSourcesOptions, LanguageOptions, AuthOptions,
ContextServiceOptions } from '../../lib';
import { IgoEnvironment } from '../../lib';

interface Environment {
production: boolean;
igo: {
searchSources?: SearchSourcesOptions;
language?: LanguageOptions;
auth?: AuthOptions;
context?: ContextServiceOptions;
};
igo: IgoEnvironment;
};

export const environment: Environment = {
Expand Down
14 changes: 12 additions & 2 deletions src/lib/auth/shared/auth.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,28 @@ import { HttpEvent, HttpInterceptor, HttpHandler,
HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';

import { ConfigService } from '../../core/config/config.service';
import { TokenService } from './token.service';

@Injectable()
export class AuthInterceptor implements HttpInterceptor {

constructor(private tokenService: TokenService) {}
private trustHosts: string[] = [];

constructor(private tokenService: TokenService, private config: ConfigService) {
this.trustHosts = this.config.getConfig('auth.trustHosts') || [];
this.trustHosts.push(window.location.hostname);
}

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const token = this.tokenService.get();
if (!token) {
const element = document.createElement('a');
element.href = req.url;

if (!token && !this.trustHosts.includes(element.hostname)) {
return next.handle(req);
}

const authHeader = `Bearer ${token}`;
const authReq = req.clone({headers: req.headers.set('Authorization', authHeader)});
return next.handle(authReq);
Expand Down
1 change: 1 addition & 0 deletions src/lib/auth/shared/auth.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface AuthOptions {
intern?: AuthInternOptions;
facebook?: AuthFacebookOptions;
google?: AuthGoogleOptions;
trustHosts?: string[];
}

export interface User {
Expand Down
10 changes: 7 additions & 3 deletions src/lib/core/config/config.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from '@angular/core';
import { Injectable, Injector } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { catchError } from 'rxjs/operators';
Expand All @@ -12,7 +12,7 @@ export class ConfigService {

private config: Object = {};

constructor(private http: HttpClient) {}
constructor(private injector: Injector) {}

/**
* Use to get the data found in config file
Expand All @@ -28,11 +28,15 @@ export class ConfigService {
if (options.default) {
this.config = options.default;
}

if (!options.path) {
return true;
}

const http = this.injector.get(HttpClient);

return new Promise((resolve, reject) => {
this.http.get(options.path).pipe(
http.get(options.path).pipe(
catchError((error: any): any => {
console.log(`Configuration file ${options.path} could not be read`);
resolve(true);
Expand Down
12 changes: 12 additions & 0 deletions src/lib/environment.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { SearchSourcesOptions, LanguageOptions, AuthOptions,
ContextServiceOptions, CatalogServiceOptions, ImportExportServiceOptions
} from './';

export interface IgoEnvironment {
searchSources?: SearchSourcesOptions;
language?: LanguageOptions;
auth?: AuthOptions;
context?: ContextServiceOptions;
catalog?: CatalogServiceOptions;
importExport?: ImportExportServiceOptions;
}
2 changes: 2 additions & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ export * from './shared';
export * from './share-map';
export * from './tool';
export * from './utils';

export * from './environment.interface';

0 comments on commit 89bd0f2

Please sign in to comment.