forked from OneIdentity/safeguard.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SessionStorage.js
90 lines (79 loc) · 2.21 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
/**
* (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'
}
/**
* Gets the access token from storage.
* @returns {string} The access token.
*/
getAccessToken() {
return sessionStorage.getItem(this.SessionStorageKeys.ACCESSTOKEN);
}
/**
* 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);
}
/**
* Writes the access token to storage.
*/
setAccessToken(accessToken) {
sessionStorage.setItem(this.SessionStorageKeys.ACCESSTOKEN, accessToken);
}
/**
* 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);
}
/**
* Clears all values from storage.
*/
clearStorage() {
let userToken = this.getUserToken();
let accessToken = this.getAccessToken();
let sessionHostName = this.getHostName();
if (userToken)
{
this.setUserToken('');
}
if (accessToken)
{
this.setAccessToken('');
}
if (sessionHostName)
{
this.setHostName('');
}
}
}
module.exports = { SessionStorage }