2022-01-10 14:01:24 -08:00
|
|
|
import { Map as ImmutableMap, List as ImmutableList, 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
|
|
|
|
|
|
|
const initialState = ImmutableMap({
|
|
|
|
tokens: ImmutableList(),
|
2022-01-07 12:26:19 -08:00
|
|
|
mfa: ImmutableMap({
|
|
|
|
settings: ImmutableMap({
|
|
|
|
totp: false,
|
|
|
|
}),
|
|
|
|
}),
|
2021-03-23 19:15:47 -07:00
|
|
|
});
|
|
|
|
|
2021-08-03 12:29:36 -07:00
|
|
|
const deleteToken = (state, tokenId) => {
|
|
|
|
return state.update('tokens', tokens => {
|
|
|
|
return tokens.filterNot(token => token.get('id') === tokenId);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-01-07 12:26:19 -08:00
|
|
|
const importMfa = (state, data) => {
|
|
|
|
return state.set('mfa', data);
|
|
|
|
};
|
|
|
|
|
|
|
|
const enableMfa = (state, method) => {
|
|
|
|
return state.setIn(['mfa', 'settings', method], true);
|
|
|
|
};
|
|
|
|
|
|
|
|
const disableMfa = (state, method) => {
|
|
|
|
return state.setIn(['mfa', 'settings', method], false);
|
|
|
|
};
|
|
|
|
|
2021-03-23 19:15:47 -07:00
|
|
|
export default function security(state = initialState, action) {
|
|
|
|
switch(action.type) {
|
|
|
|
case FETCH_TOKENS_SUCCESS:
|
|
|
|
return state.set('tokens', fromJS(action.tokens));
|
|
|
|
case REVOKE_TOKEN_SUCCESS:
|
2021-08-03 12:29:36 -07:00
|
|
|
return deleteToken(state, action.id);
|
2022-01-07 12:26:19 -08:00
|
|
|
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);
|
2021-03-23 19:15:47 -07:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2021-08-03 12:22:51 -07:00
|
|
|
}
|