-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
140 lines (128 loc) · 4.12 KB
/
main.js
File metadata and controls
140 lines (128 loc) · 4.12 KB
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
var baseFusionAuthURL = 'http://localhost:9011';
var grantType = 'urn:ietf:params:oauth:grant-type:device_code';
var clientId = 'c6b9de0c-2f71-4019-920b-58bc2d4206fc';
var tokenEndpoint;
var expiresIn;
var intervalSeconds = 5;
var deviceCode;
var pollId;
var deviceAuthEndpoint;
var accessToken;
// retrieve the device_authorization_endpoint and token_endpoint
$(document).ready(function() {
$.ajax({
type: 'GET',
url: baseFusionAuthURL + '/.well-known/openid-configuration',
datatype: 'json',
success: function(data) {
deviceAuthEndpoint = data.device_authorization_endpoint;
tokenEndpoint = data.token_endpoint;
}
});
// Load saved values
const config = JSON.parse(localStorage.getItem('config') || '{}');
for (const key in config) {
$("input[name='" + key + "'],textarea[name='" + key + "']").val(config[key]);
}
$("#connect-button").click(connectDevice);
$("#reset-button").click(reset);
$('input,textarea').on('keyup', function(e) {
const target = $(e.target);
const config = JSON.parse(localStorage.getItem('config') || '{}');
config[target.attr('name')] = target.val();
localStorage.setItem('config', JSON.stringify(config));
});
});
// call the device_authorization_endpoint, display the verification_uri and user_code, then start polling /token endpoint
function connectDevice() {
const idpId = $('input[name="identityProviderId"]').val() || '';
const idpToken = $('textarea[name="identityProviderToken"]').val() || '';
let idpLinkScope = '';
if (idpId && idpToken) {
idpLinkScope = ' idp-link:' + idpId + ':' + idpToken;
}
$.ajax({
type: 'POST',
url: deviceAuthEndpoint,
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
data: {
'client_id': clientId,
'scope': 'offline_access' + idpLinkScope,
'metaData.device.name': 'Demo TV app',
'metaData.device.type': 'TV'
},
datatype: 'json',
success: function(data) {
expiresIn = data.expires_in;
intervalSeconds = data.interval;
deviceCode = data.device_code;
// make user_code a little more readable
let userCode = data.user_code;
let ucLen = userCode.length / 2;
userCode = userCode.substring(0,ucLen) + "-" + userCode.substring(ucLen);
// Remove the schema to make it simpler on screen
$("#device-url").text(data.verification_uri.replace("http://", "").replace("https://", ""));
$("#user-code").text(userCode);
$("#connect-device").hide();
$("#sign-in").show();
// generate a qr code from the verification_uri_complete
$("#qrcode").empty();
new QRCode(document.getElementById("qrcode"), {
text: data.verification_uri_complete,
width: 150,
height:150
});
$("#qrlink").attr("href", data.verification_uri_complete)
.attr("target", "_blank");
pollForToken();
},
error: function(data) {
$("#error-msg").show();
}
});
}
// poll the token endpoint, displaying success or error messages, and adjusting polling interval as appropriate
function pollForToken() {
pollId = setInterval(function() {
$.ajax({
type: 'POST',
url: tokenEndpoint,
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
data: {'device_code': deviceCode, 'grant_type': grantType, 'client_id': clientId},
datatype: 'json',
success: function(data) {
clearInterval(pollId);
// use the access_token
accessToken = data.access_token;
$("#sign-in").hide();
$("#success-msg").show();
},
error: function(data) {
let err = $.parseJSON(data.responseText);
if (err.error == 'slow_down') {
clearInterval(pollId);
// spec says to add 5 seconds to all subsequent calls if this happens
intervalSeconds += 5;
pollForToken();
} else if (err.error == 'authorization_pending') {
// keep polling
} else {
// an invalid request occurred, nothing to do but to stop and let user try again
clearInterval(pollId);
$("#sign-in").hide();
$("#connect-device").show();
$("#error-msg").show();
}
}
});
}, intervalSeconds * 1000);
}
function reset() {
$("#connect-device").show();
$("#sign-in").hide();
$("#success").hide();
$("#fa-tut").hide();
if (pollId) {
clearInterval(pollId);
}
}