|
4 | 4 | * Licensed under the MIT License. See License.txt in the project root for license information.
|
5 | 5 | */
|
6 | 6 |
|
7 |
| -import { NbAuthOAuth2Token, NbAuthJWTToken, NbAuthSimpleToken } from './token'; |
| 7 | +import { NbAuthOAuth2Token, NbAuthJWTToken, NbAuthSimpleToken, NbAuthOAuth2JWTToken } from './token'; |
8 | 8 |
|
9 | 9 |
|
10 | 10 | describe('auth token', () => {
|
@@ -38,23 +38,39 @@ describe('auth token', () => {
|
38 | 38 | invalidJWTToken.getPayload();
|
39 | 39 | })
|
40 | 40 | .toThrow(new Error(
|
41 |
| - `The token ${invalidJWTToken.getValue()} is not valid JWT token and must consist of three parts.`)); |
| 41 | + `The payload ${invalidJWTToken.getValue()} is not valid JWT payload and must consist of three parts.`)); |
42 | 42 | });
|
43 | 43 |
|
44 | 44 | it('getPayload, not valid JWT token, cannot be decoded', () => {
|
45 | 45 | expect(() => {
|
46 | 46 | emptyJWTToken.getPayload();
|
47 | 47 | })
|
48 | 48 | .toThrow(new Error(
|
49 |
| - `The token ${emptyJWTToken.getValue()} is not valid JWT token and cannot be decoded.`)); |
| 49 | + `The payload ${emptyJWTToken.getValue()} is not valid JWT payload and cannot be decoded.`)); |
50 | 50 | });
|
51 | 51 |
|
52 | 52 | it('getPayload, not valid base64 in JWT token, cannot be decoded', () => {
|
53 | 53 | expect(() => {
|
54 | 54 | invalidBase64JWTToken.getPayload();
|
55 | 55 | })
|
56 | 56 | .toThrow(new Error(
|
57 |
| - `The token ${invalidBase64JWTToken.getValue()} is not valid JWT token and cannot be parsed.`)); |
| 57 | + `The payload ${invalidBase64JWTToken.getValue()} is not valid JWT payload and cannot be parsed.`)); |
| 58 | + }); |
| 59 | + |
| 60 | + it('getCreatedAt success : now for simpleToken', () => { |
| 61 | + // we consider dates are the same if differing from minus than 10 ms |
| 62 | + expect(simpleToken.getCreatedAt().getTime() - now.getTime() < 10); |
| 63 | + }); |
| 64 | + |
| 65 | + it('getCreatedAt success : exp for validJWTToken', () => { |
| 66 | + const date = new Date(); |
| 67 | + date.setTime(1532350800000) |
| 68 | + expect(validJWTToken.getCreatedAt()).toEqual(date); |
| 69 | + }); |
| 70 | + |
| 71 | + it('getCreatedAt success : now for noIatJWTToken', () => { |
| 72 | + // we consider dates are the same if differing from minus than 10 ms |
| 73 | + expect(noIatJWTToken.getCreatedAt().getTime() - now.getTime() < 10); |
58 | 74 | });
|
59 | 75 |
|
60 | 76 | it('getCreatedAt success : now for simpleToken', () => {
|
@@ -206,4 +222,114 @@ describe('auth token', () => {
|
206 | 222 | expect(NbAuthOAuth2Token.NAME).toEqual(validToken.getName());
|
207 | 223 | });
|
208 | 224 | });
|
| 225 | + |
| 226 | + describe('NbAuthOAuth2JWTToken', () => { |
| 227 | + |
| 228 | + const exp = 2532350800; |
| 229 | + const iat = 1532350800; |
| 230 | + const expires_in = 1000000000; |
| 231 | + |
| 232 | + const accessTokenPayload = { |
| 233 | + 'iss': 'cerema.fr', |
| 234 | + 'iat': 1532350800, |
| 235 | + 'exp': 2532350800, |
| 236 | + 'sub': 'Alain CHARLES', |
| 237 | + 'admin': true, |
| 238 | + }; |
| 239 | + |
| 240 | + const validPayload = { |
| 241 | + // tslint:disable-next-line |
| 242 | + access_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjZXJlbWEuZnIiLCJpYXQiOjE1MzIzNTA4MDAsImV4cCI6MjUzMjM1MDgwMCwic3ViIjoiQWxhaW4gQ0hBUkxFUyIsImFkbWluIjp0cnVlfQ.Rgkgb4KvxY2wp2niXIyLJNJeapFp9z3tCF-zK6Omc8c', |
| 243 | + expires_in: 1000000000, |
| 244 | + refresh_token: 'tGzv3JOkF0XG5Qx2TlKWIA', |
| 245 | + token_type: 'bearer', |
| 246 | + example_parameter: 'example_value', |
| 247 | + }; |
| 248 | + |
| 249 | + const noExpButIatPayload = { |
| 250 | + // tslint:disable-next-line |
| 251 | + access_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjZXJlbWEuZnIiLCJpYXQiOjE1MzIzNTA4MDAsInN1YiI6IkFsYWluIENIQVJMRVMiLCJhZG1pbiI6dHJ1ZX0.heHVXkHexwqbPCPUAvkJlXO6tvxzxTKf4iP0OWBbp7Y', |
| 252 | + expires_in: expires_in, |
| 253 | + refresh_token: 'tGzv3JOkF0XG5Qx2TlKWIA', |
| 254 | + token_type: 'bearer', |
| 255 | + example_parameter: 'example_value', |
| 256 | + }; |
| 257 | + |
| 258 | + const noExpNoIatPayload = { |
| 259 | + // tslint:disable-next-line |
| 260 | + access_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjZXJlbWEuZnIiLCJzdWIiOiJBbGFpbiBDSEFSTEVTIiwiYWRtaW4iOnRydWV9.LKZggkN-r_5hnEcCg5GzbSqZz5_SUHEB1Bf9Sy1qJd4', |
| 261 | + expires_in: expires_in, |
| 262 | + refresh_token: 'tGzv3JOkF0XG5Qx2TlKWIA', |
| 263 | + token_type: 'bearer', |
| 264 | + example_parameter: 'example_value', |
| 265 | + }; |
| 266 | + |
| 267 | + const permanentPayload = { |
| 268 | + // tslint:disable-next-line |
| 269 | + access_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJjZXJlbWEuZnIiLCJzdWIiOiJBbGFpbiBDSEFSTEVTIiwiYWRtaW4iOnRydWV9.LKZggkN-r_5hnEcCg5GzbSqZz5_SUHEB1Bf9Sy1qJd4', |
| 270 | + token_type: 'bearer', |
| 271 | + example_parameter: 'example_value', |
| 272 | + }; |
| 273 | + |
| 274 | + const validToken = new NbAuthOAuth2JWTToken(validPayload, 'strategy'); |
| 275 | + let noExpButIatToken = new NbAuthOAuth2JWTToken(noExpButIatPayload, 'strategy'); |
| 276 | + const emptyToken = new NbAuthOAuth2JWTToken({}, 'strategy'); |
| 277 | + const permanentToken = new NbAuthOAuth2JWTToken(permanentPayload, 'strategy'); |
| 278 | + |
| 279 | + it('getPayload success', () => { |
| 280 | + expect(validToken.getPayload()).toEqual(validPayload); |
| 281 | + }); |
| 282 | + |
| 283 | + it('getAccessTokenPayload success', () => { |
| 284 | + expect(validToken.getAccessTokenPayload()).toEqual(accessTokenPayload); |
| 285 | + }); |
| 286 | + |
| 287 | + it('getPayload, not valid token, cannot be decoded', () => { |
| 288 | + expect(() => { |
| 289 | + emptyToken.getPayload(); |
| 290 | + }) |
| 291 | + .toThrow(new Error( |
| 292 | + `Cannot extract payload from an empty token.`)); |
| 293 | + }); |
| 294 | + |
| 295 | + it('getCreatedAt success for valid token', () => { |
| 296 | + const date = new Date(0); |
| 297 | + date.setUTCSeconds(iat); |
| 298 | + expect(validToken.getCreatedAt()).toEqual(date); |
| 299 | + }); |
| 300 | + |
| 301 | + it('getCreatedAt success for no iat token', () => { |
| 302 | + noExpButIatToken = new NbAuthOAuth2JWTToken(noExpButIatPayload, 'strategy'); |
| 303 | + const date = new Date(); |
| 304 | + expect(noExpButIatToken.getTokenExpDate().getTime() - date.getTime() < 10); |
| 305 | + }); |
| 306 | + |
| 307 | + it('getExpDate success when exp is set', () => { |
| 308 | + const date = new Date(0); |
| 309 | + date.setUTCSeconds(exp); |
| 310 | + expect(validToken.getTokenExpDate()).toEqual(date); |
| 311 | + }); |
| 312 | + |
| 313 | + it('getExpDate success when exp is not set but iat and expires_in are set', () => { |
| 314 | + const date = new Date(0); |
| 315 | + date.setUTCSeconds(iat + expires_in); |
| 316 | + expect(noExpButIatToken.getTokenExpDate()).toEqual(date); |
| 317 | + }); |
| 318 | + |
| 319 | + it('getExpDate success when only expires_in is set', () => { |
| 320 | + const NoExpNoIatToken = new NbAuthOAuth2JWTToken(noExpNoIatPayload, 'strategy'); |
| 321 | + const date = new Date(); |
| 322 | + date.setTime(date.getTime() + expires_in * 1000); |
| 323 | + expect(NoExpNoIatToken.getTokenExpDate().getTime() - date.getTime() < 10); |
| 324 | + }); |
| 325 | + |
| 326 | + it('getTokenExpDate is empty', () => { |
| 327 | + expect(permanentToken.getTokenExpDate()).toBeNull(); |
| 328 | + }); |
| 329 | + |
| 330 | + it('name', () => { |
| 331 | + expect(NbAuthOAuth2JWTToken.NAME).toEqual(validToken.getName()); |
| 332 | + }); |
| 333 | + }); |
| 334 | + |
209 | 335 | });
|
0 commit comments