2019-12-08 00:32:00 +05:30
|
|
|
import { UserResponse } from 'app/services/api/accounts';
|
|
|
|
import logger from 'app/services/logger';
|
|
|
|
import request, { InternalServerError } from 'app/services/request';
|
|
|
|
import { getInfo as getInfoEndpoint } from 'app/services/api/accounts';
|
2016-06-04 19:16:39 +05:30
|
|
|
|
2019-01-28 00:42:58 +05:30
|
|
|
export interface OAuthResponse {
|
2019-11-27 14:33:32 +05:30
|
|
|
access_token: string;
|
|
|
|
refresh_token?: string;
|
|
|
|
expires_in: number; // count seconds before expire
|
|
|
|
success: true;
|
2019-01-28 00:42:58 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export function login({
|
2019-11-27 14:33:32 +05:30
|
|
|
login,
|
|
|
|
password,
|
|
|
|
totp,
|
|
|
|
rememberMe = false,
|
2019-01-28 00:42:58 +05:30
|
|
|
}: {
|
2019-12-07 16:58:52 +05:30
|
|
|
login: string;
|
|
|
|
password?: string;
|
|
|
|
totp?: string;
|
|
|
|
rememberMe: boolean;
|
2019-01-28 00:42:58 +05:30
|
|
|
}): Promise<OAuthResponse> {
|
2019-11-27 14:33:32 +05:30
|
|
|
return request.post(
|
|
|
|
'/api/authentication/login',
|
|
|
|
{
|
|
|
|
login,
|
|
|
|
password,
|
|
|
|
totp,
|
|
|
|
rememberMe,
|
|
|
|
},
|
|
|
|
{ token: null },
|
|
|
|
);
|
2019-01-28 00:42:58 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} token - an optional token to overwrite headers
|
|
|
|
* in middleware and disable token auto-refresh
|
|
|
|
*
|
2019-11-27 14:33:32 +05:30
|
|
|
* @returns {Promise}
|
2019-01-28 00:42:58 +05:30
|
|
|
*/
|
2019-11-27 14:33:32 +05:30
|
|
|
export function logout(token?: string): Promise<{ success: boolean }> {
|
|
|
|
return request.post(
|
|
|
|
'/api/authentication/logout',
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
token,
|
|
|
|
},
|
|
|
|
);
|
2019-01-28 00:42:58 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
export function forgotPassword(
|
|
|
|
login: string,
|
|
|
|
captcha: string,
|
|
|
|
): Promise<{
|
2019-12-07 16:58:52 +05:30
|
|
|
success: boolean;
|
2019-11-27 14:33:32 +05:30
|
|
|
data: {
|
2019-12-07 16:58:52 +05:30
|
|
|
canRepeatIn: number;
|
|
|
|
emailMask: string | void;
|
|
|
|
repeatFrequency: number;
|
|
|
|
};
|
2019-11-27 14:33:32 +05:30
|
|
|
errors: {
|
2019-12-07 16:58:52 +05:30
|
|
|
[key: string]: string;
|
|
|
|
};
|
2019-01-28 00:42:58 +05:30
|
|
|
}> {
|
2019-11-27 14:33:32 +05:30
|
|
|
return request.post(
|
|
|
|
'/api/authentication/forgot-password',
|
|
|
|
{
|
|
|
|
login,
|
|
|
|
captcha,
|
|
|
|
},
|
|
|
|
{ token: null },
|
|
|
|
);
|
2019-01-28 00:42:58 +05:30
|
|
|
}
|
2016-07-28 10:33:30 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
export function recoverPassword(
|
|
|
|
key: string,
|
|
|
|
newPassword: string,
|
|
|
|
newRePassword: string,
|
|
|
|
): Promise<OAuthResponse> {
|
|
|
|
return request.post(
|
|
|
|
'/api/authentication/recover-password',
|
|
|
|
{
|
|
|
|
key,
|
|
|
|
newPassword,
|
|
|
|
newRePassword,
|
|
|
|
},
|
|
|
|
{ token: null },
|
|
|
|
);
|
2019-01-28 00:42:58 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves if token is valid
|
|
|
|
*
|
|
|
|
* @param {number} id
|
|
|
|
* @param {string} token
|
|
|
|
* @param {string} refreshToken
|
|
|
|
*
|
2019-11-27 14:33:32 +05:30
|
|
|
* @returns {Promise} - resolves with options.token or with a new token
|
2019-01-28 00:42:58 +05:30
|
|
|
* if it was refreshed. As a side effect the response
|
|
|
|
* will have a `user` field with current user data
|
|
|
|
*
|
|
|
|
*/
|
2019-11-27 14:33:32 +05:30
|
|
|
export async function validateToken(
|
|
|
|
id: number,
|
|
|
|
token: string,
|
2019-12-07 16:58:52 +05:30
|
|
|
refreshToken: string | void | null,
|
2019-11-27 14:33:32 +05:30
|
|
|
): Promise<{
|
2019-12-07 16:58:52 +05:30
|
|
|
token: string;
|
|
|
|
refreshToken: string | null;
|
|
|
|
user: UserResponse;
|
2019-01-28 00:42:58 +05:30
|
|
|
}> {
|
2019-11-27 14:33:32 +05:30
|
|
|
if (typeof token !== 'string') {
|
|
|
|
throw new Error('token must be a string');
|
|
|
|
}
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
refreshToken = refreshToken || null;
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
let user: UserResponse;
|
|
|
|
try {
|
|
|
|
user = await getInfoEndpoint(id, token);
|
|
|
|
} catch (resp) {
|
|
|
|
token = await handleTokenError(resp, refreshToken);
|
|
|
|
user = await getInfoEndpoint(id, token); // TODO: replace with recursive call
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
token,
|
|
|
|
refreshToken,
|
|
|
|
user,
|
|
|
|
};
|
2019-01-28 00:42:58 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
const recoverableErrors = [
|
2019-11-27 14:33:32 +05:30
|
|
|
'Token expired',
|
|
|
|
'Incorrect token',
|
|
|
|
'You are requesting with an invalid credential.',
|
2019-01-28 00:42:58 +05:30
|
|
|
];
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
function handleTokenError(
|
|
|
|
resp: Error | { message: string },
|
2019-12-07 16:58:52 +05:30
|
|
|
refreshToken: string | null,
|
2019-11-27 14:33:32 +05:30
|
|
|
): Promise<string> {
|
|
|
|
if (resp instanceof InternalServerError) {
|
|
|
|
// delegate error recovering to the bsod middleware
|
|
|
|
return new Promise(() => {});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (refreshToken) {
|
|
|
|
if (recoverableErrors.includes(resp.message)) {
|
|
|
|
return requestToken(refreshToken);
|
2019-01-28 00:42:58 +05:30
|
|
|
}
|
2019-01-25 22:59:32 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
logger.error('Unexpected error during token validation', { resp });
|
|
|
|
}
|
2016-11-05 15:41:41 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return Promise.reject(resp);
|
2019-01-28 00:42:58 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request new access token using a refreshToken
|
|
|
|
*
|
|
|
|
* @param {string} refreshToken
|
|
|
|
*
|
2019-11-27 14:33:32 +05:30
|
|
|
* @returns {Promise} - resolves to token
|
2019-01-28 00:42:58 +05:30
|
|
|
*/
|
|
|
|
export async function requestToken(refreshToken: string): Promise<string> {
|
2019-11-27 14:33:32 +05:30
|
|
|
try {
|
|
|
|
const response: OAuthResponse = await request.post(
|
|
|
|
'/api/authentication/refresh-token',
|
|
|
|
{
|
2019-12-07 16:58:52 +05:30
|
|
|
refresh_token: refreshToken,
|
2019-11-27 14:33:32 +05:30
|
|
|
},
|
|
|
|
{
|
|
|
|
token: null,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
return response.access_token;
|
|
|
|
} catch (resp) {
|
|
|
|
const errors = resp.errors || {};
|
|
|
|
|
|
|
|
if (errors.refresh_token !== 'error.refresh_token_not_exist') {
|
|
|
|
logger.error('Failed refreshing token: unknown error', {
|
|
|
|
resp,
|
|
|
|
});
|
2016-06-04 19:16:39 +05:30
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
throw resp;
|
|
|
|
}
|
2019-01-28 00:42:58 +05:30
|
|
|
}
|