Commit df63a192 authored by Eduardo Sanz García's avatar Eduardo Sanz García
Browse files

feat: JwtModule must be set up independently

parent 80706fe5
<a name="1.0.0-alpha.10"></a>
# [1.0.0-alpha.10](https://gitlab.ebi.ac.uk/tools-glue/angular-aap-auth/compare/1.0.0-alpha.9...1.0.0-alpha.10) (2018-06-18)
### Features
* JwtModule must be set up independently of the AuthModule. Both must have the
same tokenGetter function in order to work properly.
<a name="1.0.0-alpha.9"></a>
# [1.0.0-alpha.9](https://gitlab.ebi.ac.uk/tools-glue/angular-aap-auth/compare/1.0.0-alpha.8...1.0.0-alpha.9) (2018-05-16)
### Features
* upgrade to @auth0/angular-jwt version 2 and drop dependency of rxjs-compat
<a name="1.0.0-alpha.7"></a>
<a name="1.0.0-alpha.8"></a>
# [1.0.0-alpha.8](https://gitlab.ebi.ac.uk/tools-glue/angular-aap-auth/compare/1.0.0-alpha.7...1.0.0-alpha.8) (2018-05-08)
......@@ -19,7 +25,6 @@
when no value for the claim was found. Fixed.
<a name="1.0.0-alpha.6"></a>
# [1.0.0-alpha.6](https://gitlab.ebi.ac.uk/tools-glue/angular-aap-auth/compare/1.0.0-alpha.5...1.0.0-alpha.6) (2018-02-16)
### Features
......
......@@ -15,11 +15,11 @@ session storage).
To install this library, run:
```
npm install --save angular-aap-auth
npm install --save angular-aap-auth @auth0/angular-jwt
or
yarn add angular-aap-auth
yarn add angular-aap-auth @auth0/angular-jwt
```
Compatibility table
......@@ -55,6 +55,11 @@ import {
imports: [
BrowserModule,
AuthModule.forRoot(),
JwtModuld.forRoot({
config: {
tokenGetter: () => localStorage.getItem( 'id_token')
}
})
],
providers: [],
bootstrap: [AppComponent]
......@@ -214,6 +219,11 @@ export function removeToken(): void {
tokenUpdater: updateToken,
tokenRemover: removeToken // Optional
}),
JwtModuld.forRoot({
config: {
tokenGetter: getToken,
}
})
],
providers: [],
bootstrap: [AppComponent]
......
{
"name": "angular-aap-auth",
"version": "1.0.0-alpha.9",
"version": "1.0.0-alpha.10",
"license": "Apache-2.0",
"scripts": {
"ng": "ng",
......
......@@ -11,14 +11,14 @@ import {
AppComponent
} from './app.component';
import {
AuthModule
} from './modules/auth/auth.module';
CommonStub
} from 'app/../../testing/common';
describe('AppComponent', () => {
beforeEach(async (() => {
TestBed.configureTestingModule({
imports: [
AuthModule.forRoot()
CommonStub
],
declarations: [
AppComponent
......
......@@ -11,6 +11,9 @@ import {
import {
AuthModule
} from './modules/auth/auth.module';
import {
JwtModule
} from '@auth0/angular-jwt';
export function getToken(): string {
return localStorage.getItem('jwt_token') || '';
......@@ -36,12 +39,12 @@ export function removeToken(): void {
tokenUpdater: updateToken,
// tokenRemover: removeToken // Optional
}),
// JwtModule.forRoot({
// config: {
// tokenGetter: getToken,
// whitelistedDomains: []
// }
// })
JwtModule.forRoot({
config: {
tokenGetter: getToken,
whitelistedDomains: []
}
})
],
providers: [
],
......
......@@ -33,15 +33,7 @@ export class AuthModule {
static forRoot(options?: AuthConfig): ModuleWithProviders {
return {
ngModule: AuthModule,
providers: [{
provide: JWT_OPTIONS,
useValue: options ? {
tokenGetter: options.tokenGetter
} : {
tokenGetter: DEFAULT_CONF.tokenGetter
}
},
JwtHelperService,
providers: [
TokenService,
{
provide: AAP_CONFIG,
......
import {
TestBed,
inject,
// fakeAsync,
// flushMicrotasks
fakeAsync,
flushMicrotasks
} from '@angular/core/testing';
import {
......@@ -25,6 +25,9 @@ import {
} from './auth.config';
describe('AuthService (valid token)', () => {
let service: AuthService;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [{
......@@ -44,57 +47,65 @@ describe('AuthService (valid token)', () => {
});
});
it('should be created', inject([AuthService], (service: AuthService) => {
beforeEach(inject([AuthService], (serv: AuthService) => { service = serv; }));
it('should be created', () => {
expect(service).toBeTruthy();
}));
});
it('should be authenticated', inject([AuthService], (service: AuthService) => {
it('should be authenticated', () => {
const isAuthenticated = service.isAuthenticated();
isAuthenticated.subscribe(result => expect(result).toBeTruthy());
}));
});
it('should have credentials', inject([AuthService], (service: AuthService) => {
it('should have credentials', () => {
const credentials = service.credentials();
credentials.subscribe(result => expect(result).toBeTruthy());
}));
});
it('should have username', inject([AuthService], (service: AuthService) => {
it('should have username', () => {
const username = service.username();
username.subscribe(result => expect(result).toEqual('test@ebi.ac.uk'));
}));
});
it('should have realname', inject([AuthService], (service: AuthService) => {
it('should have realname', () => {
const realname = service.realname();
realname.subscribe(result => expect(result).toEqual('Ed Munden Gras'));
}));
});
it('should have token', inject([AuthService], (service: AuthService) => {
it('should have token', () => {
const token = service.token();
token.subscribe(result => expect(result).toEqual(VALID_TOKEN));
}));
});
// It doesn't work because async and timer issues
xit('should have log out', inject([AuthService], (service: AuthService) => {
service.logOut();
const isAuthenticated = service.isAuthenticated();
isAuthenticated.subscribe(result => expect(result).toBeFalsy());
}));
it('should be correct single sign on URL', inject([AuthService], (service: AuthService) => {
it('should have log out', fakeAsync(() => {
let isAuthenticated = false;
service.isAuthenticated().subscribe(result => isAuthenticated = result);
flushMicrotasks();
expect(isAuthenticated).toEqual(true);
// This doesn't work because the token is not coming from local storage but is a constant value.
// service.logOut();
// window.dispatchEvent(new Event('storage'));
// flushMicrotasks();
// expect(isAuthenticated).toEqual(false);
}));
it('should be correct single sign on URL', () => {
expect(service.getSSOURL({
'ttl': '30',
'o': '3'
}))
.toEqual('https://api.aai.ebi.ac.uk/sso?from=http%3A%2F%2Flocalhost%3A9876&ttl=30&o=3');
}));
});
it('should be correct single sign on URL', inject([AuthService], (service: AuthService) => {
it('should be correct single sign on URL', () => {
expect(service.getSSOURL({
'ttl': '1441',
'o': '3'
}))
.toEqual('https://api.aai.ebi.ac.uk/sso?from=http%3A%2F%2Flocalhost%3A9876&ttl=1440&o=3');
}));
});
});
describe('AuthService (expired token)', () => {
......
import {
NgModule
} from '@angular/core';
import {
JwtModule
} from '@auth0/angular-jwt';
import {
AuthModule
} from '../src/app/modules/auth/auth.module';
export const jwt_token = 'test';
export function getToken(): string {
return localStorage.getItem('jwt_token') || '';
}
export function updateToken(newToken: string): void {
return localStorage.setItem('jwt_token', newToken);
}
// Optional
export function removeToken(): void {
return localStorage.removeItem('jwt_token');
}
@NgModule({
imports: [
AuthModule.forRoot({
aapURL: 'https://blah.com',
tokenGetter: getToken,
tokenUpdater: updateToken,
tokenRemover: removeToken // Optional
}),
JwtModule.forRoot({
config: {
tokenGetter: getToken,
whitelistedDomains: []
}
}),
],
exports: [
JwtModule,
AuthModule,
],
})
export class CommonStub {}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment