forked from OneIdentity/safeguard.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SessionStorage.js
170 lines (151 loc) · 4.75 KB
/
SessionStorage.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* (Public) Default storage class for SafeguardJs. This uses sessionStorage for storing and
* retrieving the authentication information for communicating with the safeguard
* appliance.
*
* If implementing a different storage class other than sessionStorage (such as a
* database), the new class must be written with all of the same methods as those
* defined here.
*/
class SessionStorage {
/**
* (Public) The keys used for storing authentication information.
*/
SessionStorageKeys = {
ACCESSTOKEN: 'AccessToken',
USERTOKEN: 'UserToken',
HOSTNAME: 'HostName',
CODEVERIFIER: 'CodeVerifier',
RANDOMSTATE: 'RandomState',
CODE: 'Code',
STATE: 'State',
NEWLOGIN: 'NewLogin'
}
/**
* Gets the access token from storage.
* @returns {string} The access token.
*/
getAccessToken() {
return sessionStorage.getItem(this.SessionStorageKeys.ACCESSTOKEN);
}
/**
* Gets the access pkce code from storage.
* @returns {string} The pkce code.
*/
getCode() {
return sessionStorage.getItem(this.SessionStorageKeys.CODE);
}
/**
* Gets the access pkce code verifier from storage.
* @returns {string} The pkce verifier code.
*/
getCodeVerifier() {
return sessionStorage.getItem(this.SessionStorageKeys.CODEVERIFIER);
}
/**
* Gets the access random state from storage.
* @returns {string} The random state.
*/
getRandomState() {
return sessionStorage.getItem(this.SessionStorageKeys.RANDOMSTATE);
}
/**
* Gets the access pkce state from storage.
* @returns {string} The pkce state.
*/
getState() {
return sessionStorage.getItem(this.SessionStorageKeys.STATE);
}
/**
* Gets the user token from storage.
* @returns {string} The user token.
*/
getUserToken(){
return sessionStorage.getItem(this.SessionStorageKeys.USERTOKEN);
}
/**
* Gets the host name from storage.
* @returns {string} The host name.
*/
getHostName(){
return sessionStorage.getItem(this.SessionStorageKeys.HOSTNAME);
}
/**
* Gets the new login flag from storage.
* @returns {string} The new login flag.
*/
getNewLogin(){
return sessionStorage.getItem(this.SessionStorageKeys.NEWLOGIN);
}
/**
* Writes the access token to storage.
*/
setAccessToken(accessToken) {
sessionStorage.setItem(this.SessionStorageKeys.ACCESSTOKEN, accessToken);
}
/**
* Writes the access pkce code to storage.
*/
setCode(code) {
sessionStorage.setItem(this.SessionStorageKeys.CODE, code);
}
/**
* Writes the access pkce code verifier to storage.
*/
setCodeVerifier(codeVerifier) {
sessionStorage.setItem(this.SessionStorageKeys.CODEVERIFIER, codeVerifier);
}
/**
* Writes the access random state to storage.
*/
setRandomState(state) {
sessionStorage.setItem(this.SessionStorageKeys.RANDOMSTATE, state);
}
/**
* Writes the access pkce state to storage.
*/
setState(state) {
sessionStorage.setItem(this.SessionStorageKeys.STATE, state);
}
/**
* Writes the user token to storage.
*/
setUserToken(userToken) {
sessionStorage.setItem(this.SessionStorageKeys.USERTOKEN, userToken);
}
/**
* Writes the host name to storage.
*/
setHostName(hostName) {
sessionStorage.setItem(this.SessionStorageKeys.HOSTNAME, hostName);
}
/**
* Writes the new login flag to storage.
*/
setNewLogin(newLogin) {
sessionStorage.setItem(this.SessionStorageKeys.NEWLOGIN, newLogin);
}
/**
* Clears code and state from storage.
*/
clearPKCEStorage() {
sessionStorage.removeItem(this.SessionStorageKeys.CODE);
sessionStorage.removeItem(this.SessionStorageKeys.STATE);
sessionStorage.removeItem(this.SessionStorageKeys.CODEVERIFIER);
sessionStorage.removeItem(this.SessionStorageKeys.RANDOMSTATE);
}
/**
* Clears all values from storage.
*/
clearStorage() {
sessionStorage.removeItem(this.SessionStorageKeys.USERTOKEN);
sessionStorage.removeItem(this.SessionStorageKeys.ACCESSTOKEN);
sessionStorage.removeItem(this.SessionStorageKeys.HOSTNAME);
sessionStorage.removeItem(this.SessionStorageKeys.CODE);
sessionStorage.removeItem(this.SessionStorageKeys.CODEVERIFIER);
sessionStorage.removeItem(this.SessionStorageKeys.STATE);
sessionStorage.removeItem(this.SessionStorageKeys.RANDOMSTATE);
sessionStorage.removeItem(this.SessionStorageKeys.NEWLOGIN);
}
}
module.exports = { SessionStorage }