From 673a64107edafbd79c62e1cd925e72a7a6bafbc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Haylee=20Sch=C3=A4fer?= Date: Fri, 18 Oct 2024 22:19:40 +0200 Subject: [PATCH] that too --- dist/MicrosoftAuth.js | 30 +++++++++++++++++------------- dist/MicrosoftAuth.js.map | 2 +- src/MicrosoftAuth.ts | 28 ++++++++++++++++------------ 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/dist/MicrosoftAuth.js b/dist/MicrosoftAuth.js index 25ae825..14b42a0 100644 --- a/dist/MicrosoftAuth.js +++ b/dist/MicrosoftAuth.js @@ -79,19 +79,23 @@ class MicrosoftAuth { // username+password login doesn't seem to need this prefix, code auth does rpsTicket = `d=${rpsTicket}`; } - // https://user.auth.xboxlive.com/user/authenticate - let userTokenResponse; - try { - userTokenResponse = await XboxLiveAuth.xbl.exchangeRpsTicketForUserToken(rpsTicket); - } - catch (e) { - Sentry.captureException(e, { - tags: { - stage: 'exchangeRpsTicketForIdentities' - } - }); - throw new MSAError_1.MSAError('exchangeRpsTicketForIdentities', e); - } + let userTokenResponse = await Sentry.startSpan({ + op: 'xbl', + name: 'exchangeRpsTicketForUserToken' + }, async () => { + // https://user.auth.xboxlive.com/user/authenticate + try { + return await XboxLiveAuth.xbl.exchangeRpsTicketForUserToken(rpsTicket); + } + catch (e) { + Sentry.captureException(e, { + tags: { + stage: 'exchangeRpsTicketForIdentities' + } + }); + throw new MSAError_1.MSAError('exchangeRpsTicketForIdentities', e); + } + }); // console.log("exchangeRpsTicket") // console.log(JSON.stringify(userTokenResponse)) return { diff --git a/dist/MicrosoftAuth.js.map b/dist/MicrosoftAuth.js.map index 8a0ac75..a8cc267 100644 --- a/dist/MicrosoftAuth.js.map +++ b/dist/MicrosoftAuth.js.map @@ -1 +1 @@ -{"version":3,"file":"MicrosoftAuth.js","sourceRoot":"/","sources":["MicrosoftAuth.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,s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AE;YACV,aAAa,CAAC,MAAM,CAAC,KAAK,CAAC,wBAAwB,CAAC,CAAC;YACrD,MAAM,IAAI,GAAG;gBACT,WAAW,EAAE,OAAO,CAAC,GAAG,CAAC,aAAa;gBACtC,eAAe,EAAE,OAAO,CAAC,GAAG,CAAC,iBAAiB;gBAC9C,eAAe,EAAE,gBAAgB;gBACjC,YAAY,EAAE,eAAe;gBAC7B,cAAc,EAAE,IAAI,CAAC,WAAW;aACnC,CAAA;YACD,OAAO,MAAM,IAAI,CAAC,gCAAgC,CAAC,IAAI,CAAC,CAAC;QAC7D,CAAC,CAAC,CAAC;IACP,CAAC;;AAxPL,sCA0PC;AAxPU,oBAAM,GAAmB,iBAAO,CAAC,YAAY,EAAE,CAAC","sourcesContent":["import * as process from \"node:process\";\r\nimport axios, { AxiosRequestConfig, AxiosResponse } from \"axios\";\r\nimport * as qs from \"qs\";\r\nimport * as XboxLiveAuth from \"@xboxreplay/xboxlive-auth\"\r\nimport { XBLExchangeTokensResponse } from \"@xboxreplay/xboxlive-auth\"\r\nimport {\r\n MicrosoftAuthInfo,\r\n MicrosoftIdentities, MicrosoftOauthResult,\r\n XboxInfo,\r\n XboxLoginResponse,\r\n XSTSResponse\r\n} from \"@mineskin/types\";\r\nimport { RequestHandlers } from \"./types/RequestHandler\";\r\nimport { MSAError } from \"./MSAError\";\r\nimport { epochSeconds, toEpochSeconds } from \"./util\";\r\nimport winston from \"winston\";\r\nimport * as Sentry from \"@sentry/node\";\r\n\r\nconst MC_XSTSRelyingParty = 'rp://api.minecraftservices.com/'\r\nconst XBOX_XSTSRelyingParty = 'http://xboxlive.com'\r\n\r\n// manage app on portal.azure.com\r\nexport class MicrosoftAuth {\r\n\r\n static logger: winston.Logger = winston.createLogger();\r\n\r\n constructor(\r\n private readonly requestHandlers: RequestHandlers<'generic' | 'liveLogin' | 'minecraftServices'>,\r\n private readonly redirectUri: string = process.env.MSA_REDIRECT_URI,\r\n ) {\r\n }\r\n\r\n public async newOAuthRedirect(\r\n scopes: string[],\r\n state: string,\r\n loginHint: string\r\n ) {\r\n const scope = scopes.join(\"%20\");\r\n return 'https://login.live.com/oauth20_authorize.srf?' +\r\n `client_id=${ process.env.MSA_CLIENT_ID }` +\r\n '&response_type=code' +\r\n `&redirect_uri=${ this.redirectUri }` +\r\n `&scope=${ scope }` +\r\n `&state=${ state }` +\r\n '&prompt=login&' +\r\n `login_hint=${ loginHint }`;\r\n }\r\n\r\n public async loginWithXboxCode(code: string): Promise {\r\n return await Sentry.startSpan({\r\n op: 'auth',\r\n name: 'loginWithXboxCode'\r\n }, async () => {\r\n MicrosoftAuth.logger.debug(\"loginWithXboxCode\")\r\n const form = {\r\n \"client_id\": process.env.MSA_CLIENT_ID,\r\n \"client_secret\": process.env.MSA_CLIENT_SECRET,\r\n \"code\": code,\r\n \"grant_type\": \"authorization_code\",\r\n \"redirect_uri\": this.redirectUri\r\n }\r\n return await this.authenticateXboxLiveWithFormData(form);\r\n });\r\n }\r\n\r\n async exchangeRpsTicketForIdentities(rpsTicket: string): Promise {\r\n return await Sentry.startSpan({\r\n op: 'auth',\r\n name: 'exchangeRpsTicketForIdentities'\r\n }, async () => {\r\n MicrosoftAuth.logger.debug(\"exchangeRpsTicketForIdentities\")\r\n if (!rpsTicket.startsWith(\"d=\")) {\r\n // username+password login doesn't seem to need this prefix, code auth does\r\n rpsTicket = `d=${ rpsTicket }`;\r\n }\r\n // https://user.auth.xboxlive.com/user/authenticate\r\n let userTokenResponse: XBLExchangeTokensResponse;\r\n try {\r\n userTokenResponse = await XboxLiveAuth.xbl.exchangeRpsTicketForUserToken(rpsTicket);\r\n } catch (e) {\r\n Sentry.captureException(e, {\r\n tags: {\r\n stage: 'exchangeRpsTicketForIdentities'\r\n }\r\n });\r\n throw new MSAError('exchangeRpsTicketForIdentities', e);\r\n }\r\n // console.log(\"exchangeRpsTicket\")\r\n // console.log(JSON.stringify(userTokenResponse))\r\n return {\r\n token: userTokenResponse,\r\n mc: await this.getIdentityForRelyingParty(userTokenResponse, MC_XSTSRelyingParty),\r\n xbox: await this.getIdentityForRelyingParty(userTokenResponse, XBOX_XSTSRelyingParty)\r\n };\r\n });\r\n }\r\n\r\n async getIdentityForRelyingParty(userTokenResponse: XBLExchangeTokensResponse, relyingParty: string): Promise {\r\n return await Sentry.startSpan({\r\n op: 'auth',\r\n name: 'getIdentityForRelyingParty'\r\n }, async () => {\r\n MicrosoftAuth.logger.debug(\"getIdentityForRelyingParty\")\r\n // https://xsts.auth.xboxlive.com/xsts/authorize\r\n const body = {\r\n RelyingParty: relyingParty,\r\n TokenType: \"JWT\",\r\n Properties: {\r\n SandboxId: \"RETAIL\",\r\n UserTokens: [userTokenResponse.Token]\r\n }\r\n };\r\n let authResponse: AxiosRequestConfig;\r\n try {\r\n authResponse = await this.requestHandlers.generic({\r\n method: \"POST\",\r\n url: \"https://xsts.auth.xboxlive.com/xsts/authorize\",\r\n headers: {\r\n \"Content-Type\": \"application/json\",\r\n \"Accept\": \"application/json\",\r\n /*\"x-xbl-contract-version\": 1*/\r\n },\r\n data: body\r\n });\r\n } catch (e) {\r\n Sentry.captureException(e, {\r\n tags: {\r\n stage: 'getIdentityForRelyingParty'\r\n }\r\n });\r\n throw new MSAError('getIdentityForRelyingParty', e);\r\n }\r\n return authResponse.data as XSTSResponse;\r\n });\r\n }\r\n\r\n private async authenticateXboxLiveWithFormData(form: any): Promise {\r\n return await Sentry.startSpan({\r\n op: 'auth',\r\n name: 'authenticateXboxLiveWithFormData'\r\n }, async () => {\r\n MicrosoftAuth.logger.debug(\"authenticateXboxLiveWithFormData\")\r\n let refreshResponse: AxiosResponse;\r\n try {\r\n refreshResponse = await this.requestHandlers.liveLogin({\r\n method: \"POST\",\r\n url: \"https://login.live.com/oauth20_token.srf\",\r\n headers: {\r\n \"Content-Type\": \"application/x-www-form-urlencoded\",\r\n \"Accept\": \"application/json\"\r\n },\r\n data: qs.stringify(form)\r\n });\r\n } catch (e) {\r\n Sentry.captureException(e, {\r\n tags: {\r\n stage: 'authenticateXboxLiveWithFormData'\r\n }\r\n });\r\n throw new MSAError('authenticateXboxWithFormData', e);\r\n }\r\n const refreshBody = refreshResponse.data;\r\n // console.log(\"refreshBody\");\r\n // console.log(JSON.stringify(refreshBody))\r\n\r\n // Microsoft/Xbox accessToken\r\n const xboxAccessToken = refreshBody[\"access_token\"];\r\n const xboxRefreshToken = refreshBody[\"refresh_token\"];\r\n\r\n const identityResponses = await this.exchangeRpsTicketForIdentities(xboxAccessToken);\r\n // console.log(\"identities\");\r\n // console.log(identityResponses)\r\n const mcIdentity = identityResponses.mc;\r\n const xboxIdentity = identityResponses.xbox;\r\n\r\n const userHash = mcIdentity.DisplayClaims.xui[0].uhs;\r\n const XSTSToken = mcIdentity.Token;\r\n\r\n const xboxLoginResponse = await this.loginToMinecraftWithXbox(userHash, XSTSToken);\r\n const minecraftXboxUsername = xboxLoginResponse.username;\r\n\r\n return {\r\n // Minecraft accessToken - does not return a refresh token, so need the MS one above\r\n mcAccessToken: xboxLoginResponse.access_token,\r\n msa: {\r\n auth: {\r\n accessToken: xboxAccessToken,\r\n refreshToken: xboxRefreshToken,\r\n expires: epochSeconds() + parseInt(refreshBody[\"expires_in\"]),\r\n issued: epochSeconds(),\r\n userId: refreshBody[\"user_id\"]\r\n },\r\n userToken: {\r\n token: identityResponses.token.Token,\r\n expires: toEpochSeconds(Date.parse(identityResponses.token.NotAfter)),\r\n issued: toEpochSeconds(Date.parse(identityResponses.token.IssueInstant)),\r\n userHash: identityResponses.token.DisplayClaims.xui[0].uhs\r\n },\r\n identities: {\r\n mc: {\r\n token: mcIdentity.Token,\r\n expires: toEpochSeconds(Date.parse(mcIdentity.NotAfter)),\r\n issued: toEpochSeconds(Date.parse(mcIdentity.IssueInstant)),\r\n claims: mcIdentity.DisplayClaims.xui[0]\r\n },\r\n xbox: {\r\n token: xboxIdentity.Token,\r\n expires: toEpochSeconds(Date.parse(xboxIdentity.NotAfter)),\r\n issued: toEpochSeconds(Date.parse(xboxIdentity.IssueInstant)),\r\n claims: xboxIdentity.DisplayClaims.xui[0]\r\n }\r\n }\r\n }\r\n }\r\n });\r\n }\r\n\r\n private async loginToMinecraftWithXbox(userHash: string, xstsToken: string): Promise {\r\n return await Sentry.startSpan({\r\n op: 'auth',\r\n name: 'loginToMinecraftWithXbox'\r\n }, async () => {\r\n MicrosoftAuth.logger.debug(\"loginToMinecraftWithXbox\")\r\n const body = {\r\n identityToken: `XBL3.0 x=${ userHash };${ xstsToken }`\r\n };\r\n let xboxLoginResponse: AxiosResponse;\r\n try {\r\n xboxLoginResponse = await this.requestHandlers.minecraftServices({\r\n method: \"POST\",\r\n url: \"https://api.minecraftservices.com/authentication/login_with_xbox\",\r\n headers: {\r\n \"Content-Type\": \"application/json\",\r\n \"Accept\": \"application/json\"\r\n },\r\n data: body\r\n });\r\n } catch (e) {\r\n Sentry.captureException(e, {\r\n tags: {\r\n stage: 'loginToMinecraftWithXbox'\r\n }\r\n });\r\n throw new MSAError('loginToMinecraftWithXbox', e);\r\n }\r\n const xboxLoginBody = xboxLoginResponse.data;\r\n // console.log(\"xboxLogin\")\r\n // console.log(JSON.stringify(xboxLoginBody));\r\n return xboxLoginBody as XboxLoginResponse;\r\n });\r\n }\r\n\r\n\r\n async refreshXboxAccessToken(xboxRefreshToken: string): Promise {\r\n return await Sentry.startSpan({\r\n op: 'auth',\r\n name: 'refreshXboxAccessToken'\r\n }, async () => {\r\n MicrosoftAuth.logger.debug(\"refreshXboxAccessToken\");\r\n const form = {\r\n \"client_id\": process.env.MSA_CLIENT_ID,\r\n \"client_secret\": process.env.MSA_CLIENT_SECRET,\r\n \"refresh_token\": xboxRefreshToken,\r\n \"grant_type\": \"refresh_token\",\r\n \"redirect_uri\": this.redirectUri\r\n }\r\n return await this.authenticateXboxLiveWithFormData(form);\r\n });\r\n }\r\n\r\n}\r\n"]} \ No newline at end of file +{"version":3,"file":"MicrosoftAuth.js","sourceRoot":"/","sources":["MicrosoftAuth.ts"],"names":[],"mappings":";;;;;;;;;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AAC;IAGD,KAAK,CAAC,sBAAsB,CAAC,gBAAwB;QACjD,OAAO,MAAM,MAAM,CAAC,SAAS,CAAC;YAC1B,EAAE,EAAE,MAAM;YACV,IAAI,EAAE,wBAAwB;SACjC,EAAE,KAAK,IAAI,EAAE;YACV,aAAa,CAAC,MAAM,CAAC,KAAK,CAAC,wBAAwB,CAAC,CAAC;YACrD,MAAM,IAAI,GAAG;gBACT,WAAW,EAAE,OAAO,CAAC,GAAG,CAAC,aAAa;gBACtC,eAAe,EAAE,OAAO,CAAC,GAAG,CAAC,iBAAiB;gBAC9C,eAAe,EAAE,gBAAgB;gBACjC,YAAY,EAAE,eAAe;gBAC7B,cAAc,EAAE,IAAI,CAAC,WAAW;aACnC,CAAA;YACD,OAAO,MAAM,IAAI,CAAC,gCAAgC,CAAC,IAAI,CAAC,CAAC;QAC7D,CAAC,CAAC,CAAC;IACP,CAAC;;AA5PL,sCA8PC;AA5PU,oBAAM,GAAmB,iBAAO,CAAC,YAAY,EAAE,CAAC","sourcesContent":["import * as process from \"node:process\";\r\nimport axios, { AxiosRequestConfig, AxiosResponse } from \"axios\";\r\nimport * as qs from \"qs\";\r\nimport * as XboxLiveAuth from \"@xboxreplay/xboxlive-auth\"\r\nimport { XBLExchangeTokensResponse } from \"@xboxreplay/xboxlive-auth\"\r\nimport {\r\n MicrosoftAuthInfo,\r\n MicrosoftIdentities, MicrosoftOauthResult,\r\n XboxInfo,\r\n XboxLoginResponse,\r\n XSTSResponse\r\n} from \"@mineskin/types\";\r\nimport { RequestHandlers } from \"./types/RequestHandler\";\r\nimport { MSAError } from \"./MSAError\";\r\nimport { epochSeconds, toEpochSeconds } from \"./util\";\r\nimport winston from \"winston\";\r\nimport * as Sentry from \"@sentry/node\";\r\n\r\nconst MC_XSTSRelyingParty = 'rp://api.minecraftservices.com/'\r\nconst XBOX_XSTSRelyingParty = 'http://xboxlive.com'\r\n\r\n// manage app on portal.azure.com\r\nexport class MicrosoftAuth {\r\n\r\n static logger: winston.Logger = winston.createLogger();\r\n\r\n constructor(\r\n private readonly requestHandlers: RequestHandlers<'generic' | 'liveLogin' | 'minecraftServices'>,\r\n private readonly redirectUri: string = process.env.MSA_REDIRECT_URI,\r\n ) {\r\n }\r\n\r\n public async newOAuthRedirect(\r\n scopes: string[],\r\n state: string,\r\n loginHint: string\r\n ) {\r\n const scope = scopes.join(\"%20\");\r\n return 'https://login.live.com/oauth20_authorize.srf?' +\r\n `client_id=${ process.env.MSA_CLIENT_ID }` +\r\n '&response_type=code' +\r\n `&redirect_uri=${ this.redirectUri }` +\r\n `&scope=${ scope }` +\r\n `&state=${ state }` +\r\n '&prompt=login&' +\r\n `login_hint=${ loginHint }`;\r\n }\r\n\r\n public async loginWithXboxCode(code: string): Promise {\r\n return await Sentry.startSpan({\r\n op: 'auth',\r\n name: 'loginWithXboxCode'\r\n }, async () => {\r\n MicrosoftAuth.logger.debug(\"loginWithXboxCode\")\r\n const form = {\r\n \"client_id\": process.env.MSA_CLIENT_ID,\r\n \"client_secret\": process.env.MSA_CLIENT_SECRET,\r\n \"code\": code,\r\n \"grant_type\": \"authorization_code\",\r\n \"redirect_uri\": this.redirectUri\r\n }\r\n return await this.authenticateXboxLiveWithFormData(form);\r\n });\r\n }\r\n\r\n async exchangeRpsTicketForIdentities(rpsTicket: string): Promise {\r\n return await Sentry.startSpan({\r\n op: 'auth',\r\n name: 'exchangeRpsTicketForIdentities'\r\n }, async () => {\r\n MicrosoftAuth.logger.debug(\"exchangeRpsTicketForIdentities\")\r\n if (!rpsTicket.startsWith(\"d=\")) {\r\n // username+password login doesn't seem to need this prefix, code auth does\r\n rpsTicket = `d=${ rpsTicket }`;\r\n }\r\n let userTokenResponse: XBLExchangeTokensResponse = await Sentry.startSpan({\r\n op: 'xbl',\r\n name: 'exchangeRpsTicketForUserToken'\r\n }, async () => {\r\n // https://user.auth.xboxlive.com/user/authenticate\r\n try {\r\n return await XboxLiveAuth.xbl.exchangeRpsTicketForUserToken(rpsTicket);\r\n } catch (e) {\r\n Sentry.captureException(e, {\r\n tags: {\r\n stage: 'exchangeRpsTicketForIdentities'\r\n }\r\n });\r\n throw new MSAError('exchangeRpsTicketForIdentities', e);\r\n }\r\n });\r\n // console.log(\"exchangeRpsTicket\")\r\n // console.log(JSON.stringify(userTokenResponse))\r\n return {\r\n token: userTokenResponse,\r\n mc: await this.getIdentityForRelyingParty(userTokenResponse, MC_XSTSRelyingParty),\r\n xbox: await this.getIdentityForRelyingParty(userTokenResponse, XBOX_XSTSRelyingParty)\r\n };\r\n });\r\n }\r\n\r\n async getIdentityForRelyingParty(userTokenResponse: XBLExchangeTokensResponse, relyingParty: string): Promise {\r\n return await Sentry.startSpan({\r\n op: 'auth',\r\n name: 'getIdentityForRelyingParty'\r\n }, async () => {\r\n MicrosoftAuth.logger.debug(\"getIdentityForRelyingParty\")\r\n // https://xsts.auth.xboxlive.com/xsts/authorize\r\n const body = {\r\n RelyingParty: relyingParty,\r\n TokenType: \"JWT\",\r\n Properties: {\r\n SandboxId: \"RETAIL\",\r\n UserTokens: [userTokenResponse.Token]\r\n }\r\n };\r\n let authResponse: AxiosRequestConfig;\r\n try {\r\n authResponse = await this.requestHandlers.generic({\r\n method: \"POST\",\r\n url: \"https://xsts.auth.xboxlive.com/xsts/authorize\",\r\n headers: {\r\n \"Content-Type\": \"application/json\",\r\n \"Accept\": \"application/json\",\r\n /*\"x-xbl-contract-version\": 1*/\r\n },\r\n data: body\r\n });\r\n } catch (e) {\r\n Sentry.captureException(e, {\r\n tags: {\r\n stage: 'getIdentityForRelyingParty'\r\n }\r\n });\r\n throw new MSAError('getIdentityForRelyingParty', e);\r\n }\r\n return authResponse.data as XSTSResponse;\r\n });\r\n }\r\n\r\n private async authenticateXboxLiveWithFormData(form: any): Promise {\r\n return await Sentry.startSpan({\r\n op: 'auth',\r\n name: 'authenticateXboxLiveWithFormData'\r\n }, async () => {\r\n MicrosoftAuth.logger.debug(\"authenticateXboxLiveWithFormData\")\r\n let refreshResponse: AxiosResponse;\r\n try {\r\n refreshResponse = await this.requestHandlers.liveLogin({\r\n method: \"POST\",\r\n url: \"https://login.live.com/oauth20_token.srf\",\r\n headers: {\r\n \"Content-Type\": \"application/x-www-form-urlencoded\",\r\n \"Accept\": \"application/json\"\r\n },\r\n data: qs.stringify(form)\r\n });\r\n } catch (e) {\r\n Sentry.captureException(e, {\r\n tags: {\r\n stage: 'authenticateXboxLiveWithFormData'\r\n }\r\n });\r\n throw new MSAError('authenticateXboxWithFormData', e);\r\n }\r\n const refreshBody = refreshResponse.data;\r\n // console.log(\"refreshBody\");\r\n // console.log(JSON.stringify(refreshBody))\r\n\r\n // Microsoft/Xbox accessToken\r\n const xboxAccessToken = refreshBody[\"access_token\"];\r\n const xboxRefreshToken = refreshBody[\"refresh_token\"];\r\n\r\n const identityResponses = await this.exchangeRpsTicketForIdentities(xboxAccessToken);\r\n // console.log(\"identities\");\r\n // console.log(identityResponses)\r\n const mcIdentity = identityResponses.mc;\r\n const xboxIdentity = identityResponses.xbox;\r\n\r\n const userHash = mcIdentity.DisplayClaims.xui[0].uhs;\r\n const XSTSToken = mcIdentity.Token;\r\n\r\n const xboxLoginResponse = await this.loginToMinecraftWithXbox(userHash, XSTSToken);\r\n const minecraftXboxUsername = xboxLoginResponse.username;\r\n\r\n return {\r\n // Minecraft accessToken - does not return a refresh token, so need the MS one above\r\n mcAccessToken: xboxLoginResponse.access_token,\r\n msa: {\r\n auth: {\r\n accessToken: xboxAccessToken,\r\n refreshToken: xboxRefreshToken,\r\n expires: epochSeconds() + parseInt(refreshBody[\"expires_in\"]),\r\n issued: epochSeconds(),\r\n userId: refreshBody[\"user_id\"]\r\n },\r\n userToken: {\r\n token: identityResponses.token.Token,\r\n expires: toEpochSeconds(Date.parse(identityResponses.token.NotAfter)),\r\n issued: toEpochSeconds(Date.parse(identityResponses.token.IssueInstant)),\r\n userHash: identityResponses.token.DisplayClaims.xui[0].uhs\r\n },\r\n identities: {\r\n mc: {\r\n token: mcIdentity.Token,\r\n expires: toEpochSeconds(Date.parse(mcIdentity.NotAfter)),\r\n issued: toEpochSeconds(Date.parse(mcIdentity.IssueInstant)),\r\n claims: mcIdentity.DisplayClaims.xui[0]\r\n },\r\n xbox: {\r\n token: xboxIdentity.Token,\r\n expires: toEpochSeconds(Date.parse(xboxIdentity.NotAfter)),\r\n issued: toEpochSeconds(Date.parse(xboxIdentity.IssueInstant)),\r\n claims: xboxIdentity.DisplayClaims.xui[0]\r\n }\r\n }\r\n }\r\n }\r\n });\r\n }\r\n\r\n private async loginToMinecraftWithXbox(userHash: string, xstsToken: string): Promise {\r\n return await Sentry.startSpan({\r\n op: 'auth',\r\n name: 'loginToMinecraftWithXbox'\r\n }, async () => {\r\n MicrosoftAuth.logger.debug(\"loginToMinecraftWithXbox\")\r\n const body = {\r\n identityToken: `XBL3.0 x=${ userHash };${ xstsToken }`\r\n };\r\n let xboxLoginResponse: AxiosResponse;\r\n try {\r\n xboxLoginResponse = await this.requestHandlers.minecraftServices({\r\n method: \"POST\",\r\n url: \"https://api.minecraftservices.com/authentication/login_with_xbox\",\r\n headers: {\r\n \"Content-Type\": \"application/json\",\r\n \"Accept\": \"application/json\"\r\n },\r\n data: body\r\n });\r\n } catch (e) {\r\n Sentry.captureException(e, {\r\n tags: {\r\n stage: 'loginToMinecraftWithXbox'\r\n }\r\n });\r\n throw new MSAError('loginToMinecraftWithXbox', e);\r\n }\r\n const xboxLoginBody = xboxLoginResponse.data;\r\n // console.log(\"xboxLogin\")\r\n // console.log(JSON.stringify(xboxLoginBody));\r\n return xboxLoginBody as XboxLoginResponse;\r\n });\r\n }\r\n\r\n\r\n async refreshXboxAccessToken(xboxRefreshToken: string): Promise {\r\n return await Sentry.startSpan({\r\n op: 'auth',\r\n name: 'refreshXboxAccessToken'\r\n }, async () => {\r\n MicrosoftAuth.logger.debug(\"refreshXboxAccessToken\");\r\n const form = {\r\n \"client_id\": process.env.MSA_CLIENT_ID,\r\n \"client_secret\": process.env.MSA_CLIENT_SECRET,\r\n \"refresh_token\": xboxRefreshToken,\r\n \"grant_type\": \"refresh_token\",\r\n \"redirect_uri\": this.redirectUri\r\n }\r\n return await this.authenticateXboxLiveWithFormData(form);\r\n });\r\n }\r\n\r\n}\r\n"]} \ No newline at end of file diff --git a/src/MicrosoftAuth.ts b/src/MicrosoftAuth.ts index 450f983..4749696 100644 --- a/src/MicrosoftAuth.ts +++ b/src/MicrosoftAuth.ts @@ -75,18 +75,22 @@ export class MicrosoftAuth { // username+password login doesn't seem to need this prefix, code auth does rpsTicket = `d=${ rpsTicket }`; } - // https://user.auth.xboxlive.com/user/authenticate - let userTokenResponse: XBLExchangeTokensResponse; - try { - userTokenResponse = await XboxLiveAuth.xbl.exchangeRpsTicketForUserToken(rpsTicket); - } catch (e) { - Sentry.captureException(e, { - tags: { - stage: 'exchangeRpsTicketForIdentities' - } - }); - throw new MSAError('exchangeRpsTicketForIdentities', e); - } + let userTokenResponse: XBLExchangeTokensResponse = await Sentry.startSpan({ + op: 'xbl', + name: 'exchangeRpsTicketForUserToken' + }, async () => { + // https://user.auth.xboxlive.com/user/authenticate + try { + return await XboxLiveAuth.xbl.exchangeRpsTicketForUserToken(rpsTicket); + } catch (e) { + Sentry.captureException(e, { + tags: { + stage: 'exchangeRpsTicketForIdentities' + } + }); + throw new MSAError('exchangeRpsTicketForIdentities', e); + } + }); // console.log("exchangeRpsTicket") // console.log(JSON.stringify(userTokenResponse)) return {