liveperson-functions-client
Version:
JavaScript client for LivePerson Functions.
117 lines • 5.01 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const jwt = __importStar(require("jsonwebtoken"));
const appJwtAuthentication_1 = require("../../src/helper/appJwtAuthentication");
const secret = 'mySecret';
const validAccessToken = {
access_token: jwt.sign({
aud: 'le4711',
azp: 'bf16f923-b256-40c8-afa5-1b8e8372da09',
scope: 'faas.lambda.invoke',
iss: 'Sentinel',
exp: Date.now() / 1000 + 60 * 60,
iat: Date.now(),
}, secret),
};
const invalidAccessToken = 'not-a-valid-token';
const mockClientCredentials = (token) => ({
getToken: async () => ({
token,
expired: () => false,
refresh: async () => null,
revoke: async () => { },
revokeAll: async () => { },
}),
});
jest.mock('simple-oauth2', () => ({
ClientCredentials: jest.fn(() => mockClientCredentials(validAccessToken)),
}));
const simple_oauth2_1 = require("simple-oauth2");
const createMock = simple_oauth2_1.ClientCredentials;
describe('AppJWT Authentication', () => {
afterEach(jest.clearAllMocks);
describe('Success flows', () => {
const getCsdsEntry = async () => 'sentinel.liveperson.net';
it('should return bearer header', async () => {
const auth = new appJwtAuthentication_1.AppJwtAuthentication({
accountId: 'le4711',
clientId: '4711',
clientSecret: '4711',
getCsdsEntry,
});
const bearer = await auth.getHeader();
expect(simple_oauth2_1.ClientCredentials).toBeCalledTimes(1);
expect(bearer).toBeNonEmptyString();
expect(bearer).toContain('Bearer');
});
it('should not create a new one if old one is not expired', async () => {
const auth = new appJwtAuthentication_1.AppJwtAuthentication({
accountId: 'le4711',
clientId: '4711',
clientSecret: '4711',
getCsdsEntry,
});
await auth.getHeader();
await auth.getHeader();
expect(simple_oauth2_1.ClientCredentials).toBeCalledTimes(1);
});
});
describe('Unhappy flows', () => {
const getCsdsEntry = async () => 'sentinel.liveperson.net';
it('should throw error if access token invalid', async () => {
createMock.mockReturnValue(mockClientCredentials(invalidAccessToken));
const auth = new appJwtAuthentication_1.AppJwtAuthentication({
accountId: 'le4711',
clientId: '4711',
clientSecret: '4711',
getCsdsEntry,
});
await expect(auth.getHeader()).rejects.toMatchObject({
name: 'FaaSAppJWTAuthenticationError',
message: 'Error while creating authentication bearer via AppJWT (Client Credentials): Current AppJWT is expired and new Jwt could not be retrieved.',
});
});
it('should not reset the access token if retrieving the JWT failed but the old JWT is still active', async () => {
createMock
.mockReturnValueOnce(mockClientCredentials(validAccessToken))
.mockReturnValueOnce(mockClientCredentials(invalidAccessToken));
const auth = new appJwtAuthentication_1.AppJwtAuthentication({
accountId: 'le4711',
clientId: '4711',
clientSecret: '4711',
getCsdsEntry,
expirationBufferMinutes: 70,
});
const bearer = await auth.getHeader();
const bearer2 = await auth.getHeader();
expect(simple_oauth2_1.ClientCredentials).toBeCalledTimes(2);
expect(bearer).toBeNonEmptyString();
expect(bearer).toContain('Bearer');
expect(bearer2).toEqual(bearer);
});
});
});
//# sourceMappingURL=appJwtAuthentication.test.js.map