-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
169 lines (155 loc) · 6.43 KB
/
Copy pathindex.js
File metadata and controls
169 lines (155 loc) · 6.43 KB
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
const axios = require('axios');
const constants = require('./constants');
const { AgentlessWebssoError } = require('./model/AgentlessWebssoError');
/** @module NorthwesternSSO */
/**
* @typedef {Object} sessionInfoResponse
* @property {String} status - The response code of the request
* @property {Object} data - The response body of the request
*/
module.exports = {
/**
* Parses json object of all cookies and returns the sso cookie value
* @param {Object} requestCookies - A JSON object of all request cookies to check
* @returns {String} The SSO request cookie value
*/
getSSOCookie(requestCookies) {
return requestCookies[constants.SSO_COOKIE_NAME];
},
/**
* Calls websso url to get information about the session for a given sso token
* @async
* @param {String} tokenId - The value in the sso cookie
* @param {String} apigeeEnv - The apigee environnment to call (dev, test, prod)
* @param {String} apigeeApiKey - The application's apigee apikey
* @returns {sessionInfoResponse} Returns an object with the sessionInfo request status and request body
*/
async getSessionInfo(tokenId, apigeeEnv, apigeeApiKey) {
const url = `https://northwestern-${apigeeEnv}.apigee.net/${constants.APIGEE_PROXY_NAME}/${constants.APIGEE_SESSION_INFO_PATH}`;
const requestHeaders = {
'webssotoken': tokenId,
'apikey': apigeeApiKey,
'Content-Type': 'application/json',
};
try {
const sessionInfoResponse = await axios.get(url, { headers: requestHeaders });
return {
status: sessionInfoResponse.status,
data: sessionInfoResponse.data,
};
} catch (err) {
if (err.response && err.response.data && err.response.data.fault && err.response.data.fault.faultstring) {
// NOTE Apigee returns a 500 status when the cookie is not valid even though OpenAM returns a 401 when the cookie is not valid
if (err.response.data.fault.faultstring.includes("ResponseCode 401 is treated as error")){
// console.log('nusso node package | Apigee AgentlessWebsso returned 500 when cookie not logged in. returning 401 openAM body for use with isLoggedIn method instead of throwing 500');
return {
"code": 401,
"reason": "Unauthorized",
"message": "Access Denied"
};
}
} else if (err.response && err.response.data && err.response.status) {
throw new AgentlessWebssoError('Error getting session info for cookie', err.response.status, err.response.data);
} else {
throw new AgentlessWebssoError('Error getting session info for cookie', 500, err);
}
}
},
/**
* Checks whether the user's session is valid
* @param {sessionInfoResponse} sessionInfo - The response from the session info service (status, body)
* @returns {Boolean} True if status is 200 and a netid value is found in the sessionInfo. False if status is 401 Unauthorized, or other.
*/
isLoggedIn(sessionInfo) {
switch (sessionInfo.status) {
case 200:
if (sessionInfo.data[constants.NETID_PROPERTY_NAME]) {
return true;
}
return false;
case 401:
return false;
default:
return false;
}
},
/**
* Checks whether the user passed through DUO
* @param {sessionInfoResponse} sessionInfo - The response from the session info service (status, body)
* @returns {Boolean} True if user has a valid session (200 + netid) and passed through DUO. False if session is invalid or did not pass through DUO.
*/
isDuoAuthenticated(sessionInfo) {
switch (sessionInfo.status) {
case 200:
if (sessionInfo.data[constants.NETID_PROPERTY_NAME]) {
if (sessionInfo.data.properties[constants.DUO_PROPERTY_NAME] == 'true') {
return true;
}
return false;
}
return false;
case 401:
return false;
default:
return false;
}
},
/**
* Get the url for redirect to websso login
* @async
* @param {Boolean} isDuoRequired - Whether to generate a url for login-only or login+duo
* @param {String} redirectUrl - the url to redirect to once login is successfully completed
* @param {String} apigeeEnv - The apigee environnment to call (dev, test, prod)
* @param {String} apigeeApiKey - the application's api key
* @returns {String} A websso login url
*/
async getLoginUrl(isDuoRequired, redirectUrl, apigeeEnv, apigeeApiKey) {
const url = `https://northwestern-${apigeeEnv}.apigee.net/${constants.APIGEE_PROXY_NAME}/${isDuoRequired ? constants.APIGEE_LDAP_AND_DUO_PATH : constants.APIGEE_LDAP_ONLY_PATH}`;
const requestHeaders = {
'Content-Type': 'application/json',
'goto': redirectUrl,
'apikey': apigeeApiKey,
};
try {
const sessionInfoResponse = await axios.get(url, { headers: requestHeaders });
return sessionInfoResponse.data.redirecturl;
} catch (err) {
if (err.response && err.response.status && err.response.data) {
throw new AgentlessWebssoError('Error getting sso login url', err.response.status, err.response.data);
} else {
throw new AgentlessWebssoError('Error getting sso login url', 500, err);
}
}
},
/**
* Get the url for redirect to websso logout
* @async
* @param {String} apigeeEnv - The apigee environnment to call (dev, test, prod)
* @param {String} apigeeApiKey - the application's api key
* @returns {String} A websso logout url
*/
async getLogoutUrl(apigeeEnv, apigeeApiKey) {
const url = `https://northwestern-${apigeeEnv}.apigee.net/${constants.APIGEE_PROXY_NAME}/logout`;
const requestHeaders = {
'Content-Type': 'application/json',
};
try {
const sessionInfoResponse = await axios.get(url, { headers: requestHeaders });
return sessionInfoResponse.data.url;
} catch (err) {
if (err.response && err.response.status && err.response.data) {
throw new AgentlessWebssoError('Error getting sso logout url', err.response.status, err.response.data);
} else {
throw new AgentlessWebssoError('Error getting sso logout url', 500, err);
}
}
},
/**
* Get the user's netid
* @param {sessionInfoResponse} sessionInfo - The response from the session info service (status, body)
* @returns {String} the logged in user's netid if it exists in the session info (null otherwise)
*/
getNetID(sessionInfo) {
return sessionInfo.data[constants.NETID_PROPERTY_NAME];
},
};