import { RenewTokenResponse, UserAuthenticationResponse, ValidateTokenResponse, BasicReponse, ValidateClientAPIResponse } from './interfaces/response.interface' import axios from 'axios' export class Authorization { constructor (private applicationKey: string, private baseURL: string) { this.baseURL += '/authorization' } private getURL (endpoint: string): string { return this.baseURL + endpoint } /** * @param email user email to be authenticated * @param password user password to be authenticated * @param tokenExpirationTime the duration of the auth token in minutes */ public authenticateUser ( email: string, password:string, tokenExpirationTime = 2880 ): Promise { const endpoint = '/authenticate' const body = { Email: email, Password: password, ApplicationKey: this.applicationKey, TokenExpirationInMinutes: tokenExpirationTime } return axios .post(this.getURL(endpoint), body) .then(result => result.data) } /** * Validates a token generated by the Authenticate resource. * You must call the ValidateToken resource before any call * to a critical section of your application, to ensure the * authenticity of the credentials * @param token The authentication token generated by the Authenticate resource, to be validated */ public validateToken (token: string): Promise { const endpoint = '/validateToken' const body = { ApplicationKey: this.applicationKey, Token: token } return axios .post(this.getURL(endpoint), body) .then(result => result.data) } /** * Extends the duration of a existing token generated by the Authenticate resource. * You must call the RenewToken resource before yout actual token expires or the * request is going to fail. * @param oldToken The actual authentication token generated by the Authenticate resource */ public renewToken (oldToken: string, tokenExpirationTime: number): Promise { const endpoint = '/renewToken' const body: any = { ApplicationKey: this.applicationKey, Token: oldToken } if (tokenExpirationTime) { body.TokenExpirationInMinutes = tokenExpirationTime } return axios .post(this.getURL(endpoint), body) .then(result => result.data) } /** * Check if a user belongs to one or more of the specified roles * @param userKey Unique global key identifying the user to be checked * @param roles array of roles to check */ public isUserInRoles (userKey:string, roles: [string]): Promise { const endpoint = '/isUserInRole' if (!roles || roles.length < 1) { return Promise.reject(new Error('You must provide the user roles to be verified!')) } const body = { ApplicationKey: this.applicationKey, UserKey: userKey, RoleCollection: roles } return axios .post(this.getURL(endpoint), body) .then(result => result.data) } /** * Sends an email to the user containing instructions on how to redefine his password * @param email E-mail of the user requesting the password recovery */ public recoverPassword (email:string): Promise { const endpoint = '/recoverPassword' const body = { ApplicationKey: this.applicationKey, Email: email } return axios .post(this.getURL(endpoint), body) .then(result => result.data) } /** * Validates a given Client Application Key * @param ClientApplicationKey Key that identifies an application * @param rawData random string * @param encryptedData random string encrypted using HMAC SHA256 protocol */ public validateApplication ( ClientApplicationKey : string, RawData: string, EncryptedData: string ) : Promise { const endpoint = '/validateapplication' const body = { ClientApplicationKey, RawData, EncryptedData, ApplicationKey: this.applicationKey } return axios .post(this.getURL(endpoint), body) .then(result => result.data) .catch(error => error) } }