/
go2.js
84 lines (78 loc) · 2.24 KB
/
go2.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
// Simple Google API OAuth 2.0 Client flow lib
// Get access token and pass to callback
"use strict";
(function (w) {
var windowName = 'google_oauth2_login_popup';
if (window.name === windowName) {
if (
window.opener &&
window.opener.GO2
) {
if (window.location.hash.indexOf('access_token') !== -1) {
window.opener.GO2.receiveToken(
window.location.hash.replace(/^.*access_token=([^&]+).*$/, '$1'),
parseInt(window.location.hash.replace(/^.*expires_in=([^&]+).*$/, '$1'))
);
}
if (window.location.search.indexOf('error=')) {
window.opener.GO2.receiveToken('ERROR');
}
}
window.close();
}
var client_id,
scope = 'https://www.googleapis.com/auth/plus.me',
redirect_uri = window.location.href.substr(0, window.location.href.length - window.location.hash.length).replace(/#$/, ''),
access_token,
callbackWaitForToken;
w.GO2 = {
// init
init: function (f_client_id, f_scope, f_redirect_uri) {
if (!f_client_id) return false;
if (f_scope) scope = f_scope;
client_id = f_client_id;
},
// receive token from popup
receiveToken: function (token, expires_in) {
if (token !== 'ERROR') {
access_token = token;
if (callbackWaitForToken) callbackWaitForToken(access_token);
setTimeout(
function () {
access_token = undefined;
},
expires_in * 1000
);
} else if (token === false) {
callbackWaitForToken = undefined;
}
},
// boolean, indicate logged in or not
isLoggedIn: function () {
return !!access_token;
},
// pass the access token to callback
// if not logged in this triggers login popup;
// use isLoggedIn to check login first to prevent popup blocker
getToken: function (callback) {
if (!client_id || !redirect_uri || !scope) {
alert('You need init() first. Check the program flow.');
return false;
}
if (!access_token) {
callbackWaitForToken = callback;
window.open(
'https://accounts.google.com/o/oauth2/auth'
+ '?response_type=token'
+ '&redirect_uri=' + encodeURIComponent(redirect_uri)
+ '&scope=' + encodeURIComponent(scope)
+ '&client_id=' + encodeURIComponent(client_id),
windowName,
'width=400,height=360'
);
} else {
return callback(access_token);
}
}
};
})(this);