2019-12-08 00:32:00 +05:30
|
|
|
import { browserHistory } from 'app/services/history';
|
|
|
|
import logger from 'app/services/logger';
|
|
|
|
import localStorage from 'app/services/localStorage';
|
|
|
|
import { RootState } from 'app/reducers';
|
2016-12-07 02:38:51 +05:30
|
|
|
|
2016-03-02 02:06:14 +05:30
|
|
|
import RegisterState from './RegisterState';
|
|
|
|
import LoginState from './LoginState';
|
|
|
|
import OAuthState from './OAuthState';
|
|
|
|
import ForgotPasswordState from './ForgotPasswordState';
|
2016-05-15 02:23:58 +05:30
|
|
|
import RecoverPasswordState from './RecoverPasswordState';
|
2016-06-05 17:36:14 +05:30
|
|
|
import ActivationState from './ActivationState';
|
2016-08-27 15:49:02 +05:30
|
|
|
import CompleteState from './CompleteState';
|
2018-02-18 01:29:35 +05:30
|
|
|
import ChooseAccountState from './ChooseAccountState';
|
2016-05-23 00:28:43 +05:30
|
|
|
import ResendActivationState from './ResendActivationState';
|
2019-12-07 16:58:52 +05:30
|
|
|
import AbstractState from './AbstractState';
|
2017-06-08 01:52:51 +05:30
|
|
|
|
2017-08-23 00:09:08 +05:30
|
|
|
type Request = {
|
2019-12-07 16:58:52 +05:30
|
|
|
path: string;
|
|
|
|
query: URLSearchParams;
|
|
|
|
params: { [key: string]: any };
|
2017-06-08 01:52:51 +05:30
|
|
|
};
|
2017-09-09 20:34:26 +05:30
|
|
|
|
|
|
|
// TODO: temporary added to improve typing without major refactoring
|
|
|
|
type ActionId =
|
2019-11-27 14:33:32 +05:30
|
|
|
| 'updateUser'
|
|
|
|
| 'authenticate'
|
|
|
|
| 'activateAccount'
|
|
|
|
| 'removeAccount'
|
|
|
|
| 'logout'
|
|
|
|
| 'goBack'
|
|
|
|
| 'redirect'
|
|
|
|
| 'login'
|
|
|
|
| 'acceptRules'
|
|
|
|
| 'forgotPassword'
|
|
|
|
| 'recoverPassword'
|
|
|
|
| 'register'
|
|
|
|
| 'activate'
|
|
|
|
| 'resendActivation'
|
|
|
|
| 'contactUs'
|
|
|
|
| 'setLogin'
|
|
|
|
| 'setAccountSwitcher'
|
|
|
|
| 'setErrors'
|
|
|
|
| 'clearErrors'
|
|
|
|
| 'oAuthValidate'
|
|
|
|
| 'oAuthComplete'
|
|
|
|
| 'setClient'
|
|
|
|
| 'resetOAuth'
|
|
|
|
| 'resetAuth'
|
|
|
|
| 'setOAuthRequest'
|
|
|
|
| 'setOAuthCode'
|
|
|
|
| 'requirePermissionsAccept'
|
|
|
|
| 'setScopes'
|
|
|
|
| 'setLoadingState';
|
2017-09-09 20:34:26 +05:30
|
|
|
|
2017-08-23 00:09:08 +05:30
|
|
|
export interface AuthContext {
|
2019-12-07 16:58:52 +05:30
|
|
|
run(actionId: ActionId, payload?: any): Promise<any>;
|
|
|
|
setState(newState: AbstractState): Promise<void> | void;
|
|
|
|
getState(): RootState;
|
|
|
|
navigate(route: string, options?: { replace?: boolean }): void;
|
2019-11-27 14:33:32 +05:30
|
|
|
getRequest(): Request;
|
|
|
|
prevState: AbstractState;
|
2017-08-23 00:09:08 +05:30
|
|
|
}
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
export type ActionsDict = {
|
|
|
|
[key: string]: (action: any) => { [key: string]: any };
|
|
|
|
};
|
|
|
|
|
2017-08-23 00:09:08 +05:30
|
|
|
export default class AuthFlow implements AuthContext {
|
2019-12-07 16:58:52 +05:30
|
|
|
actions: ActionsDict;
|
2019-11-27 14:33:32 +05:30
|
|
|
state: AbstractState;
|
|
|
|
prevState: AbstractState;
|
|
|
|
/**
|
|
|
|
* A callback from router, that allows to replace (perform redirect) route
|
|
|
|
* during route transition
|
|
|
|
*/
|
2019-12-07 16:58:52 +05:30
|
|
|
replace: ((path: string) => void) | null;
|
|
|
|
onReady: () => void;
|
|
|
|
navigate: (route: string, options: { replace?: boolean }) => void;
|
2019-11-27 14:33:32 +05:30
|
|
|
currentRequest: Request;
|
2019-12-07 16:58:52 +05:30
|
|
|
dispatch: (action: { [key: string]: any }) => void;
|
|
|
|
getState: () => RootState;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
constructor(actions: ActionsDict) {
|
2019-11-27 14:33:32 +05:30
|
|
|
if (typeof actions !== 'object') {
|
|
|
|
throw new Error('AuthFlow requires an actions object');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.actions = actions;
|
|
|
|
|
|
|
|
if (Object.freeze) {
|
|
|
|
Object.freeze(this.actions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
setStore(store: {
|
|
|
|
getState: () => { [key: string]: any };
|
|
|
|
dispatch: (
|
|
|
|
action: { [key: string]: any } | ((...args: any[]) => any),
|
|
|
|
) => void;
|
|
|
|
}) {
|
2017-08-23 00:09:08 +05:30
|
|
|
/**
|
2019-11-27 14:33:32 +05:30
|
|
|
* @param {string} route
|
|
|
|
* @param {object} options
|
|
|
|
* @param {object} options.replace
|
2017-08-23 00:09:08 +05:30
|
|
|
*/
|
2019-11-27 14:33:32 +05:30
|
|
|
this.navigate = (route: string, options: { replace?: boolean } = {}) => {
|
|
|
|
const { path: currentPath } = this.getRequest();
|
|
|
|
|
|
|
|
if (currentPath !== route) {
|
|
|
|
if (
|
|
|
|
currentPath.startsWith('/oauth2/v1') &&
|
|
|
|
options.replace === undefined
|
|
|
|
) {
|
|
|
|
options.replace = true;
|
2016-04-12 09:19:58 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
this.currentRequest = {
|
|
|
|
path: route,
|
|
|
|
params: {},
|
|
|
|
query: new URLSearchParams(),
|
|
|
|
};
|
2016-04-12 09:19:58 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (this.replace) {
|
|
|
|
this.replace(route);
|
2016-04-12 09:19:58 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
browserHistory[options.replace ? 'replace' : 'push'](route);
|
|
|
|
}
|
2016-03-02 02:06:14 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
this.replace = null;
|
|
|
|
};
|
2016-03-02 02:06:14 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
this.getState = store.getState.bind(store);
|
|
|
|
this.dispatch = store.dispatch.bind(store);
|
|
|
|
}
|
2016-03-02 02:06:14 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
resolve(payload: { [key: string]: any } = {}) {
|
2019-11-27 14:33:32 +05:30
|
|
|
this.state.resolve(this, payload);
|
|
|
|
}
|
2016-03-02 02:06:14 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
reject(payload: { [key: string]: any } = {}) {
|
2019-11-27 14:33:32 +05:30
|
|
|
this.state.reject(this, payload);
|
|
|
|
}
|
2016-03-02 02:06:14 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
goBack() {
|
|
|
|
this.state.goBack(this);
|
|
|
|
}
|
2017-12-31 00:34:31 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
run(actionId: ActionId, payload?: { [key: string]: any }): Promise<any> {
|
2019-11-27 14:33:32 +05:30
|
|
|
const action = this.actions[actionId];
|
2016-03-02 02:06:14 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (!action) {
|
|
|
|
throw new Error(`Action ${actionId} does not exists`);
|
2016-03-02 02:06:14 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return Promise.resolve(this.dispatch(action(payload)));
|
|
|
|
}
|
2016-03-02 02:06:14 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
setState(state: AbstractState) {
|
|
|
|
if (!state) {
|
|
|
|
throw new Error('State is required');
|
2016-03-02 02:06:14 +05:30
|
|
|
}
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
this.state && this.state.leave(this);
|
|
|
|
this.prevState = this.state;
|
|
|
|
this.state = state;
|
|
|
|
const resp = this.state.enter(this);
|
|
|
|
|
|
|
|
if (resp && resp.then) {
|
|
|
|
// this is a state with an async enter phase
|
|
|
|
// block route components from mounting, till promise will be resolved
|
|
|
|
if (this.onReady) {
|
|
|
|
const callback = this.onReady;
|
|
|
|
this.onReady = () => {};
|
|
|
|
|
|
|
|
return resp.then(
|
|
|
|
callback,
|
|
|
|
err => err || logger.warn('State transition error', err),
|
|
|
|
);
|
|
|
|
}
|
2016-08-07 19:24:59 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {object} - current request object
|
|
|
|
*/
|
|
|
|
getRequest() {
|
|
|
|
return {
|
|
|
|
path: '',
|
|
|
|
query: new URLSearchParams(),
|
|
|
|
params: {},
|
|
|
|
...this.currentRequest,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This should be called from onEnter prop of react-router Route component
|
|
|
|
*
|
|
|
|
* @param {object} request
|
|
|
|
* @param {string} request.path
|
|
|
|
* @param {object} request.params
|
|
|
|
* @param {URLSearchParams} request.query
|
|
|
|
* @param {Function} replace
|
2019-12-07 16:58:52 +05:30
|
|
|
* @param {Function} [callback=function() {}] - an optional callback function to be called, when state will be stabilized
|
2019-11-27 14:33:32 +05:30
|
|
|
* (state's enter function's promise resolved)
|
|
|
|
*/
|
|
|
|
handleRequest(
|
|
|
|
request: Request,
|
2019-12-07 16:58:52 +05:30
|
|
|
replace: (path: string) => void,
|
|
|
|
callback: () => void = () => {},
|
2019-11-27 14:33:32 +05:30
|
|
|
) {
|
|
|
|
const { path } = request;
|
|
|
|
this.replace = replace;
|
|
|
|
this.onReady = callback;
|
|
|
|
|
|
|
|
if (!path) {
|
|
|
|
throw new Error('The request.path is required');
|
|
|
|
}
|
2016-06-15 11:31:41 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (this.getRequest().path === path) {
|
|
|
|
// we are already handling this path
|
|
|
|
this.onReady();
|
2016-06-15 11:31:41 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return;
|
|
|
|
}
|
2016-08-27 15:49:02 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
this.currentRequest = request;
|
2016-06-02 23:16:49 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (this.restoreOAuthState()) {
|
|
|
|
return;
|
2016-03-02 02:06:14 +05:30
|
|
|
}
|
2016-08-12 00:50:14 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
switch (path) {
|
|
|
|
case '/register':
|
|
|
|
this.setState(new RegisterState());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '/forgot-password':
|
|
|
|
this.setState(new ForgotPasswordState());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '/resend-activation':
|
|
|
|
this.setState(new ResendActivationState());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '/choose-account':
|
|
|
|
this.setState(new ChooseAccountState());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '/':
|
|
|
|
case '/login':
|
|
|
|
case '/password':
|
|
|
|
case '/mfa':
|
|
|
|
case '/accept-rules':
|
|
|
|
case '/oauth/permissions':
|
|
|
|
case '/oauth/finish':
|
|
|
|
case '/oauth/choose-account':
|
|
|
|
this.setState(new LoginState());
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
switch (
|
|
|
|
path.replace(/(.)\/.+/, '$1') // use only first part of an url
|
|
|
|
) {
|
|
|
|
case '/oauth2':
|
|
|
|
this.setState(new OAuthState());
|
|
|
|
break;
|
|
|
|
case '/activation':
|
|
|
|
this.setState(new ActivationState());
|
|
|
|
break;
|
|
|
|
case '/recover-password':
|
|
|
|
this.setState(new RecoverPasswordState());
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
replace('/404');
|
|
|
|
break;
|
2016-10-25 11:31:51 +05:30
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
2016-10-25 05:10:05 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
this.onReady();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tries to restore last oauth request, if it was stored in localStorage
|
|
|
|
* in last 2 hours
|
|
|
|
*
|
|
|
|
* @api private
|
|
|
|
*
|
|
|
|
* @returns {bool} - whether oauth state is being restored
|
|
|
|
*/
|
|
|
|
restoreOAuthState() {
|
|
|
|
if (/^\/(register|oauth2)/.test(this.getRequest().path)) {
|
|
|
|
// allow register or the new oauth requests
|
|
|
|
return;
|
|
|
|
}
|
2016-08-27 15:49:02 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
try {
|
|
|
|
const data = JSON.parse(localStorage.getItem('oauthData'));
|
|
|
|
const expirationTime = 2 * 60 * 60 * 1000; // 2h
|
2016-08-12 00:50:14 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (Date.now() - data.timestamp < expirationTime) {
|
|
|
|
this.run('oAuthValidate', data.payload)
|
|
|
|
.then(() => this.setState(new CompleteState()))
|
|
|
|
.then(() => this.onReady());
|
2016-08-27 15:49:02 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
/* bad luck :( */
|
2016-08-12 00:50:14 +05:30
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-02 02:06:14 +05:30
|
|
|
}
|