import { Base } from './base'; import { authorisationTokenRequest } from './request/authorisation-token'; import { ChangePassword } from './models/change-password'; import { Status200Error } from './errors'; import { SessionCookie } from './types/session'; export class Authorisation extends Base { private membershipApi = this.config.get('membershipApi'); public SESSION_COOKIE_EXPIRY = 15552000000; // 6 Months public SESSION_TOKEN_NAME = 'FTSession'; public SECURE_SESSION_TOKEN_NAME = 'FTSession_s'; /** * This function is the public method for getting an authorisation token from the Membership API * @param sessionToken The 'FTSession_s' value from the user's cookie. Obtained by logging in a user - see below. * @returns authorisation token */ public async getToken(sessionToken: string): Promise { const clientId = this.config.get('authorisationApiClientId'); const url = `${this.membershipApi}/authorize`; const additionalHeaders = this.tracingHeaders; return authorisationTokenRequest({ url, clientId, sessionToken, additionalHeaders }, this.fetch); } /** * In order to get the session token needed above, a user needs to be logged in. * This function is the public method for logging in a user to return the 'FTSession_s' value - sessionToken - from the Membership API. * @param email The user's email * @param password The user's password * @param rememberMe Optional, defaults to false. Keeps the user logged in if set to `true` * @returns An Array of objects containing the session cookie data. */ public async login(email: string, password: string, rememberMe: boolean = false): Promise> { const key = this.config.get('loginApiKey'); const url = `${this.membershipApi}/idm/v1/login`; const response = await this.requestPost({ key, url, body: { email, password, rememberMe, context: {}, skipRecaptcha: true } }); const options = { path: '/', domain: '.ft.com', expires: new Date(Date.now() + this.SESSION_COOKIE_EXPIRY) }; if (response.status !== 'SUCCESS') { throw new Status200Error('auth failed', response); } return [{ key: this.SESSION_TOKEN_NAME, token: response.session, options }, { key: this.SECURE_SESSION_TOKEN_NAME, token: response.secureSession, options: Object.assign({}, options, { secure: true }) }]; } /** * This function is the public method for changing a user's password. * @param userId Required * @param reasonForChange Required, the reason the user has chosen to change their password. * @param newPassword * @param oldPassword * @param resetPasswordToken The token in the url link that was given to the user to reset their password. * It returns a status code of 204 */ public async changePassword(userId: string, reasonForChange: string, newPassword?: string, oldPassword?: string, resetPasswordToken?: string): Promise { const key = this.config.get('userCredApiKey'); const url = `${this.membershipApi}/idm/v1/users/${userId}/credentials/change-password`; const body = new ChangePassword(reasonForChange, newPassword, oldPassword, resetPasswordToken).body; const response = await this.requestPost({ key, url, body }); return response.ok; } }