-
Notifications
You must be signed in to change notification settings - Fork 485
Commit
* Support promise based tokenGetter in JwtHelperService * Revert doublequotes * Fix lint issues * Fix tests * fix tests
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { | ||
HttpClientTestingModule, | ||
} from '@angular/common/http/testing'; | ||
import { JwtModule, JwtHelperService } from 'angular-jwt'; | ||
|
||
describe('Example HttpService: with simple based tokken getter', () => { | ||
let service: JwtHelperService; | ||
const tokenGetter = jasmine.createSpy('tokenGetter'); | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ | ||
HttpClientTestingModule, | ||
JwtModule.forRoot({ | ||
config: { | ||
tokenGetter: tokenGetter, | ||
allowedDomains: ['example-1.com', 'example-2.com', 'example-3.com'], | ||
}, | ||
}), | ||
], | ||
}); | ||
service = TestBed.inject(JwtHelperService); | ||
}); | ||
|
||
it('should return null when tokenGetter returns null', () => { | ||
tokenGetter.and.returnValue(null); | ||
|
||
expect(service.decodeToken()).toBeNull(); | ||
}); | ||
|
||
it('should throw an error when token contains less than 2 dots', () => { | ||
tokenGetter.and.returnValue('a.b'); | ||
|
||
expect(() => service.decodeToken()).toThrow(); | ||
}); | ||
|
||
it('should throw an error when token contains more than 2 dots', () => { | ||
tokenGetter.and.returnValue('a.b.c.d'); | ||
|
||
expect(() => service.decodeToken()).toThrow(); | ||
}); | ||
}); | ||
|
||
describe('Example HttpService: with a promise based tokken getter', () => { | ||
let service: JwtHelperService; | ||
const tokenGetter = jasmine.createSpy('tokenGetter'); | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ | ||
HttpClientTestingModule, | ||
JwtModule.forRoot({ | ||
config: { | ||
tokenGetter: tokenGetter, | ||
allowedDomains: ['example-1.com', 'example-2.com', 'example-3.com'], | ||
}, | ||
}), | ||
], | ||
}); | ||
service = TestBed.inject(JwtHelperService); | ||
}); | ||
|
||
it('should return null when tokenGetter returns null', async () => { | ||
tokenGetter.and.resolveTo(null); | ||
|
||
await expectAsync(service.decodeToken()).toBeResolvedTo(null); | ||
}); | ||
|
||
it('should throw an error when token contains less than 2 dots', async () => { | ||
tokenGetter.and.resolveTo('a.b'); | ||
|
||
await expectAsync(service.decodeToken()).toBeRejected(); | ||
}); | ||
|
||
it('should throw an error when token contains more than 2 dots', async () => { | ||
tokenGetter.and.resolveTo('a.b.c.d'); | ||
|
||
await expectAsync(service.decodeToken()).toBeRejected(); | ||
}); | ||
|
||
it('should return the token when tokenGetter returns a valid JWT', async () => { | ||
tokenGetter.and.resolveTo('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2NjY2ODU4NjAsImV4cCI6MTY5ODIyMTg2MCwiYXVkIjoid3d3LmV4YW1wbGUuY29tIiwic3ViIjoianJvY2tldEBleGFtcGxlLmNvbSIsIkdpdmVuTmFtZSI6IkpvaG5ueSIsIlN1cm5hbWUiOiJSb2NrZXQiLCJFbWFpbCI6Impyb2NrZXRAZXhhbXBsZS5jb20iLCJSb2xlIjpbIk1hbmFnZXIiLCJQcm9qZWN0IEFkbWluaXN0cmF0b3IiXX0.lXrRPRZ8VNUpwBsT9fLPPO0p0BotQle4siItqg4LqLQ'); | ||
|
||
await expectAsync(service.decodeToken()).toBeResolvedTo(jasmine.anything()); | ||
}); | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ import { JWT_OPTIONS } from './jwtoptions.token'; | |
|
||
@Injectable() | ||
export class JwtHelperService { | ||
tokenGetter: () => string; | ||
tokenGetter: () => string | Promise<string>; | ||
|
||
constructor(@Inject(JWT_OPTIONS) config = null) { | ||
this.tokenGetter = (config && config.tokenGetter) || function () {}; | ||
|
@@ -77,7 +77,17 @@ export class JwtHelperService { | |
); | ||
} | ||
|
||
public decodeToken<T = any>(token: string = this.tokenGetter()): T { | ||
public decodeToken<T = any>(token: string = null): T | Promise<T> { | ||
const _token = token || this.tokenGetter(); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
frederikprijck
Author
Member
|
||
|
||
if (_token instanceof Promise) { | ||
return _token.then(t => this._decodeToken(t)); | ||
} | ||
|
||
return this._decodeToken(_token); | ||
} | ||
|
||
private _decodeToken(token: string) { | ||
if (!token || token === '') { | ||
return null; | ||
} | ||
|
@@ -99,8 +109,19 @@ export class JwtHelperService { | |
} | ||
|
||
public getTokenExpirationDate( | ||
token: string = this.tokenGetter() | ||
): Date | null { | ||
token: string = null | ||
): Date | null | Promise<Date> { | ||
|
||
const _token = token || this.tokenGetter(); | ||
|
||
if (_token instanceof Promise) { | ||
return _token.then(t => this._getTokenExpirationDate(t)); | ||
} | ||
|
||
return this._getTokenExpirationDate(_token); | ||
} | ||
|
||
private _getTokenExpirationDate(token: string) { | ||
let decoded: any; | ||
decoded = this.decodeToken(token); | ||
|
||
|
@@ -115,7 +136,20 @@ export class JwtHelperService { | |
} | ||
|
||
public isTokenExpired( | ||
token: string = this.tokenGetter(), | ||
token: string = null, | ||
offsetSeconds?: number | ||
): boolean | Promise<boolean> { | ||
const _token = token || this.tokenGetter(); | ||
|
||
if (_token instanceof Promise) { | ||
return _token.then(t => this._isTokenExpired(t, offsetSeconds)); | ||
} | ||
|
||
return this._isTokenExpired(_token, offsetSeconds); | ||
} | ||
|
||
public _isTokenExpired( | ||
token: string, | ||
offsetSeconds?: number | ||
): boolean { | ||
if (!token || token === '') { | ||
|
Leads to error
'T | Promise<T>' is not assignable to type 'T'.
This broke my project. Thanks for minor update :(