-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
185 lines (151 loc) · 6.04 KB
/
app.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
// Copyright © 2023. Cloud Software Group, Inc. All Rights Reserved.
// In a production service, this should this should be pulled in from another source, such as a configuration file, environment variable, or other secure location
const APPLICATION_ID = ""
// Creates an axios instance for calling the Citrix API that handles retrieving tokens
function CreateApiHandler(baseUrl, tmsBaseUrl, requestVerifyToken) {
// access token and other details stored in memory within a private function
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures#emulating_private_methods_with_closures
let token = null
let promiseForToken = null
let refreshTime = -1;
// Retrieve token from TMS (if needed)
async function RetrieveToken() {
async function RetrieveTokenDo() {
let tokenResponse = await axios({
headers: { 'RequestVerificationToken': requestVerifyToken },
method: 'post',
url: `${tmsBaseUrl}/Session/RetrieveToken`,
withCredentials: true
})
let expiresIn = tokenResponse.data.expiresIn - 60
refreshTime = Date.now() + (expiresIn) * 1000
token = tokenResponse.data.accessToken
return token
}
if (Date.now() >= refreshTime || promiseForToken == null) {
refreshTime = Date.now() + 30000
promiseForToken = RetrieveTokenDo();
}
return await promiseForToken;
}
const axiosInstance = axios.create({
baseURL: baseUrl,
});
// Interceptor for retrieving and adding token to Auth header
axiosInstance.interceptors.request.use(async function (config) {
const token = await RetrieveToken()
config.headers["Citrix-ApplicationId"] = APPLICATION_ID
if (token) {
config.headers["Authorization"] = "Bearer " + token
}
return config;
})
return axiosInstance
}
var apiHandler = null
window.addEventListener('load', async () => {
const el = $('#app');
// Load html templates
const errorTemplate = $('#error-template').html();
const loadingTemplate = $('#loading-template').html();
const resourcesTemplate = $('#resources-template').html();
const signInTemplate = $('#signin-template').html();
// Show the loading page
el.html(loadingTemplate);
try {
const tmsBaseUrl = "https://localhost:7182"
const sessionState = await axios.get(`${tmsBaseUrl}/Session/CheckSession`, { withCredentials: true })
if (sessionState.data.isLoggedIn) {
apiHandler = CreateApiHandler(`https://${sessionState.data.workspaceDomain}/api`, tmsBaseUrl, sessionState.data.requestVerificationToken)
let discoveryResponse = await apiHandler.get(`https://${sessionState.data.workspaceDomain}/api/discovery/configurations`)
let resourcesListUrl = new URL(discoveryResponse.data.services.find(service => service.service === "store").endpoints.find(endpoint => endpoint.id === "ListResources").url)
resourcesListUrl.searchParams.append('acceptCachedResults', 'true');
let resourcesResponse = await apiHandler.get(
resourcesListUrl
);
let template = Handlebars.compile(resourcesTemplate)
let resourceTable = template(resourcesResponse.data)
el.html(resourceTable);
} else {
// Show the sign in page
el.html(signInTemplate);
}
} catch (err) {
// Show an error page
el.html(errorTemplate);
throw err
}
})
let launching = false
async function PerformLaunch(card, resourceLinks) {
if (launching) {
return;
}
try {
$(card).children(".loader").addClass('active')
launching = true;
const resourceLinks = card.dataset
const launchType = document.getElementById('launch-type').value
switch (launchType) {
case "Receiver": {
await launchReceiver(resourceLinks.launchstatus)
break;
}
case "HTML5": {
await launchHTML5(resourceLinks.launchica)
break;
}
case "IFrame": {
await launchIFrame(resourceLinks.launchica)
break;
}
}
}
finally {
launching = false;
$(card).children(".loader").removeClass('active')
}
}
async function launchReceiver(launchUrl) {
let launchTicketResponse = await apiHandler.post(launchUrl)
let receiverUri = launchTicketResponse.data.receiverUri
window.open(receiverUri, "Launching...")
}
async function launchHTML5(launchUrl) {
citrix.receiver.setPath("https://localhost:7183/receiver");
let icaFile = await apiHandler.get(launchUrl)
const sessionId = "html5"
const connectionParams = {
"launchType": "newtab",
"container": {
"type": "window"
}
};
function sessionCreated(sessionObject){
const launchData = {"type": "ini", value: icaFile.data};
sessionObject.start(launchData);
}
citrix.receiver.createSession(sessionId, connectionParams,sessionCreated);
}
async function launchIFrame(launchUrl) {
citrix.receiver.setPath("https://localhost:7183/receiver");
let icaFile = await apiHandler.get(launchUrl)
const id = "iframe"
const connectionParams = {
"launchType": "embed",
"container": {
"type": "iframe",
"id": "sessionIframe"
}
};
document.getElementById("sessionIframe").style.display = "block";
function sessionCreated(sessionObject){
function connectionClosedHandler(event){
document.getElementById("sessionIframe").style.display = "none";
}
sessionObject.addListener("onConnectionClosed",connectionClosedHandler);
const launchData = {"type": "ini", value: icaFile.data};
sessionObject.start(launchData);
}
citrix.receiver.createSession(id, connectionParams,sessionCreated);
}