Skip to content

Commit

Permalink
small fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
grzegorz914 committed Feb 13, 2023
1 parent 07babab commit b5d9745
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 44 deletions.
4 changes: 2 additions & 2 deletions homebridge-ui/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {
HomebridgePluginUiServer,
RequestError
} = require('@homebridge/plugin-ui-utils');
const Authentication = require('../src/webApi//authentication.js')
const Authentication = require('../src/webApi/authentication.js')
const fs = require('fs');
const fsPromises = fs.promises;

Expand All @@ -30,7 +30,7 @@ class PluginUiServer extends HomebridgePluginUiServer {

try {
if (fs.existsSync(authTokenFile)) {
await fsPromises.writeFile(authTokenFile, '');
await fsPromises.writeFile(authTokenFile, JSON.stringify({}));
};

return true;
Expand Down
18 changes: 9 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,20 @@ class XBOXDEVICE {
this.inputsTargetVisibilityFile = `${this.prefDir}/inputsTargetVisibility_${this.host.split('.').join('')}`;

//check if the directory exists, if not then create it
if (fs.existsSync(this.prefDir) === false) {
if (!fs.existsSync(this.prefDir)) {
fs.mkdirSync(this.prefDir);
}
if (fs.existsSync(this.authTokenFile) === false) {
fs.writeFileSync(this.authTokenFile, '');
if (!fs.existsSync(this.authTokenFile)) {
fs.writeFileSync(this.authTokenFile, JSON.stringify({}));
}
if (fs.existsSync(this.inputsFile) === false) {
fs.writeFileSync(this.inputsFile, '');
if (!fs.existsSync(this.inputsFile)) {
fs.writeFileSync(this.inputsFile, JSON.stringify([]));
}
if (fs.existsSync(this.inputsNamesFile) === false) {
fs.writeFileSync(this.inputsNamesFile, '');
if (!fs.existsSync(this.inputsNamesFile)) {
fs.writeFileSync(this.inputsNamesFile, JSON.stringify({}));
}
if (fs.existsSync(this.inputsTargetVisibilityFile) === false) {
fs.writeFileSync(this.inputsTargetVisibilityFile, '');
if (!fs.existsSync(this.inputsTargetVisibilityFile)) {
fs.writeFileSync(this.inputsTargetVisibilityFile, JSON.stringify({}));
}

//mqtt client
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"displayName": "Xbox TV",
"name": "homebridge-xbox-tv",
"version": "2.6.0",
"version": "2.6.1",
"description": "Homebridge plugin (https://github.com/homebridge/homebridge) to control Xbox game consoles.",
"license": "MIT",
"author": "grzegorz914",
Expand Down
64 changes: 33 additions & 31 deletions src/webApi/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,16 @@ class AUTHENTICATION {
this.userToken = config.userToken;
this.uhs = config.uhs;
this.tokensFile = config.tokensFile;

this.scopes = ['XboxLive.signin', 'XboxLive.offline_access'];
this.endpoints = {
live: 'https://login.live.com',
auth: 'https://user.auth.xboxlive.com',
xsts: 'https://xsts.auth.xboxlive.com'
}

this.user = {
gamertag: {},
gtg: {},
xid: {},
uhs: {},
agg: {},
usr: {},
utr: {},
prv: {}
prv: {},
agg: {}
};
this.tokens = {
oauth: {},
Expand Down Expand Up @@ -107,13 +101,13 @@ class AUTHENTICATION {
await this.refreshTokens('xsts');
resolve();
} else {
this.user.gamertag = this.tokens.xsts.DisplayClaims.xui[0].gtg;
this.user.gtg = this.tokens.xsts.DisplayClaims.xui[0].gtg;
this.user.xid = this.tokens.xsts.DisplayClaims.xui[0].xid;
this.user.uhs = this.tokens.xsts.DisplayClaims.xui[0].uhs;
this.user.agg = this.tokens.xsts.DisplayClaims.xui[0].agg;
this.user.usr = this.tokens.xsts.DisplayClaims.xui[0].usr;
this.user.utr = this.tokens.xsts.DisplayClaims.xui[0].utr;
this.user.prv = this.tokens.xsts.DisplayClaims.xui[0].prv;
this.user.agg = this.tokens.xsts.DisplayClaims.xui[0].agg;
resolve();
}
} catch (error) {
Expand All @@ -125,13 +119,13 @@ class AUTHENTICATION {
this.tokens.xsts = userToken;
await this.saveTokens(this.tokens);

this.user.gamertag = userToken.DisplayClaims.xui[0].gtg;
this.user.gtg = userToken.DisplayClaims.xui[0].gtg;
this.user.xid = userToken.DisplayClaims.xui[0].xid;
this.user.uhs = userToken.DisplayClaims.xui[0].uhs;
this.user.agg = userToken.DisplayClaims.xui[0].agg;
this.user.usr = userToken.DisplayClaims.xui[0].usr;
this.user.utr = userToken.DisplayClaims.xui[0].utr;
this.user.prv = userToken.DisplayClaims.xui[0].prv;
this.user.agg = userToken.DisplayClaims.xui[0].agg;
resolve();
} catch (error) {
reject(error);
Expand All @@ -157,7 +151,9 @@ class AUTHENTICATION {
}

const postData = QueryString.stringify(tokenParams);
const data = await this.httpClient.post(this.endpoints.live + '/oauth20_token.srf', { 'Content-Type': 'application/x-www-form-urlencoded' }, postData);
const url = 'https://login.live.com/oauth20_token.srf';
const headers = { 'Content-Type': 'application/x-www-form-urlencoded' };
const data = await this.httpClient.post(url, headers, postData);
const responseData = JSON.parse(data);
responseData.issued = new Date().toISOString();
resolve(responseData);
Expand All @@ -171,17 +167,19 @@ class AUTHENTICATION {
return new Promise(async (resolve, reject) => {
try {
const tokenParams = {
"RelyingParty": "http://auth.xboxlive.com",
"TokenType": "JWT",
"RelyingParty": 'http://auth.xboxlive.com',
"TokenType": 'JWT',
"Properties": {
"AuthMethod": "RPS",
"SiteName": "user.auth.xboxlive.com",
"RpsTicket": "d=" + accessToken
"AuthMethod": 'RPS',
"SiteName": 'user.auth.xboxlive.com',
"RpsTicket": `d=${accessToken}`
},
}

const postData = JSON.stringify(tokenParams);
const data = await this.httpClient.post(this.endpoints.auth + '/user/authenticate', { 'Content-Type': 'application/json' }, postData);
const url = 'https://user.auth.xboxlive.com/user/authenticate';
const headers = { 'Content-Type': 'application/json' };
const data = await this.httpClient.post(url, headers, postData);
const responseData = JSON.parse(data);
resolve(responseData);
} catch (error) {
Expand All @@ -194,16 +192,18 @@ class AUTHENTICATION {
return new Promise(async (resolve, reject) => {
try {
const tokenParams = {
"RelyingParty": "http://xboxlive.com",
"TokenType": "JWT",
"RelyingParty": 'http://xboxlive.com',
"TokenType": 'JWT',
"Properties": {
"UserTokens": [userToken],
"SandboxId": "RETAIL",
"SandboxId": 'RETAIL',
},
}

const postData = JSON.stringify(tokenParams);
const data = await this.httpClient.post(this.endpoints.xsts + '/xsts/authorize', { 'Content-Type': 'application/json', 'x-xbl-contract-version': '1' }, postData);
const url = 'https://xsts.auth.xboxlive.com/xsts/authorize';
const headers = { 'Content-Type': 'application/json', 'x-xbl-contract-version': '1' };
const data = await this.httpClient.post(url, headers, postData);
const responseData = JSON.parse(data);
resolve(responseData);
} catch (error) {
Expand Down Expand Up @@ -239,18 +239,20 @@ class AUTHENTICATION {
try {
const tokenParams = {
"client_id": this.clientId,
"grant_type": "authorization_code",
"grant_type": 'authorization_code',
"scope": this.scopes.join(' '),
"code": webApiToken,
"redirect_uri": `http://localhost:8581/auth/callback`
"redirect_uri": 'http://localhost:8581/auth/callback'
}

if (this.clientSecret !== '') {
tokenParams.client_secret = this.clientSecret;
}

const postData = QueryString.stringify(tokenParams);
const data = await this.httpClient.post(this.endpoints.live + '/oauth20_token.srf', { 'Content-Type': 'application/x-www-form-urlencoded' }, postData);
const url = 'https://login.live.com/oauth20_token.srf';
const headers = { 'Content-Type': 'application/x-www-form-urlencoded' };
const data = await this.httpClient.post(url, headers, postData);
const responseData = JSON.parse(data);
responseData.issued = new Date().toISOString();
this.tokens.oauth = responseData;
Expand All @@ -267,13 +269,13 @@ class AUTHENTICATION {
try {
const paramsObject = {
"client_id": this.clientId,
"response_type": "code",
"approval_prompt": "auto",
"response_type": 'code',
"approval_prompt": 'auto',
"scope": this.scopes.join(' '),
"redirect_uri": `http://localhost:8581/auth/callback`
"redirect_uri": 'http://localhost:8581/auth/callback'
}
const params = QueryString.stringify(paramsObject);
const oauth2URI = 'https://login.live.com/oauth20_authorize.srf?' + params;
const oauth2URI = `https://login.live.com/oauth20_authorize.srf?${params}`;
resolve(oauth2URI);
} catch (error) {
reject(error);
Expand Down
2 changes: 1 addition & 1 deletion src/webApi/xboxwebapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class XBOXWEBAPI extends EventEmitter {

this.emit('authenticated', true);
this.headers = {
'Authorization': (this.authentication.userToken !== '' && this.authentication.uhs !== '') ? 'XBL3.0 x=' + this.authentication.uhs + ';' + this.authentication.userToken : 'XBL3.0 x=' + this.authentication.user.uhs + ';' + this.authentication.tokens.xsts.Token,
'Authorization': (this.authentication.userToken !== '' && this.authentication.uhs !== '') ? `XBL3.0 x=${this.authentication.uhs};${this.authentication.userToken}` : `XBL3.0 x=${this.authentication.user.uhs};${this.authentication.tokens.xsts.Token}`,
'Accept-Language': 'en-US',
'x-xbl-contract-version': '4',
'x-xbl-client-name': 'XboxApp',
Expand Down

0 comments on commit b5d9745

Please sign in to comment.