-
Notifications
You must be signed in to change notification settings - Fork 6
/
authentication.js
381 lines (314 loc) · 15.1 KB
/
authentication.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
const QueryString = require('querystring')
const Debug = require('debug')('xbox-webapi-node:authentication')
const HttpClient = require('./http.js')
var fs = require('fs')
var express = require('express')
module.exports = function(clientId, secret, userToken, uhs){
return {
_clientId: clientId,
_clientSecret: secret,
_userToken: userToken,
_uhs: uhs,
_scopes: ['XboxLive.signin', 'XboxLive.offline_access'],
_express: false,
_expressServer: false,
_tokensFile: '.tokens.json',
_tokens: {
oauth: {},
user: {},
xsts: {}
},
_user: false,
_endpoints: {
live: 'https://login.live.com',
auth: 'https://user.auth.xboxlive.com',
xsts: 'https://xsts.auth.xboxlive.com'
},
generateAuthorizationUrl: function(){
var params_object = {
"client_id": this._clientId,
"response_type": "code",
"approval_prompt": "auto",
"scope": this._scopes.join(' '),
"redirect_uri": this.getReturnUrl(),
}
var params = QueryString.stringify(params_object)
return 'https://login.live.com/oauth20_authorize.srf?'+params
},
getReturnUrl: function(){
// @TODO: Get port from express
return 'http://localhost:8080/auth/callback'
},
startServer: function(callback, port){
if(port === undefined)
port = 8080
var app = express()
app.get('/auth/callback', function(req, res) {
this.getTokenRequest(req.query.code).then(function(data){
res.send('Login succeeded!')
Debug('[AUTH] Got oauth token:', data)
this._tokens.oauth = data
this.saveTokens()
callback(data)
this.stopServer()
}.bind(this)).catch(function(error){
console.log('OAUTH Error:', error)
res.send('Failed to login. Please check the console output')
})
}.bind(this))
var server = app.listen(port, function () {
Debug('[HTTP] Authentication server ready on :'+port)
Debug('Authorization URL: ', this.generateAuthorizationUrl())
}.bind(this))
this._express = app
this._expressServer = server
},
stopServer: function(){
return this._expressServer.close()
},
refreshTokens: function(type){
if(type == undefined){
type = 'oauth'
}
Debug('refreshTokens('+type+')')
return new Promise(function(resolve, reject) {
if(type == 'oauth'){
if(this._tokens.oauth.refresh_token){
this.refreshTokens('user').then(function(){
resolve()
}).catch(function(error){
reject(error)
})
} else {
reject('No oauth token found. Run authentication flow first')
}
} else if(type == 'user'){
if(this._tokens.user.Token){
var user_expire_user = new Date(this._tokens.user.NotAfter).getTime()
if(new Date() > user_expire_user){
// Oauth token expired, refresh user token
// console.log('TODO: refresh user token')
this.refreshToken(this._tokens.oauth.refresh_token).then(function(user_token){
Debug('refreshTokens('+type+') Get token,', user_token)
this._tokens.oauth = user_token
this.saveTokens()
this.getUserToken(this._tokens.oauth.access_token).then(function(token){
this._tokens.user = token
this._tokens.xsts = {}
// this._tokens.xsts = false // Force xsts refresh
this.saveTokens()
this.refreshTokens('xsts').then(function(){
resolve()
}).catch(function(error){
reject(error)
})
}.bind(this)).catch(function(error){
reject('Unable to refresh oauth access token. Reauthenticate again')
})
}.bind(this)).catch(function(error){
reject('Unable to refresh user access token. Reauthenticate again')
})
} else {
// Token is still valid
// console.log('user token is valid')
this.refreshTokens('xsts').then(function(){
resolve()
}).catch(function(error){
reject(error)
})
}
} else {
// Get user token
this.getUserToken(this._tokens.oauth.access_token).then(function(data){
// Got user token, continue with xsts
this._tokens.user = data
this._tokens.xsts = {}
// this._tokens.xsts = false // Force xsts refresh
this.saveTokens()
this.refreshTokens('xsts').then(function(){
resolve()
}).catch(function(error){
reject(error)
})
}.bind(this)).catch(function(error){
reject(error)
})
}
} else if(type == 'xsts'){
// console.log(this._tokens)
if(this._tokens.xsts.Token){
Debug('refreshTokens('+type+') Has token')
var oauth_expire = new Date(this._tokens.xsts.NotAfter).getTime()
if(new Date() > oauth_expire){
Debug('refreshTokens('+type+') Token is expired, refreshing token')
// Oauth token expired, refresh user token
// console.log('TODO: refresh xsts token')
this._tokens.xsts = {}
this.saveTokens()
this.getXstsToken(this._tokens.user.Token).then(function(token){
this._tokens.xsts = token
this.saveTokens()
Debug('refreshTokens('+type+') Token refresh succes')
this.refreshTokens('xsts').then(function(){
Debug('refreshTokens('+type+') Token xsts flow ok')
resolve()
}).catch(function(error){
Debug('refreshTokens('+type+') Token xsts flow failed')
reject(error)
})
}.bind(this)).catch(function(error){
// @TODO: Investigate this part of the auth flow
Debug('refreshTokens('+type+') Token refresh failed:', error)
reject('Unable to refresh xsts access token. Reauthenticate again', error)
}.bind(this))
} else {
Debug('refreshTokens('+type+') Token is valid')
// Token is still valid
// console.log('xsts token is valid')
this._user = {
gamertag: this._tokens.xsts.DisplayClaims.xui[0].gtg,
xid: this._tokens.xsts.DisplayClaims.xui[0].xid,
uhs: this._tokens.xsts.DisplayClaims.xui[0].uhs
// @TODO: Check if we need more data?
}
resolve()
}
} else {
Debug('refreshTokens('+type+') Token not set, get token')
// Get user token
this.getXstsToken(this._tokens.user.Token).then(function(data){
// Got user token, continue with xsts
this._tokens.xsts = data
this.saveTokens()
// console.log(this._tokens)
this._user = {
gamertag: data.DisplayClaims.xui[0].gtg,
xid: data.DisplayClaims.xui[0].xid,
uhs: data.DisplayClaims.xui[0].uhs
// @TODO: Check if we need more data?
}
resolve()
}.bind(this)).catch(function(error){
reject(error)
}.bind(this))
}
}
}.bind(this))
},
getTokenRequest: function(code, callback){
return new Promise(function(resolve, reject) {
const tokenParams = {
"client_id": this._clientId,
"grant_type": "authorization_code",
"scope": this._scopes.join(' '),
"code": code,
"redirect_uri": this.getReturnUrl()
}
if(this._clientSecret !== '')
tokenParams.client_secret = this._clientSecret
var postData = QueryString.stringify(tokenParams)
HttpClient().post(this._endpoints.live+'/oauth20_token.srf', {'Content-Type': 'application/x-www-form-urlencoded'}, postData).then(function(data){
var responseData = JSON.parse(data)
responseData.issued = new Date().toISOString()
resolve(responseData)
}).catch(function(error){
reject(error)
})
}.bind(this))
},
refreshToken: function(refreshToken){
Debug('refreshToken()')
return new Promise(function(resolve, reject) {
const tokenParams = {
"client_id": this._clientId,
"grant_type": "refresh_token",
"scope": this._scopes.join(' '),
"refresh_token": refreshToken,
}
if(this._clientSecret !== '')
tokenParams.client_secret = this._clientSecret
var postData = QueryString.stringify(tokenParams)
HttpClient().post(this._endpoints.live+'/oauth20_token.srf', {'Content-Type': 'application/x-www-form-urlencoded'}, postData).then(function(data){
var responseData = JSON.parse(data)
responseData.issued = new Date().toISOString()
Debug('refreshToken() Resolved')
resolve(responseData)
}).catch(function(error){
Debug('refreshToken() Rejected', error)
reject(error)
})
}.bind(this))
},
getUserToken: function(accessToken){
Debug('getUserToken()')
return new Promise(function(resolve, reject) {
const tokenParams = {
"RelyingParty": "http://auth.xboxlive.com",
"TokenType": "JWT",
"Properties": {
"AuthMethod": "RPS",
"SiteName": "user.auth.xboxlive.com",
"RpsTicket": "d="+accessToken
},
}
const postData = JSON.stringify(tokenParams)
HttpClient().post(this._endpoints.auth+'/user/authenticate', {'Content-Type': 'application/json'}, postData).then(function(data){
var responseData = JSON.parse(data)
// responseData.issued = new Date().toISOString()
Debug('getUserToken() Resolved')
resolve(responseData)
}).catch(function(error){
Debug('getUserToken() Rejected', error)
reject(error)
})
}.bind(this))
},
getXstsToken: function(userToken){
Debug('getXstsToken()')
return new Promise(function(resolve, reject) {
const tokenParams = {
"RelyingParty": "http://xboxlive.com",
"TokenType": "JWT",
"Properties": {
"UserTokens": [userToken],
"SandboxId": "RETAIL",
},
}
const postData = JSON.stringify(tokenParams)
HttpClient().post(this._endpoints.xsts+'/xsts/authorize', {'Content-Type': 'application/json', 'x-xbl-contract-version': '1'}, postData).then(function(data){
Debug('getXstsToken() Resolved')
resolve(JSON.parse(data))
}).catch(function(error){
Debug('getXstsToken() Rejected')
reject(error)
})
}.bind(this))
},
isAuthenticated: function(){
Debug('isAuthenticated()')
return new Promise(function(resolve, reject) {
if (fs.existsSync(this._tokensFile)){
this.loadTokens()
}
if(this._clientId !== ''){
this.refreshTokens().then(function(){
resolve()
}).catch(function(error){
reject(error)
})
} else if(this._userToken !== '' && this._uhs !== ''){
resolve()
} else {
reject({ error: 'No authentication supplied.' })
}
}.bind(this))
},
saveTokens: function(){
return fs.writeFileSync(this._tokensFile, JSON.stringify(this._tokens))
},
loadTokens: function(){
var token_store = fs.readFileSync(this._tokensFile).toString()
this._tokens = JSON.parse(token_store)
}
}
}