forked from oauth-io/oauth-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth.js
130 lines (112 loc) · 3.83 KB
/
oauth.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
(function() {
"use strict";
var config = {
oauthd_url: 'https://oauth.io/auth'
};
config.oauthd_base = getAbsUrl(config.oauthd_url).match(/^.{2,5}:\/\/[^/]+/)[0];
var oauth_result;
(function parse_urlfragment() {
var results = /[\\#&]oauthio=([^&]*)/.exec(document.location.hash);
if (results) {
document.location.hash = '';
oauth_result = decodeURIComponent(results[1].replace(/\+/g, " "));
}
})();
function getAbsUrl(url) {
if (url[0] === '/')
url = document.location.protocol + '//' + document.location.host + url;
else if ( ! url.match(/^.{2,5}:\/\//))
url = document.location.protocol + '//' + document.location.host + document.location.pathname + '/' + url;
return url;
}
function sendCallback(opts) {
var data;
var err;
try {
data = JSON.parse(opts.data);
} catch (e) {}
if ( ! data || ! data.provider)
return;
if (opts.provider && data.provider.toLowerCase() !== opts.provider.toLowerCase())
return;
if (data.status === 'error' || data.status === 'fail') {
err = new Error(data.message);
err.body = data.data;
return opts.callback(err);
}
if (data.status !== 'success' || ! data.data) {
err = new Error();
err.body = data.data;
return opts.callback(err);
}
if ( ! opts.provider)
data.data.provider = data.provider;
return opts.callback(null, data.data);
}
window.OAuth = {
initialize: function(public_key) {
config.key = public_key;
},
popup: function(provider, callback) {
var wnd;
if ( ! config.key)
return callback(new Error('OAuth object must be initialized'));
var url = config.oauthd_url + '/' + provider + "?k=" + config.key + '&d=' + encodeURIComponent(getAbsUrl('/'));
// create popup
var wnd_settings = {
width: Math.floor(window.outerWidth * 0.8),
height: Math.floor(window.outerHeight * 0.5)
};
if (wnd_settings.height < 350)
wnd_settings.height = 350;
if (wnd_settings.width < 800)
wnd_settings.width = 800;
wnd_settings.left = window.screenX + (window.outerWidth - wnd_settings.width) / 2;
wnd_settings.top = window.screenY + (window.outerHeight - wnd_settings.height) / 8;
var wnd_options = "width=" + wnd_settings.width + ",height=" + wnd_settings.height;
wnd_options += ",toolbar=0,scrollbars=1,status=1,resizable=1,location=1,menuBar=0";
wnd_options += ",left=" + wnd_settings.left + ",top=" + wnd_settings.top;
var opts = {provider:provider};
function getMessage(e) {
if (e.source !== wnd || e.origin !== config.oauthd_base)
return;
opts.data = e.data;
return sendCallback(opts);
}
opts.callback = function(e, r) {
if (window.removeEventListener)
window.removeEventListener("message", getMessage, false);
else if (window.detachEvent)
window.detachEvent("onmessage", getMessage);
else if (document.detachEvent)
document.detachEvent("onmessage", getMessage);
opts.callback = function() {};
return callback(e,r);
};
if (window.attachEvent)
window.attachEvent("onmessage", getMessage);
else if (document.attachEvent)
document.attachEvent("onmessage", getMessage);
else if (window.addEventListener)
window.addEventListener("message", getMessage, false);
setTimeout(function() {
opts.callback(new Error('Authorization timed out'));
}, 600 * 1000);
wnd = window.open(url, "Authorization", wnd_options);
if (wnd)
wnd.focus();
},
redirect: function(provider, url) {
url = encodeURIComponent(getAbsUrl(url));
url = config.oauthd_url + '/' + provider + "?k=" + config.key + "&redirect_uri=" + url;
document.location.href = url;
},
callback: function(provider, callback) {
if ( ! oauth_result)
return;
if (arguments.length === 1)
return sendCallback({data:oauth_result, callback:provider});
return sendCallback({data:oauth_result, provider:provider, callback:callback});
}
};
})();