2019-12-08 00:32:00 +05:30
|
|
|
import { getJwtPayloads } from 'app/functions';
|
|
|
|
import { sessionStorage } from 'app/services/localStorage';
|
2020-05-24 04:38:24 +05:30
|
|
|
import { validateToken, requestToken, logout } from 'app/services/api/authentication';
|
|
|
|
import { relogin as navigateToLogin, setAccountSwitcher } from 'app/components/auth/actions';
|
2019-12-08 00:32:00 +05:30
|
|
|
import { updateUser, setGuest } from 'app/components/user/actions';
|
|
|
|
import { setLocale } from 'app/components/i18n/actions';
|
|
|
|
import logger from 'app/services/logger';
|
2020-07-22 15:31:12 +05:30
|
|
|
import { Action as AppAction } from 'app/types';
|
2016-10-30 17:42:49 +05:30
|
|
|
|
2019-12-26 17:48:58 +05:30
|
|
|
import { getActiveAccount, Account } from './reducer';
|
2020-05-24 04:38:24 +05:30
|
|
|
import { add, remove, activate, reset, updateToken } from './actions/pure-actions';
|
2017-01-27 11:59:20 +05:30
|
|
|
|
2018-03-14 02:21:37 +05:30
|
|
|
export { updateToken, activate, remove };
|
2016-10-30 17:42:49 +05:30
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Account|object} account
|
|
|
|
* @param {string} account.token
|
|
|
|
* @param {string} account.refreshToken
|
2016-11-08 12:00:53 +05:30
|
|
|
*
|
2019-11-27 14:33:32 +05:30
|
|
|
* @returns {Function}
|
2016-10-30 17:42:49 +05:30
|
|
|
*/
|
2019-11-27 14:33:32 +05:30
|
|
|
export function authenticate(
|
2020-05-24 04:38:24 +05:30
|
|
|
account:
|
|
|
|
| Account
|
|
|
|
| {
|
|
|
|
token: string;
|
|
|
|
refreshToken: string | null;
|
|
|
|
},
|
2020-07-22 15:31:12 +05:30
|
|
|
): AppAction<Promise<Account>> {
|
2020-05-24 04:38:24 +05:30
|
|
|
const { token, refreshToken } = account;
|
|
|
|
const email = 'email' in account ? account.email : null;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return async (dispatch, getState) => {
|
|
|
|
let accountId: number;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
if ('id' in account && typeof account.id === 'number') {
|
|
|
|
accountId = account.id;
|
|
|
|
} else {
|
|
|
|
accountId = findAccountIdFromToken(token);
|
|
|
|
}
|
2018-02-28 02:47:31 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
const knownAccount = getState().accounts.available.find((item) => item.id === accountId);
|
2018-02-28 02:47:31 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
if (knownAccount) {
|
|
|
|
// this account is already available
|
|
|
|
// activate it before validation
|
|
|
|
dispatch(activate(knownAccount));
|
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
try {
|
|
|
|
const { token: newToken, refreshToken: newRefreshToken, user } = await validateToken(
|
|
|
|
accountId,
|
|
|
|
token,
|
|
|
|
refreshToken,
|
|
|
|
);
|
|
|
|
const { auth } = getState();
|
|
|
|
const newAccount: Account = {
|
|
|
|
id: user.id,
|
|
|
|
username: user.username,
|
|
|
|
email: user.email,
|
|
|
|
token: newToken,
|
|
|
|
refreshToken: newRefreshToken,
|
|
|
|
};
|
|
|
|
dispatch(add(newAccount));
|
|
|
|
dispatch(activate(newAccount));
|
|
|
|
dispatch(
|
|
|
|
updateUser({
|
|
|
|
isGuest: false,
|
|
|
|
...user,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
// TODO: probably should be moved from here, because it is a side effect
|
|
|
|
logger.setUser(user);
|
|
|
|
|
|
|
|
if (!newRefreshToken) {
|
|
|
|
// mark user as stranger (user does not want us to remember his account)
|
|
|
|
sessionStorage.setItem(`stranger${newAccount.id}`, '1');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auth && auth.oauth && auth.oauth.clientId) {
|
|
|
|
// if we authenticating during oauth, we disable account chooser
|
|
|
|
// because user probably has made his choise now
|
|
|
|
// this may happen, when user registers, logs in or uses account
|
|
|
|
// chooser panel during oauth
|
|
|
|
dispatch(setAccountSwitcher(false));
|
|
|
|
}
|
|
|
|
|
|
|
|
await dispatch(setLocale(user.lang));
|
|
|
|
|
|
|
|
return newAccount;
|
|
|
|
} catch (resp) {
|
|
|
|
// all the logic to get the valid token was failed,
|
|
|
|
// looks like we have some problems with token
|
|
|
|
// lets redirect to login page
|
|
|
|
if (typeof email === 'string') {
|
|
|
|
// TODO: we should somehow try to find email by token
|
|
|
|
dispatch(relogin(email));
|
|
|
|
}
|
|
|
|
|
|
|
|
throw resp;
|
|
|
|
}
|
|
|
|
};
|
2019-01-28 00:42:58 +05:30
|
|
|
}
|
2016-12-06 00:44:38 +05:30
|
|
|
|
2019-12-29 17:27:44 +05:30
|
|
|
/**
|
|
|
|
* Re-fetch user data for currently active account
|
|
|
|
*/
|
2020-07-22 15:31:12 +05:30
|
|
|
export function refreshUserData(): AppAction<Promise<void>> {
|
2020-05-24 04:38:24 +05:30
|
|
|
return async (dispatch, getState) => {
|
|
|
|
const activeAccount = getActiveAccount(getState());
|
2019-12-29 17:27:44 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
if (!activeAccount) {
|
|
|
|
throw new Error('Can not fetch user data. No user.id available');
|
|
|
|
}
|
2019-12-29 17:27:44 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
await dispatch(authenticate(activeAccount));
|
|
|
|
};
|
2019-12-29 17:27:44 +05:30
|
|
|
}
|
|
|
|
|
2019-01-28 00:42:58 +05:30
|
|
|
function findAccountIdFromToken(token: string): number {
|
2020-05-24 04:38:24 +05:30
|
|
|
const { sub, jti } = getJwtPayloads(token);
|
2017-01-31 11:35:36 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
// sub has the format "ely|{accountId}", so we must trim "ely|" part
|
|
|
|
if (sub) {
|
|
|
|
return parseInt(sub.substr(4), 10);
|
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
// In older backend versions identity was stored in jti claim. Some users still have such tokens
|
|
|
|
if (jti) {
|
|
|
|
return jti;
|
|
|
|
}
|
2019-01-28 00:42:58 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
throw new Error('payloads is not contains any identity claim');
|
2016-10-30 17:42:49 +05:30
|
|
|
}
|
|
|
|
|
2018-02-13 02:24:31 +05:30
|
|
|
/**
|
|
|
|
* Checks the current user's token exp time. Supposed to be used before performing
|
|
|
|
* any api request
|
|
|
|
*
|
|
|
|
* @see components/user/middlewares/refreshTokenMiddleware
|
|
|
|
*
|
2019-11-27 14:33:32 +05:30
|
|
|
* @returns {Function}
|
2018-02-13 02:24:31 +05:30
|
|
|
*/
|
2020-07-22 15:31:12 +05:30
|
|
|
export function ensureToken(): AppAction<Promise<void>> {
|
2020-05-24 04:38:24 +05:30
|
|
|
return (dispatch, getState) => {
|
|
|
|
const { token } = getActiveAccount(getState()) || {};
|
2017-12-31 00:34:31 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
try {
|
|
|
|
const SAFETY_FACTOR = 300; // ask new token earlier to overcome time desynchronization problem
|
|
|
|
const { exp } = getJwtPayloads(token as any);
|
2017-12-31 00:34:31 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
if (exp - SAFETY_FACTOR < Date.now() / 1000) {
|
|
|
|
return dispatch(requestNewToken());
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
logger.warn('Refresh token error: bad token', {
|
|
|
|
token,
|
|
|
|
});
|
2017-12-31 00:34:31 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
dispatch(relogin());
|
2017-12-31 00:34:31 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return Promise.reject(new Error('Invalid token'));
|
|
|
|
}
|
2017-12-31 00:34:31 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return Promise.resolve();
|
|
|
|
};
|
2017-12-31 00:34:31 +05:30
|
|
|
}
|
|
|
|
|
2018-02-13 02:24:31 +05:30
|
|
|
/**
|
|
|
|
* Checks whether request `error` is an auth error and tries recover from it by
|
|
|
|
* requesting a new auth token
|
|
|
|
*
|
|
|
|
* @see components/user/middlewares/refreshTokenMiddleware
|
|
|
|
*
|
|
|
|
* @param {object} error
|
|
|
|
*
|
2019-11-27 14:33:32 +05:30
|
|
|
* @returns {Function}
|
2018-02-13 02:24:31 +05:30
|
|
|
*/
|
2019-11-27 14:33:32 +05:30
|
|
|
export function recoverFromTokenError(
|
2020-05-24 04:38:24 +05:30
|
|
|
error: {
|
|
|
|
status: number;
|
|
|
|
message: string;
|
|
|
|
} | void,
|
2020-07-22 15:31:12 +05:30
|
|
|
): AppAction<Promise<void>> {
|
2020-05-24 04:38:24 +05:30
|
|
|
return (dispatch, getState) => {
|
|
|
|
if (error && error.status === 401) {
|
|
|
|
const activeAccount = getActiveAccount(getState());
|
|
|
|
|
|
|
|
if (activeAccount && activeAccount.refreshToken) {
|
|
|
|
if (
|
|
|
|
['Token expired', 'Incorrect token', 'You are requesting with an invalid credential.'].includes(
|
|
|
|
error.message,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
// request token and retry
|
|
|
|
return dispatch(requestNewToken());
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.error('Unknown unauthorized response', {
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// user's access token is outdated and we have no refreshToken
|
|
|
|
// or something unexpected happend
|
|
|
|
// in both cases we resetting all the user's state
|
|
|
|
dispatch(relogin());
|
2017-12-31 00:34:31 +05:30
|
|
|
}
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return Promise.reject(error);
|
|
|
|
};
|
2017-12-31 00:34:31 +05:30
|
|
|
}
|
|
|
|
|
2018-02-13 02:24:31 +05:30
|
|
|
/**
|
|
|
|
* Requests new token and updates state. In case, when token can not be updated,
|
|
|
|
* it will redirect user to login page
|
|
|
|
*
|
2019-11-27 14:33:32 +05:30
|
|
|
* @returns {Function}
|
2018-02-13 02:24:31 +05:30
|
|
|
*/
|
2020-07-22 15:31:12 +05:30
|
|
|
export function requestNewToken(): AppAction<Promise<void>> {
|
2020-05-24 04:38:24 +05:30
|
|
|
return (dispatch, getState) => {
|
|
|
|
const { refreshToken } = getActiveAccount(getState()) || {};
|
2017-12-31 00:34:31 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
if (!refreshToken) {
|
|
|
|
dispatch(relogin());
|
2017-12-31 00:34:31 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return requestToken(refreshToken)
|
|
|
|
.then((token) => {
|
|
|
|
dispatch(updateToken(token));
|
|
|
|
})
|
|
|
|
.catch((resp) => {
|
|
|
|
// all the logic to get the valid token was failed,
|
|
|
|
// looks like we have some problems with token
|
|
|
|
// lets redirect to login page
|
|
|
|
dispatch(relogin());
|
|
|
|
|
|
|
|
return Promise.reject(resp);
|
|
|
|
});
|
|
|
|
};
|
2017-12-31 00:34:31 +05:30
|
|
|
}
|
|
|
|
|
2016-10-30 17:42:49 +05:30
|
|
|
/**
|
2017-01-27 11:59:20 +05:30
|
|
|
* Remove one account from current user's account list
|
|
|
|
*
|
2016-10-30 17:42:49 +05:30
|
|
|
* @param {Account} account
|
2016-11-08 12:00:53 +05:30
|
|
|
*
|
2019-11-27 14:33:32 +05:30
|
|
|
* @returns {Function}
|
2016-10-30 17:42:49 +05:30
|
|
|
*/
|
2020-07-22 15:31:12 +05:30
|
|
|
export function revoke(account: Account): AppAction<Promise<void>> {
|
2020-05-24 04:38:24 +05:30
|
|
|
return async (dispatch, getState) => {
|
|
|
|
const accountToReplace: Account | null =
|
|
|
|
getState().accounts.available.find(({ id }) => id !== account.id) || null;
|
|
|
|
|
|
|
|
if (accountToReplace) {
|
|
|
|
await dispatch(authenticate(accountToReplace))
|
|
|
|
.finally(() => {
|
|
|
|
// we need to logout user, even in case, when we can
|
|
|
|
// not authenticate him with new account
|
|
|
|
logout(account.token).catch(() => {
|
|
|
|
// we don't care
|
|
|
|
});
|
|
|
|
dispatch(remove(account));
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
// we don't care
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2016-11-08 12:00:53 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return dispatch(logoutAll());
|
|
|
|
};
|
2016-10-30 17:42:49 +05:30
|
|
|
}
|
|
|
|
|
2020-07-22 15:31:12 +05:30
|
|
|
export function relogin(email?: string): AppAction<Promise<void>> {
|
2020-05-24 04:38:24 +05:30
|
|
|
return async (dispatch, getState) => {
|
|
|
|
const activeAccount = getActiveAccount(getState());
|
2017-12-31 00:34:31 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
if (!email && activeAccount) {
|
|
|
|
email = activeAccount.email;
|
|
|
|
}
|
2017-12-31 00:34:31 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
dispatch(navigateToLogin(email || null));
|
|
|
|
};
|
2017-12-31 00:34:31 +05:30
|
|
|
}
|
|
|
|
|
2020-07-22 15:31:12 +05:30
|
|
|
export function logoutAll(): AppAction<Promise<void>> {
|
2020-05-24 04:38:24 +05:30
|
|
|
return (dispatch, getState) => {
|
|
|
|
dispatch(setGuest());
|
2017-01-04 11:22:46 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
const {
|
|
|
|
accounts: { available },
|
|
|
|
} = getState();
|
2016-12-06 00:44:38 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
available.forEach((account) =>
|
|
|
|
logout(account.token).catch(() => {
|
|
|
|
// we don't care
|
|
|
|
}),
|
|
|
|
);
|
2016-12-06 00:44:38 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
dispatch(reset());
|
|
|
|
dispatch(relogin());
|
2017-01-04 11:22:46 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return Promise.resolve();
|
|
|
|
};
|
2016-12-06 00:44:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logouts accounts, that was marked as "do not remember me"
|
|
|
|
*
|
|
|
|
* We detecting foreign accounts by the absence of refreshToken. The account
|
|
|
|
* won't be removed, until key `stranger${account.id}` is present in sessionStorage
|
|
|
|
*
|
2019-11-27 14:33:32 +05:30
|
|
|
* @returns {Function}
|
2016-12-06 00:44:38 +05:30
|
|
|
*/
|
2020-07-22 15:31:12 +05:30
|
|
|
export function logoutStrangers(): AppAction<Promise<void>> {
|
2020-05-24 04:38:24 +05:30
|
|
|
return async (dispatch, getState) => {
|
|
|
|
const {
|
|
|
|
accounts: { available },
|
|
|
|
} = getState();
|
|
|
|
const activeAccount = getActiveAccount(getState());
|
2019-12-07 16:58:52 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
const isStranger = ({ refreshToken, id }: Account) => !refreshToken && !sessionStorage.getItem(`stranger${id}`);
|
|
|
|
|
|
|
|
if (available.some(isStranger)) {
|
|
|
|
const accountToReplace = available.find((account) => !isStranger(account));
|
|
|
|
|
|
|
|
if (accountToReplace) {
|
|
|
|
available.filter(isStranger).forEach((account) => {
|
|
|
|
dispatch(remove(account));
|
|
|
|
logout(account.token);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (activeAccount && isStranger(activeAccount)) {
|
|
|
|
await dispatch(authenticate(accountToReplace));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
await dispatch(logoutAll());
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-12-06 00:44:38 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return Promise.resolve();
|
|
|
|
};
|
2016-12-06 00:44:38 +05:30
|
|
|
}
|