-
Notifications
You must be signed in to change notification settings - Fork 383
/
security.js
107 lines (103 loc) · 3.5 KB
/
security.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* Copyright 2017, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
import {
LOGIN_SUCCESS,
LOGIN_FAIL,
LOGOUT,
CHANGE_PASSWORD_SUCCESS,
CHANGE_PASSWORD_FAIL,
RESET_ERROR,
REFRESH_SUCCESS,
SESSION_VALID
} from '../actions/security';
import { SET_CONTROL_PROPERTY } from '../actions/controls';
import { USERMANAGER_UPDATE_USER } from '../actions/users';
import SecurityUtils from '../utils/SecurityUtils';
import assign from 'object-assign';
import { cloneDeep, head } from 'lodash';
function security(state = {user: null, errorCause: null}, action) {
switch (action.type) {
case USERMANAGER_UPDATE_USER:
if (state.user && action.user && state.user.id === action.user.id) {
return assign({}, state, {
user: cloneDeep(action.user)
});
}
return state;
case SET_CONTROL_PROPERTY:
if (action.control === 'ResetPassword' && action.property === 'enabled') {
return assign({}, state, {
passwordChanged: false,
passwordError: null
});
}
return state;
case LOGIN_SUCCESS:
{
const userAttributes = SecurityUtils.getUserAttributes(action.userDetails.User);
const userUuid = head(userAttributes.filter(attribute => attribute.name.toLowerCase() === 'uuid'));
const timestamp = new Date() / 1000 | 0;
return assign({}, state, {
user: action.userDetails.User,
token: (action.userDetails && action.userDetails.access_token) || (userUuid && userUuid.value),
refresh_token: (action.userDetails && action.userDetails.refresh_token),
expires: (action.userDetails && action.userDetails.expires) ? timestamp + action.userDetails.expires : timestamp + 48 * 60 * 60,
authHeader: action.authHeader,
loginError: null
});
}
case REFRESH_SUCCESS:
{
const timestamp = new Date() / 1000 | 0;
return assign({}, state, {
token: (action.userDetails && action.userDetails.access_token),
refresh_token: (action.userDetails && action.userDetails.refresh_token),
expires: (action.userDetails && action.userDetails.expires) ? timestamp + action.userDetails.expires : timestamp + 48 * 60 * 60
});
}
case LOGIN_FAIL:
return assign({}, state, {
loginError: action.error
});
case RESET_ERROR:
return assign({}, state, {
loginError: null
});
case LOGOUT:
return assign({}, state, {
user: null,
token: null,
refresh_token: null,
expires: null,
authHeader: null,
loginError: null
});
case CHANGE_PASSWORD_SUCCESS:
return assign({}, state, {
user: assign({}, state.user, assign({}, action.user, {date: new Date().getTime()})),
authHeader: action.authHeader,
passwordChanged: true,
passwordError: null
});
case CHANGE_PASSWORD_FAIL:
return assign({}, state, {
passwordError: action.error,
passwordChanged: false
});
case SESSION_VALID:
{
return assign({}, state, {
user: action.userDetails.User,
loginError: null
});
}
default:
return state;
}
}
export default security;