@speechly/browser-client
Advanced tools
Comparing version 1.0.0-beta.3 to 1.0.0-beta.4
{ | ||
"name": "@speechly/browser-client", | ||
"version": "1.0.0-beta.3", | ||
"version": "1.0.0-beta.4", | ||
"description": "Browser client for Speechly API", | ||
@@ -5,0 +5,0 @@ "private": false, |
declare type fetchFn = (input: RequestInfo, init?: RequestInit) => Promise<Response>; | ||
declare type nowFn = () => number; | ||
export declare const minTokenValidTime: number; | ||
export interface Token { | ||
@@ -10,3 +11,3 @@ appId: string; | ||
audience: string; | ||
expiresAt: number; | ||
expiresAtMs: number; | ||
} | ||
@@ -13,0 +14,0 @@ export declare function fetchToken(baseUrl: string, appId: string, deviceId: string, fetcher?: fetchFn, nowFn?: nowFn): Promise<string>; |
@@ -13,3 +13,3 @@ "use strict"; | ||
const base_64_1 = require("base-64"); | ||
const secondsInHour = 60 * 60; | ||
exports.minTokenValidTime = 60 * 60 * 1000; // 1 hour | ||
function fetchToken(baseUrl, appId, deviceId, fetcher = fetch, nowFn = Date.now) { | ||
@@ -42,4 +42,3 @@ var _a; | ||
const decoded = decodeToken(token); | ||
// If the token will expire in an hour or less, mark it as invalid. | ||
if ((decoded.expiresAt - now()) / 1000 < secondsInHour) { | ||
if (decoded.expiresAtMs - now() < exports.minTokenValidTime) { | ||
return false; | ||
@@ -72,3 +71,3 @@ } | ||
audience: body.aud, | ||
expiresAt: body.exp, | ||
expiresAtMs: body.exp * 1000, | ||
}; | ||
@@ -75,0 +74,0 @@ } |
@@ -17,3 +17,3 @@ "use strict"; | ||
const f = mockFetch(200, { access_token: testTokenString }); | ||
yield expect(token_1.fetchToken('base-url', testToken.appId, testToken.deviceId, f, jest.fn().mockReturnValue(0))).resolves.toBe(testTokenString); | ||
yield expect(token_1.fetchToken('base-url', testToken.appId, testToken.deviceId, f, beforeExpiry(testToken))).resolves.toBe(testTokenString); | ||
})); | ||
@@ -23,7 +23,7 @@ test('throws if API call fails', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const f = jest.fn().mockRejectedValue(err); | ||
yield expect(token_1.fetchToken('base-url', testToken.appId, testToken.deviceId, f, jest.fn().mockReturnValue(0))).rejects.toThrow(err); | ||
yield expect(token_1.fetchToken('base-url', testToken.appId, testToken.deviceId, f, beforeExpiry(testToken))).rejects.toThrow(err); | ||
})); | ||
test('throws if API returns non-OK status', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const f = mockFetch(400, { access_token: testTokenString }); | ||
yield expect(token_1.fetchToken('base-url', testToken.appId, testToken.deviceId, f, jest.fn().mockReturnValue(0))).rejects.toThrow(); | ||
yield expect(token_1.fetchToken('base-url', testToken.appId, testToken.deviceId, f, beforeExpiry(testToken))).rejects.toThrow(); | ||
})); | ||
@@ -33,11 +33,11 @@ test('throws if body cannot be read', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const f = mockFailFetch(500, err); | ||
yield expect(token_1.fetchToken('base-url', testToken.appId, testToken.deviceId, f, jest.fn().mockReturnValue(0))).rejects.toThrow(err); | ||
yield expect(token_1.fetchToken('base-url', testToken.appId, testToken.deviceId, f, beforeExpiry(testToken))).rejects.toThrow(err); | ||
})); | ||
test('throws if token is not in response', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const f = mockFetch(200, {}); | ||
yield expect(token_1.fetchToken('base-url', testToken.appId, testToken.deviceId, f, jest.fn().mockReturnValue(0))).rejects.toThrow(); | ||
yield expect(token_1.fetchToken('base-url', testToken.appId, testToken.deviceId, f, beforeExpiry(testToken))).rejects.toThrow(); | ||
})); | ||
test('throws if token is invalid', () => __awaiter(void 0, void 0, void 0, function* () { | ||
const f = mockFetch(200, { access_token: 'some-invalid-token' }); | ||
yield expect(token_1.fetchToken('base-url', testToken.appId, testToken.deviceId, f, jest.fn().mockReturnValue(0))).rejects.toThrow(); | ||
yield expect(token_1.fetchToken('base-url', testToken.appId, testToken.deviceId, f, beforeExpiry(testToken))).rejects.toThrow(); | ||
})); | ||
@@ -47,12 +47,12 @@ }); | ||
test('returns true for correct token', () => { | ||
expect(token_1.validateToken(testTokenString, testToken.appId, testToken.deviceId, jest.fn().mockReturnValue(0))).toBeTruthy(); | ||
expect(token_1.validateToken(testTokenString, testToken.appId, testToken.deviceId, beforeExpiry(testToken))).toBeTruthy(); | ||
}); | ||
test('returns false when appId does not match', () => { | ||
expect(token_1.validateToken(testTokenString, 'other-app-id', testToken.deviceId, jest.fn().mockReturnValue(0))).toBeFalsy(); | ||
expect(token_1.validateToken(testTokenString, 'other-app-id', testToken.deviceId, beforeExpiry(testToken))).toBeFalsy(); | ||
}); | ||
test('returns false when deviceId does not match', () => { | ||
expect(token_1.validateToken(testTokenString, testToken.appId, 'other-device-id', jest.fn().mockReturnValue(0))).toBeFalsy(); | ||
expect(token_1.validateToken(testTokenString, testToken.appId, 'other-device-id', beforeExpiry(testToken))).toBeFalsy(); | ||
}); | ||
test('returns false when token is expired', () => { | ||
expect(token_1.validateToken(testTokenString, testToken.appId, testToken.deviceId, jest.fn().mockReturnValue(9999999999))).toBeFalsy(); | ||
expect(token_1.validateToken(testTokenString, testToken.appId, testToken.deviceId, afterExpiry(testToken))).toBeFalsy(); | ||
}); | ||
@@ -89,3 +89,3 @@ }); | ||
audience: 'https://api.speechly.com/', | ||
expiresAt: 1599313656, | ||
expiresAtMs: 1599313656000, | ||
}; | ||
@@ -100,2 +100,8 @@ function mockFetch(status, data) { | ||
} | ||
function beforeExpiry(t) { | ||
return jest.fn().mockReturnValue(t.expiresAtMs - token_1.minTokenValidTime); | ||
} | ||
function afterExpiry(t) { | ||
return jest.fn().mockReturnValue(t.expiresAtMs); | ||
} | ||
//# sourceMappingURL=token.test.js.map |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
146552
2743