This repository has been archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
190 lines (173 loc) · 4.96 KB
/
index.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/***
*
* CAS client specific to BYU
*
* ____ ______
* /---- /===\ /
* || / \ (---
* || / ===== \ )
* \---- / \ ====ss
*
* ~~~terrible word art~~~
*/
var Promise = typeof Promise !== 'undefined' ? Promise : require('bluebird');
var CAS = require('xcas'),
cheerio = require('cheerio'),
request = require('request'),
https = require('https');
var byucas = {
/**
* Checks whether or not a ticket is valid.
* @param {string} ticket - The ticket to validate
* @param {string} service - Should match the URL passed as a service in the CAS
* query string, e.g., cas.byu.edu/cas/signin?service=example.com
* @return Promise rejected when the ticket is invalid, resolved otherwise.
*/
validate: function(ticket, service) {
return new Promise(function (resolve, reject) {
var cas = new CAS({
base_url: 'https://cas.byu.edu/cas/',
service: service,
version: 2.0
});
cas.validate(ticket, function(err, status, username, details) {
if (err || !status) {
reject(Error("Could not validate CAS ticket: " + err));
} else {
var attributes = _parseAttributes(details.attributes);
resolve({username: username, attributes: attributes});
}
});
});
}
};
// converts data in the attributes from all arrays to appropriate types
function _parseAttributes(attributes) {
var BOOLS = [
'activeParttimeNonBYUEmployee',
'activeParttimeInstructor',
'inactiveFulltimeEmployee',
'activeFulltimeInstructor',
'activeFulltimeNonBYUEmployee',
'inactiveParttimeNonBYUEmployee',
'organization',
'activeEligibletoRegisterStudent',
'preferredSurname',
'inactiveParttimeInstructor',
'inactiveFulltimeNonBYUEmployee',
'restricted',
'alumni',
'inactiveParttimeEmployee',
'inactiveFulltimeInstructor',
'activeFulltimeEmployee',
'activeParttimeEmployee',
];
var STRINGS = [
'restOfName',
'surname',
'preferredFirstName',
'sortName',
'name',
'netId',
'byuId',
'emailAddress',
'personId',
'fullName'
]
var SPLIT_ARRAY = ['memberOf'];
var new_attrs = {};
Object.keys(attributes).forEach(function(key) {
if(BOOLS.indexOf(key) !== -1) {
new_attrs[key] = (attributes[key][0] == 'true');
}
else if(STRINGS.indexOf(key) !== -1) {
new_attrs[key] = attributes[key][0];
}
else if(SPLIT_ARRAY.indexOf(key) !== -1) {
var val = attributes[key][0];
if(val === '') {
new_attrs[key] = [];
}
else
{
new_attrs[key] = val.split(',');
}
}
else
{
new_attrs[key] = attributes[key];
}
});
return new_attrs;
};
// helper function for getTicket
function _getCASFieldsFromHTML(html) {
var fields = {};
var $ = cheerio.load(html);
$('textarea, input').each(function(){
fields[$(this).attr('name')] = $(this).val();
});
return fields;
};
if(process.env.INSECURE) {
/**
* Submits a username and password to BYU's CAS services to retrieve a
* ticket.
* @see validate
* @return Promise fulfilled with a ticket
*/
byucas.getTicket = function (username, password, service) {
console.warn("NOTE: getTicket certain security implications when handling "
+ "user data. 'validate' should almost always be used instead.");
return new Promise(function(resolve, reject) {
var r = request.defaults({
jar: true,
followRedirect: false
});
var url = 'https://cas.byu.edu/cas/login?service=' + encodeURIComponent(service);
var req = r.get(url, function(error, response, body){
if(error) {
_handleCASError(error);
return;
}
_handleHTML(body);
});
function _handleHTML(html) {
var fields = _parseHTML(html);
var req = r.post(url, {form: fields}, function(error, response, body){
if(error) {
_handleCASError(error);
return;
}
if(response.headers.location === undefined) {
// there might be a link to the ticket instead of a redirect. try that
var $ = cheerio.load(body);
_extractTicket($('a').attr('href'));
}
else
{
_extractTicket(response.headers.location);
}
});
};
function _extractTicket(url) {
var query = require('url').parse(url, true).query;
if(!query || query.ticket === undefined) {
reject(Error("No ticket returned"));
return;
}
resolve(query.ticket);
};
function _parseHTML(html) {
var fields = _getCASFieldsFromHTML(html);
fields.username = username;
fields.password = password;
return fields;
};
function _handleCASError(e){
reject(Error(e));
};
});
};
}
module.exports = byucas;