-
Notifications
You must be signed in to change notification settings - Fork 0
/
identity.js
executable file
·216 lines (180 loc) · 6.66 KB
/
identity.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
'use strict';
var googlePlusUserLoader = (function() {
var STATE_START=1;
var STATE_ACQUIRING_AUTHTOKEN=2;
var STATE_AUTHTOKEN_ACQUIRED=3;
var state = STATE_START;
var signin_button, xhr_button, revoke_button, user_info_div;
function disableButton(button) {
button.setAttribute('disabled', 'disabled');
}
function enableButton(button) {
button.removeAttribute('disabled');
}
function changeState(newState) {
state = newState;
switch (state) {
case STATE_START:
enableButton(signin_button);
disableButton(xhr_button);
disableButton(revoke_button);
break;
case STATE_ACQUIRING_AUTHTOKEN:
sampleSupport.log('Acquiring token...');
disableButton(signin_button);
disableButton(xhr_button);
disableButton(revoke_button);
break;
case STATE_AUTHTOKEN_ACQUIRED:
disableButton(signin_button);
enableButton(xhr_button);
enableButton(revoke_button);
break;
}
}
// @corecode_begin getProtectedData
function xhrWithAuth(method, url, interactive, callback) {
var access_token;
var retry = true;
getToken();
function getToken() {
chrome.identity.getAuthToken({ interactive: interactive }, function(token) {
if (chrome.runtime.lastError) {
callback(chrome.runtime.lastError);
return;
}
access_token = token;
requestStart();
});
}
function requestStart() {
var xhr = new XMLHttpRequest();
xhr.open(method, url);
xhr.setRequestHeader('Authorization', 'Bearer ' + access_token);
xhr.onload = requestComplete;
xhr.send();
}
function requestComplete() {
if (this.status == 401 && retry) {
retry = false;
chrome.identity.removeCachedAuthToken({ token: access_token },
getToken);
} else {
callback(null, this.status, this.response);
}
}
}
function getUserInfo(interactive) {
xhrWithAuth('GET',
'https://www.googleapis.com/plus/v1/people/me',
interactive,
onUserInfoFetched);
}
// @corecode_end getProtectedData
// Code updating the user interface, when the user information has been
// fetched or displaying the error.
function onUserInfoFetched(error, status, response) {
if (!error && status == 200) {
changeState(STATE_AUTHTOKEN_ACQUIRED);
sampleSupport.log(response);
var user_info = JSON.parse(response);
populateUserInfo(user_info);
} else {
changeState(STATE_START);
}
}
function populateUserInfo(user_info) {
user_info_div.innerHTML = "Hello " + user_info.displayName;
fetchImageBytes(user_info);
}
function fetchImageBytes(user_info) {
if (!user_info || !user_info.image || !user_info.image.url) return;
var xhr = new XMLHttpRequest();
xhr.open('GET', user_info.image.url, true);
xhr.responseType = 'blob';
xhr.onload = onImageFetched;
xhr.send();
}
function onImageFetched(e) {
if (this.status != 200) return;
var imgElem = document.createElement('img');
var objUrl = window.webkitURL.createObjectURL(this.response);
imgElem.src = objUrl;
imgElem.onload = function() {
window.webkitURL.revokeObjectURL(objUrl);
}
user_info_div.insertAdjacentElement("afterbegin", imgElem);
}
// OnClick event handlers for the buttons.
/**
Retrieves a valid token. Since this is initiated by the user
clicking in the Sign In button, we want it to be interactive -
ie, when no token is found, the auth window is presented to the user.
Observe that the token does not need to be cached by the app.
Chrome caches tokens and takes care of renewing when it is expired.
In that sense, getAuthToken only goes to the server if there is
no cached token or if it is expired. If you want to force a new
token (for example when user changes the password on the service)
you need to call removeCachedAuthToken()
**/
function interactiveSignIn() {
changeState(STATE_ACQUIRING_AUTHTOKEN);
// @corecode_begin getAuthToken
// @description This is the normal flow for authentication/authorization
// on Google properties. You need to add the oauth2 client_id and scopes
// to the app manifest. The interactive param indicates if a new window
// will be opened when the user is not yet authenticated or not.
// @see http://developer.chrome.com/apps/app_identity.html
// @see http://developer.chrome.com/apps/identity.html#method-getAuthToken
chrome.identity.getAuthToken({ 'interactive': true }, function(token) {
if (chrome.runtime.lastError) {
sampleSupport.log(chrome.runtime.lastError);
changeState(STATE_START);
} else {
sampleSupport.log('Token acquired:'+token+
'. See chrome://identity-internals for details.');
changeState(STATE_AUTHTOKEN_ACQUIRED);
}
});
// @corecode_end getAuthToken
}
function revokeToken() {
user_info_div.innerHTML="";
chrome.identity.getAuthToken({ 'interactive': false },
function(current_token) {
if (!chrome.runtime.lastError) {
// @corecode_begin removeAndRevokeAuthToken
// @corecode_begin removeCachedAuthToken
// Remove the local cached token
chrome.identity.removeCachedAuthToken({ token: current_token },
function() {});
// @corecode_end removeCachedAuthToken
// Make a request to revoke token in the server
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://accounts.google.com/o/oauth2/revoke?token=' +
current_token);
xhr.send();
// @corecode_end removeAndRevokeAuthToken
// Update the user interface accordingly
changeState(STATE_START);
sampleSupport.log('Token revoked and removed from cache. '+
'Check chrome://identity-internals to confirm.');
}
});
}
return {
onload: function () {
signin_button = document.querySelector('#signin');
signin_button.addEventListener('click', interactiveSignIn);
xhr_button = document.querySelector('#getxhr');
xhr_button.addEventListener('click', getUserInfo.bind(xhr_button, true));
revoke_button = document.querySelector('#revoke');
revoke_button.addEventListener('click', revokeToken);
user_info_div = document.querySelector('#user_info');
// Trying to get user's info without signing in, it will work if the
// application was previously authorized by the user.
getUserInfo(false);
}
};
})();
window.onload = googlePlusUserLoader.onload;