This repository was archived by the owner on Nov 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathgoogle-auth.js
68 lines (54 loc) · 2.32 KB
/
google-auth.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
/**
* Handles the Google login workflow.
*/
$(function() {
var plusOnScriptEl = document.createElement('script'),
referenceScriptEl = document.getElementsByTagName('script')[0],
// Called when the auth attempt has completed
s3GoogleOauthHandler = function(authResult) {
// If authenticated...
if (authResult.status.signed_in) {
var expiresInMs = parseInt(authResult.expires_in) * 1000;
$(document).trigger("tokenReceived.s3Demo");
setUserName(authResult.access_token);
// Get S3 credentials
s3DemoGlobals.assumeRoleWithWebIdentity({
roleArn: "arn:aws:iam::776099607611:role/demo-s3-noserver-google",
idToken: authResult.id_token
});
// Ensure the user is asked to re-auth before the token expires
setTimeout(function() {
alert("Token expired. You must sign in again.");
$(document).trigger("tokenExpired.s3Demo");
}, expiresInMs - 10000)
}
else {
$(document).trigger("tokenExpired.s3Demo");
}
},
showButton = function() {
$("#google-signin").show();
},
// Grabs the authenticated user's name (for file storage)
setUserName = function(accessToken) {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
if (xhr.status === 200) {
var userName = JSON.parse(xhr.responseText).displayName;
s3DemoGlobals.userName = userName;
}
};
xhr.open("GET", "https://www.googleapis.com/plus/v1/people/me?access_token=" + accessToken);
xhr.send();
};
window.s3GoogleOauthHandler = s3GoogleOauthHandler;
// Setup the script tag used to load the SDK
plusOnScriptEl.type = "text/javascript";
plusOnScriptEl.async = true;
plusOnScriptEl.src = "https://plus.google.com/js/client:plusone.js";
referenceScriptEl.parentNode.insertBefore(plusOnScriptEl, referenceScriptEl);
$(document).on("tokenExpired.s3Demo", showButton);
$(document).on("tokenReceived.s3Demo", function() {
$("#google-signin").hide();
});
});