UNPKG

@apihawk/billia-sdk

Version:

The ApiHawk Billia SDK

378 lines (377 loc) 13.6 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const md5 = require("md5"); const billia_sdk_service_base_1 = require("../lib/billia-sdk-service-base"); class BilliaSDKAuthentication extends billia_sdk_service_base_1.BilliaSDKServiceBase { /** * Check if user exists * @param {string} username * @returns {Promise<any>} */ checkUsername(username) { return __awaiter(this, void 0, void 0, function* () { return yield this.api.call({ url: `/user/finder/${username}`, method: 'GET', headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json' } }); }); } /** * Authenticate user * @param {string} username * @param {string} password * @returns {Promise} */ authenticate(username, password) { return __awaiter(this, void 0, void 0, function* () { const validationError = yield this.validate({ username, password }, [ 'authentication', 'authenticate' ]); if (validationError) { throw validationError; } let userSession = { details: {}, access_token: '', token_type: '', refresh_token: '', expires_in: 0, expires: 0, sid: '' }; const oauthReponse = yield this.api.call({ url: '/oauth', method: 'POST', body: { username, password, grant_type: 'password' }, auth: true, headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json' } }); // user settings const now = Math.floor(Date.now() / 1000); userSession = { details: { username }, access_token: oauthReponse.access_token, token_type: oauthReponse.token_type, refresh_token: oauthReponse.refresh_token, expires_in: oauthReponse.expires_in, expires: now + parseInt(oauthReponse.expires_in, 10) - 2, sid: '' }; return this.getSessionDetails(userSession); }); } /** * One time login with token * @param {string} token * @returns {Promise<IBilliaUserSession>} */ tokenLogin(token) { return __awaiter(this, void 0, void 0, function* () { const session = yield this.api.call({ url: `/user/one-time-login/${token}`, method: 'POST', body: [], headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json' } }); const whoAmI = yield this.api.call({ url: '/customer/who-am-i', method: 'GET', session: session, headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json' } }); const now = Math.floor(Date.now() / 1000); const userSession = { details: { username: whoAmI.user.username || session.user_id, user_id: whoAmI.user.id, first_name: whoAmI.user.first_name, last_name: whoAmI.user.last_name, status: whoAmI.user.status, gravatar: md5(whoAmI.user.username), role: whoAmI.user.role, permissions: whoAmI.permissions, support_pin: whoAmI.user.support_pin, impersonated: session.op_id != null && session.op_id > 0 }, access_token: session.access_token, token_type: session.token_type || 'Bearer', refresh_token: session.refresh_token || '', expires_in: session.expires_in, expires: now + session.expires_in - 2, sid: '' }; return userSession; }); } /** * Fast creating account * @param {string} email * @returns {Promise} */ fastCreateAccount(email, lang) { return __awaiter(this, void 0, void 0, function* () { const headers = { Accept: 'application/hal+json', 'Content-Type': 'application/json' }; if (lang) { headers['Accept-Language'] = lang; } const response = yield this.api.call({ url: '/customer/fast-create', method: 'POST', body: { email }, headers }); const now = Math.floor(Date.now() / 1000); const userSession = { details: { username: response.username }, access_token: response.login.access_token, token_type: response.token_type || 'Bearer', refresh_token: response.refresh_token, expires_in: response.expires_in || 3600, expires: now + parseInt(response.expires_in || 3600, 10) - 2 }; return this.getSessionDetails(userSession); }); } /** * Send email for reset password * @param {string} email * @returns {Promise<boolean>} */ sendResetPasswordEmail(email) { return __awaiter(this, void 0, void 0, function* () { return yield this.api .call({ url: `/customer/password-reset?username=${email}`, method: 'GET', headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json' } }) .then(() => true); }); } /** * Reset user password * @param {string} password * @param {string} token * @returns {Promise<boolean>} */ resetPassword(password, token) { return __awaiter(this, void 0, void 0, function* () { return this.api .call({ url: '/customer/password-reset', method: 'POST', body: { account: token, new_password: password }, headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json' } }) .then(() => true); }); } /** * Activate user account * @param {string} token * @param password password * @returns {Promise<IBilliaUserSession>} */ activateAccount(token, password) { return __awaiter(this, void 0, void 0, function* () { const activateAccountData = {}; let userSession = {}; if (password) { activateAccountData.password = password; } return this.api .call({ url: `/customer/activate?account=${token}`, method: 'POST', body: activateAccountData, headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json' } }) .then((response) => { const now = Math.floor(Date.now() / 1000); userSession = { details: { username: response.authorization.user_id }, access_token: response.authorization.access_token, token_type: response.token_type || 'Bearer', refresh_token: response.authorization.refresh_token || '', expires_in: response.authorization.expires_in, expires: now + parseInt(response.authorization.expires_in, 10) - 2 }; return this.api.call({ url: '/customer/who-am-i', method: 'GET', session: userSession, headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json' } }); }) .then((details) => { userSession.details = { username: details.user.username, user_id: details.user.id, first_name: details.user.first_name, last_name: details.user.last_name, status: details.user.status, gravatar: md5(details.user.username), role: details.user.role, permissions: details.permissions }; return userSession; }); }); } /** * Reactivate user account * @param {string} email * @returns {Promise<boolean>} */ reactivateAccount(email) { return __awaiter(this, void 0, void 0, function* () { return this.api .call({ url: `/customer/reactivate?email=${email}`, method: 'GET', headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json' } }) .then(() => true); }); } /** * Get social providers * @returns {Promise<any>} */ getSocialProviders() { return __awaiter(this, void 0, void 0, function* () { return this.api.call({ url: '/social/account/login/providers', method: 'GET', headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json' } }); }); } /** * Impoersonate an account * @returns {Promise<any>} */ impersonate(accessToken) { return __awaiter(this, void 0, void 0, function* () { const now = Math.floor(Date.now() / 1000); const userSession = { details: {}, access_token: accessToken, token_type: 'Bearer', refresh_token: null, expires_in: 3600, expires: now + 3600 - 2, sid: '' }; return this.getSessionDetails(userSession); }); } getSessionDetails(userSession) { return __awaiter(this, void 0, void 0, function* () { const details = yield this.api.call({ url: '/customer/who-am-i', method: 'GET', session: userSession, headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json' } }); userSession.details = { username: details.user.username, user_id: details.user.id, // firstName: details.user.first_name, // lastName: details.user.last_name, first_name: details.user.first_name, last_name: details.user.last_name, status: details.user.status, gravatar: md5(details.user.username), role: details.user.role, permissions: details.permissions, support_pin: details.user.support_pin }; const settings = yield this.api.call({ url: '/customer/users-setting', method: 'GET', session: userSession, headers: { Accept: 'application/hal+json', 'Content-Type': 'application/json' } }); const tfa = { enabled: false, username: '' }; settings._embedded.oauth_users_setting.forEach((setting) => { if (setting.name === 'twoFAUsername') { tfa.username = setting.value; } if (setting.name === 'twoFAEnabled') { tfa.enabled = setting.value === 'true'; } }); userSession.details.tfa = tfa; return userSession; }); } } exports.BilliaSDKAuthentication = BilliaSDKAuthentication;