liveperson-functions-cli
Version:
LivePerson Functions CLI
71 lines • 2.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoginService = void 0;
const got_1 = require("got");
const csds_service_1 = require("./csds.service");
class LoginService {
constructor({ csdsClient = new csds_service_1.CsdsClient(), gotDefault = got_1.default, } = {}) {
this.csdsClient = csdsClient;
this.got = gotDefault;
}
/**
* Checks if the token is still valid
* @param { token, accountId, userId }
* @returns {Promise<boolean>}
* @memberof LoginService
*/
async isTokenValid({ csrf, accountId, sessionId, }) {
try {
const domain = await this.csdsClient.getUri(accountId, 'agentVep');
const url = `https://${domain}/api/account/${accountId}/refresh`;
await this.got(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Cookie: `session_id=${sessionId}`,
},
json: { csrf },
});
return true;
}
catch (error) {
if (error.message.includes('401') ||
error.message === 'Service "agentVep" could not be found.') {
return false;
}
throw new Error(error);
}
}
/**
* Performs the login to the agentVep service
* @param { accountId, username, password }
* @returns {Promise<ILoginResponse>} - bearer, username and userId
* @memberof LoginService
*/
async login({ accountId, username, password, }) {
try {
const domain = await this.csdsClient.getUri(accountId, 'agentVep');
const url = `https://${domain}/api/account/${accountId}/login?v=1.3`;
return await this.got(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
'user-agent': 'faas-cli',
},
responseType: 'json',
resolveBodyOnly: true,
body: JSON.stringify({
username,
password,
}),
});
}
catch (error) {
throw new Error(error);
}
}
}
exports.LoginService = LoginService;
//# sourceMappingURL=login.service.js.map