-
Notifications
You must be signed in to change notification settings - Fork 1
/
oauth-cors-sso.js
162 lines (145 loc) · 3.89 KB
/
oauth-cors-sso.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
;(function (factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('jquery'));
} else {
factory(this.jQuery || this.Zepto);
}
})(function ($) {
var window = this;
window.OAuthSSO = OAuthSSO;
function OAuthSSO(options) {
this.options = options;
}
OAuthSSO.prototype.sign = function (baseString, callback) {
var data, signer;
signer = this.options.signer;
data = {baseString: baseString};
if (typeof signer.csrf_param === "object") {
data[signer.csrf_param.name] = signer.csrf_param.value;
}
// See http://api.jquery.com/jquery.ajax/#jQuery-ajax-settings
$.ajax(signer.path, {
method: "POST",
data: data,
dataType: "json",
statusCode: {
403: function (jqXHR, textStatus, errorThrown) {
callback("forbidden", {});
},
200: function (data, textStatus, jqXHR) {
callback(void(0), data);
}
}
});
};
OAuthSSO.prototype.headerParams = function () {
return {
oauth_consumer_key: this.options.sso.consumer_key,
oauth_nonce: oauth_nonce(),
oauth_timestamp: oauth_timestamp(),
oauth_signature_method: "RSA-SHA1",
oauth_version: "1.0"
};
};
OAuthSSO.prototype.baseString = function (params) {
return [
"POST",
baseStringUrl(this.options.sso.service_url),
baseStringParams(params || this.headerParams())
].join("&");
};
OAuthSSO.prototype.buildOAuthHeader = function (oauthParams) {
return "OAuth " + dumpOAuthHeader(oauthParams);
};
OAuthSSO.prototype.auth = function (callback) {
var oauth, oauthParams;
oauth = this;
oauthParams = oauth.headerParams();
waterfall([
function (callback) {
oauth.sign(oauth.baseString(oauthParams), callback);
},
function (data, callback) {
oauthParams.oauth_signature = data.signature;
$.ajax(oauth.options.sso.service_url, {
method: "POST",
data: JSON.stringify({userData: data.userData}),
dataType: "json",
contentType: "application/json; charset=UTF-8",
crossDomain: true,
jsonp: false,
xhrFields: {
withCredentials: true
},
beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", oauth.buildOAuthHeader(oauthParams));
},
statusCode: {
200: function (data, textStatus, jqXHR) {
callback(null, data.location_href);
}
}
});
}
], function (error, href) {
if (!error) {
callback(href);
}
});
};
// Utils
function waterfall(tasks, callback) {
var iterator = tasks.slice(0);
function next(err) {
if (err || !iterator.length) {
callback.apply(null, arguments);
} else {
var args = iterator.slice.call(arguments, 1);
args.push(next);
iterator.shift().apply(null, args);
}
}
next();
}
// See: http://oauth.net/core/1.0/#encoding_parameters
function rfc3986_oauth_encode(content) {
var escaped_chars;
escaped_chars = /[^0-9a-zA-Z%\-\._~]/g;
return encodeURIComponent(content).replace(escaped_chars, escape).replace(/\*/g, '%2A');
}
// OAuth helpers
function oauth_nonce(length) {
var pLen, possible, text;
text = "";
possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
pLen = possible.length;
if (!length) {
length = Math.floor(Math.random() * 8) + 8;
}
for (var i = 0; i < length; i++) {
text += possible.charAt(Math.floor(Math.random() * pLen));
}
return text;
}
function oauth_timestamp() {
return (new Date().getTime() / 1000) | 0;
}
function baseStringUrl(url) {
return rfc3986_oauth_encode(url.toLowerCase());
}
function baseStringParams(params) {
return rfc3986_oauth_encode($.param(params));
}
function dumpOAuthHeader(oauthParams) {
var key, params;
params = [];
for (key in oauthParams) {
if (oauthParams.hasOwnProperty(key)) {
params.push(key + '="' + rfc3986_oauth_encode(oauthParams[key]) + '"');
}
}
return params.join(",");
}
});