forked from arenaxr/arena-web-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
142 lines (131 loc) · 5.03 KB
/
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
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
// auth.js
//
// Authentication and Authorization for the following ARENA assets:
// - MQTT broker
//
// Required:
// <script src="https://apis.google.com/js/platform.js"></script>
// <script src="./defaults.js"></script> <!-- for window.defaults -->
// <script src="./auth.js"></script> <!-- browser authorization flow -->
//
// Optional:
// <script src="./events.js"></script> <!-- for window.globals -->
//
// Implement the following 'onauth' event handler and use it to start code that would
// automatically connects to the MQTT broker so that authentication and access tokens
// can be present when making a broker connection which will need username (email) and
// password (access token).
//
// window.addEventListener('onauth', function (e) {
// client.connect({
// onSuccess: onConnect,
// userName: e.detail.mqtt_username,
// password: e.detail.mqtt_token
// });
// });
'use strict';
//window.dispatchEvent(new CustomEvent('onauth'));
var auth2;
// check if the current user is already signed in
gapi.load('auth2', function () {
auth2 = gapi.auth2.init({
client_id: defaults.gAuthClientId
}).then(function () {
auth2 = gapi.auth2.getAuthInstance();
if (!auth2.isSignedIn.get()) {
console.log("User is not signed in.");
// send login with redirection url from this page
location.href = "./signin?redirect_uri=" + encodeURI(location.href);;
} else {
console.log("User is already signed in.");
var googleUser = auth2.currentUser.get();
onSignIn(googleUser);
}
});
});
function onSignIn(googleUser) {
var profile = googleUser.getBasicProfile();
console.log('ID: ' + profile.getId());
console.log('Full Name: ' + profile.getName());
console.log('Email: ' + profile.getEmail());
// add auth name to objects when user has not defined their name
if (typeof globals !== 'undefined') {
if (typeof defaults !== 'undefined' && globals.userParam == defaults.userParam) {
// Use auth name to create human-readable name
globals.displayName = profile.getName();
// globals.userParam = encodeURI(profile.getName());
globals.userParam = profile.getName().replace(/[^a-zA-Z0-9]/g, '');
// replay global id setup from events.js
globals.idTag = globals.timeID + "_" + globals.userParam; // e.g. 1234_eric
if (globals.fixedCamera !== '') {
globals.camName = "camera_" + globals.fixedCamera + "_" + globals.fixedCamera;
} else {
globals.camName = "camera_" + globals.idTag; // e.g. camera_1234_eric
}
globals.viveLName = "viveLeft_" + globals.idTag; // e.g. viveLeft_9240_X
globals.viveRName = "viveRight_" + globals.idTag; // e.g. viveRight_9240_X
}
}
// request mqtt-auth
var id_token = googleUser.getAuthResponse().id_token;
requestMqttToken(profile.getEmail(), id_token);
}
function signOut(rootPath) {
// logout, and disassociate user
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
console.log('User signed out.');
});
auth2.disconnect();
// back to signin page
location.href = rootPath + "/signin?redirect_uri=" + encodeURI(location.href);
}
function requestMqttToken(mqtt_username, id_token) {
// Request JWT before connection
let xhr = new XMLHttpRequest();
var params = "username=" + mqtt_username + "&id_token=" + id_token;
params += "&id_auth=google";
// provide user control topics for token construction
if (typeof globals !== 'undefined') {
if (globals.scenenameParam) {
params += "&scene=" + globals.scenenameParam;
}
if (globals.camName) {
params += "&camid=" + globals.camName;
}
if (globals.viveLName) {
params += "&ctrlid1=" + globals.viveLName;
}
if (globals.viveRName) {
params += "&ctrlid2=" + globals.viveRName;
}
}
xhr.open('POST', defaults.urlMqttAuth);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(params);
xhr.responseType = 'json';
xhr.onload = () => {
if (xhr.status !== 200) {
alert(`Error loading token: ${xhr.status}: ${xhr.statusText}`);
} else {
console.log("got user/token:", xhr.response.username, xhr.response.token);
// token must be set to authorize access to MQTT broker
const authCompleteEvent = new CustomEvent('onauth', {
detail: {
mqtt_username: xhr.response.username,
mqtt_token: xhr.response.token
}
});
window.dispatchEvent(authCompleteEvent);
}
};
}
function getAuthStatus() {
var googleUser = auth2.currentUser.get();
var profile = googleUser.getBasicProfile();
return {
type: "Google",
name: profile.getName(),
email: profile.getEmail(),
};
}