2022-05-12 08:45:40 -07:00
|
|
|
import { Map as ImmutableMap, List as ImmutableList, Record as ImmutableRecord, fromJS } from 'immutable';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2022-01-07 12:26:19 -08:00
|
|
|
import {
|
|
|
|
MFA_FETCH_SUCCESS,
|
|
|
|
MFA_CONFIRM_SUCCESS,
|
|
|
|
MFA_DISABLE_SUCCESS,
|
|
|
|
} from '../actions/mfa';
|
2022-01-10 14:17:52 -08:00
|
|
|
import {
|
|
|
|
FETCH_TOKENS_SUCCESS,
|
|
|
|
REVOKE_TOKEN_SUCCESS,
|
|
|
|
} from '../actions/security';
|
2021-03-23 19:15:47 -07:00
|
|
|
|
2022-06-07 09:25:53 -07:00
|
|
|
import type { AnyAction } from 'redux';
|
|
|
|
|
2022-05-12 08:45:40 -07:00
|
|
|
const TokenRecord = ImmutableRecord({
|
|
|
|
id: 0,
|
|
|
|
app_name: '',
|
|
|
|
valid_until: '',
|
|
|
|
});
|
|
|
|
|
|
|
|
const ReducerRecord = ImmutableRecord({
|
|
|
|
tokens: ImmutableList<Token>(),
|
2022-01-07 12:26:19 -08:00
|
|
|
mfa: ImmutableMap({
|
|
|
|
settings: ImmutableMap({
|
|
|
|
totp: false,
|
|
|
|
}),
|
|
|
|
}),
|
2021-03-23 19:15:47 -07:00
|
|
|
});
|
|
|
|
|
2022-05-12 08:45:40 -07:00
|
|
|
type State = ReturnType<typeof ReducerRecord>;
|
|
|
|
|
|
|
|
export type Token = ReturnType<typeof TokenRecord>;
|
|
|
|
|
|
|
|
const deleteToken = (state: State, tokenId: number) => {
|
2021-08-03 12:29:36 -07:00
|
|
|
return state.update('tokens', tokens => {
|
2022-05-12 08:45:40 -07:00
|
|
|
return tokens.filterNot(token => token.id === tokenId);
|
2021-08-03 12:29:36 -07:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-05-12 08:45:40 -07:00
|
|
|
const importMfa = (state: State, data: any) => {
|
2022-01-07 12:26:19 -08:00
|
|
|
return state.set('mfa', data);
|
|
|
|
};
|
|
|
|
|
2022-05-12 08:45:40 -07:00
|
|
|
const enableMfa = (state: State, method: string) => {
|
2022-01-07 12:26:19 -08:00
|
|
|
return state.setIn(['mfa', 'settings', method], true);
|
|
|
|
};
|
|
|
|
|
2022-05-12 08:45:40 -07:00
|
|
|
const disableMfa = (state: State, method: string) => {
|
2022-01-07 12:26:19 -08:00
|
|
|
return state.setIn(['mfa', 'settings', method], false);
|
|
|
|
};
|
|
|
|
|
2022-05-12 08:45:40 -07:00
|
|
|
export default function security(state = ReducerRecord(), action: AnyAction) {
|
2022-05-11 10:40:34 -07:00
|
|
|
switch (action.type) {
|
2022-05-11 14:06:35 -07:00
|
|
|
case FETCH_TOKENS_SUCCESS:
|
2022-05-12 08:45:40 -07:00
|
|
|
return state.set('tokens', ImmutableList(action.tokens.map(TokenRecord)));
|
2022-05-11 14:06:35 -07:00
|
|
|
case REVOKE_TOKEN_SUCCESS:
|
|
|
|
return deleteToken(state, action.id);
|
|
|
|
case MFA_FETCH_SUCCESS:
|
|
|
|
return importMfa(state, fromJS(action.data));
|
|
|
|
case MFA_CONFIRM_SUCCESS:
|
|
|
|
return enableMfa(state, action.method);
|
|
|
|
case MFA_DISABLE_SUCCESS:
|
|
|
|
return disableMfa(state, action.method);
|
|
|
|
default:
|
|
|
|
return state;
|
2021-03-23 19:15:47 -07:00
|
|
|
}
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|