-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
161 lines (140 loc) · 6.22 KB
/
Copy pathindex.js
File metadata and controls
161 lines (140 loc) · 6.22 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
'use strict';
/**
* UW Shibboleth Authentication Module for sails-auth
*
* This module exposes a sails-auth compatible passport Strategy object that is pre-configured to work with the UW's Shibboleth
* Identity Provider (IdP). To use this, you must register your server with the UW IdP.
*
* This modules is based on passport-uwshib by Dave Stearns. https://github.com/drstearns/passport-uwshib
*
* For details about sails-uwshib, see https://github.com/kevintechie/sails-uwshib
*
* @module sails-uwshib
* @author Kevin Coleman
*/
var saml = require('passport-saml');
var util = require('util');
var uwIdPCert = 'MIID/TCCAuWgAwIBAgIJAMoYJbDt9lKKMA0GCSqGSIb3DQEBBQUAMFwxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJXQTEhMB8GA1UEChMYVW5pdmVyc2l0eSBvZiBXYXNoaW5ndG9uMR0wGwYDVQQDExRpZHAudS53YXNoaW5ndG9uLmVkdTAeFw0xMTA0MjYxOTEwMzlaFw0yMTA0MjMxOTEwMzlaMFwxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJXQTEhMB8GA1UEChMYVW5pdmVyc2l0eSBvZiBXYXNoaW5ndG9uMR0wGwYDVQQDExRpZHAudS53YXNoaW5ndG9uLmVkdTCCASIwDQYJKoZIh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ml';
// map of possible profile attributes and what name
// map of possible profile attributes and what name
// we should give them on the resulting user object
// add to this with other attrs if you request them
var profileAttrs = {
'urn:oid:0.9.2342.19200300.100.1.1': 'netId',
'urn:oid:2.16.840.1.113730.3.1.241': 'displayName',
'urn:oid:1.3.6.1.4.1.5923.1.1.1.1': 'affiliation',
'urn:oid:2.5.4.3': 'cn',
'urn:oid:0.9.2342.19200300.100.1.3': 'email',
'urn:oid:2.16.840.1.113730.3.1.3': 'empNum',
'urn:oid:1.3.6.1.4.1.5923.1.1.1.6': 'principalName',
'urn:oid:2.5.4.42': 'givenName',
'urn:oid:2.5.4.18': 'box',
'urn:oid:2.5.4.20': 'phone',
'urn:oid:2.5.4.4': 'surname',
'urn:oid:2.5.4.12': 'title',
'urn:oid:1.2.840.113994.200.21': 'studentId',
'urn:oid:1.2.840.113994.200.24': 'regId'
};
function convertProfileToUser(profile) {
var user = {};
var niceName;
var idx;
var keys = Object.keys(profile);
var key;
for (idx = 0; idx < keys.length; ++idx) {
key = keys[ idx ];
niceName = profileAttrs[ key ];
if (niceName) {
user[ niceName ] = profile[ key ];
}
}
user.provider = strategyName;
user.id = user.netId;
user.emails = [
{
value: user.principalName
}
];
return user;
}
/**
* Passport Strategy for UW Shibboleth Authentication
*
* This class extends passport-saml.Strategy, providing the necessary options for the UW Shibboleth IdP
* and converting the returned profile into a user object with sensible property names.
*
* @param {Object} options - Configuration options
* @param {string} options.entityId - Your server's entity id (often same as domain name)
* @param {string} options.domain - Your server's domain name
* @param {string} options.callbackUrl - Relative URL for the login callback (we will add https:// and domain)
* @param {string} options.privateKey - Optional private key for signing SAML requests
* @param verifyUser - The callback used to verify that the user is registered in your application database
* @constructor
*/
module.exports.Strategy = function (options, verifyUser) {
options = options || {};
options.entryPoint = options.entryPoint || uwIdPEntryPoint;
options.cert = options.cert || uwIdPCert;
options.identifierFormat = null;
options.issuer = options.issuer || options.entityId || options.domain;
options.callbackUrl = 'https://' + options.domain + options.callbackUrl;
options.decryptionPvk = options.privateKey;
options.privateCert = options.privateKey;
saml.Strategy.call(this, options, verifyUser);
this.name = strategyName;
};
util.inherits(module.exports.Strategy, saml.Strategy);
module.exports.Strategy.prototype.authenticate = function (req, options) {
var self = this;
options.samlFallback = options.samlFallback || 'login-request';
function validateCallback(err, profile, loggedOut) {
if (err) {
return self.error(err);
}
if (loggedOut) {
req.logout();
if (profile) {
req.samlLogoutRequest = profile;
return self._saml.getLogoutResponseUrl(req, redirectIfSuccess);
}
return self.pass();
}
var verified = function (err, user, info) {
if (err) {
return self.error(err);
}
if (!user) {
return self.fail(info);
}
self.success(user, info);
};
if (self._passReqToCallback) {
self._verify(req, convertProfileToUser(profile), verified);
} else {
self._verify(convertProfileToUser(profile), verified);
}
}
function redirectIfSuccess(err, url) {
if (err) {
self.error(err);
} else {
self.redirect(url);
}
}
if (req.body && req.body.SAMLResponse) {
this._saml.validatePostResponse(req.body, validateCallback);
} else if (req.body && req.body.SAMLRequest) {
this._saml.validatePostRequest(req.body, validateCallback);
} else {
var operation = {
'login-request': 'getAuthorizeUrl',
'logout-request': 'getLogoutUrl'
}[ options.samlFallback ];
if (!operation) {
return self.fail();
}
this._saml[ operation ](req, redirectIfSuccess);
}
};