generated from PrismarineJS/prismarine-template
-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add methods for fetching PlayFab and Minecraft Service tokens #107
Open
LucienHH
wants to merge
9
commits into
PrismarineJS:master
Choose a base branch
from
LucienHH:playfab
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6ec8d6f
Add PlayFab and Minecraft Services managers
LucienHH b0915a9
Add PlayFab example
LucienHH e36c102
Add types
LucienHH e23ede2
Format Minecraft Services response
LucienHH b44b5e6
Linting
LucienHH db139a8
Add new caches
LucienHH 9331434
Correct output
LucienHH a7cd0b4
Add docs
LucienHH 9212577
Fix some types
LucienHH File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const { Authflow, Titles } = require('prismarine-auth') | ||
|
||
const [, , username, cacheDir] = process.argv | ||
|
||
if (!username) { | ||
console.log('Usage: node deviceCode.js <username> [cacheDirectory]') | ||
process.exit(1) | ||
} | ||
|
||
async function doAuth () { | ||
const flow = new Authflow(username, cacheDir, { authTitle: Titles.MinecraftNintendoSwitch, deviceType: 'Nintendo', flow: 'live' }) | ||
|
||
const response = await flow.getPlayfabLogin() | ||
|
||
console.log(response) | ||
} | ||
|
||
module.exports = doAuth() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const { Authflow, Titles } = require('prismarine-auth') | ||
|
||
const [, , username, cacheDir] = process.argv | ||
|
||
if (!username) { | ||
console.log('Usage: node deviceCode.js <username> [cacheDirectory]') | ||
process.exit(1) | ||
} | ||
|
||
async function doAuth () { | ||
const flow = new Authflow(username, cacheDir, { authTitle: Titles.MinecraftNintendoSwitch, deviceType: 'Nintendo', flow: 'live' }) | ||
|
||
const response = await flow.getMinecraftServicesToken() | ||
|
||
console.log(response) | ||
} | ||
|
||
module.exports = doAuth() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const debug = require('debug')('prismarine-auth') | ||
const fetch = require('node-fetch') | ||
|
||
const { Endpoints } = require('../common/Constants') | ||
const { checkStatus } = require('../common/Util') | ||
|
||
class MinecraftServicesTokenManager { | ||
constructor (cache) { | ||
this.cache = cache | ||
} | ||
|
||
async getCachedAccessToken () { | ||
const { mcs: token } = await this.cache.getCached() | ||
debug('[mcs] token cache', token) | ||
|
||
if (!token) return { valid: false } | ||
|
||
const expires = new Date(token.validUntil) | ||
const remainingMs = expires - Date.now() | ||
const valid = remainingMs > 1000 | ||
return { valid, until: expires, token: token.mcToken, data: token } | ||
} | ||
|
||
async setCachedToken (data) { | ||
await this.cache.setCachedPartial(data) | ||
} | ||
|
||
async getAccessToken (sessionTicket, options = {}) { | ||
const response = await fetch(Endpoints.MinecraftServicesSessionStart, { | ||
method: 'post', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ | ||
device: { | ||
applicationType: options.applicationType ?? 'MinecraftPE', | ||
gameVersion: options.version ?? '1.20.62', | ||
id: options.deviceId ?? 'c1681ad3-415e-30cd-abd3-3b8f51e771d1', | ||
memory: options.deviceMemory ?? String(8 * (1024 * 1024 * 1024)), | ||
platform: options.platform ?? 'Windows10', | ||
playFabTitleId: options.playFabtitleId ?? '20CA2', | ||
storePlatform: options.storePlatform ?? 'uwp.store', | ||
type: options.type ?? 'Windows10' | ||
}, | ||
user: { | ||
token: sessionTicket, | ||
tokenType: 'PlayFab' | ||
} | ||
}) | ||
}).then(checkStatus) | ||
|
||
const tokenResponse = { | ||
mcToken: response.result.authorizationHeader, | ||
validUntil: response.result.validUntil, | ||
treatments: response.result.treatments, | ||
configurations: response.result.configurations, | ||
treatmentContext: response.result.treatmentContext | ||
} | ||
|
||
debug('[mc] mc-services token response', tokenResponse) | ||
|
||
await this.setCachedToken({ mcs: tokenResponse }) | ||
|
||
return response.result | ||
} | ||
} | ||
|
||
module.exports = MinecraftServicesTokenManager |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const debug = require('debug')('prismarine-auth') | ||
const fetch = require('node-fetch') | ||
|
||
const { Endpoints } = require('../common/Constants') | ||
|
||
class PlayfabTokenManager { | ||
constructor (cache) { | ||
this.cache = cache | ||
} | ||
|
||
async setCachedAccessToken (data) { | ||
await this.cache.setCachedPartial(data) | ||
} | ||
|
||
async getCachedAccessToken () { | ||
const { pfb: cache } = await this.cache.getCached() | ||
|
||
debug('[pf] token cache', cache) | ||
|
||
if (!cache) return | ||
|
||
const expires = new Date(cache.EntityToken.TokenExpiration) | ||
|
||
const remaining = expires - Date.now() | ||
|
||
const valid = remaining > 1000 | ||
|
||
return { valid, until: expires, data: cache } | ||
} | ||
|
||
async getAccessToken (xsts) { | ||
const response = await fetch(Endpoints.PlayfabLoginWithXbox, { | ||
method: 'post', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ | ||
CreateAccount: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same discussion as above, should we expose overrides to these options or leave it? |
||
EncryptedRequest: null, | ||
InfoRequestParameters: { | ||
GetCharacterInventories: false, | ||
GetCharacterList: false, | ||
GetPlayerProfile: true, | ||
GetPlayerStatistics: false, | ||
GetTitleData: false, | ||
GetUserAccountInfo: true, | ||
GetUserData: false, | ||
GetUserInventory: false, | ||
GetUserReadOnlyData: false, | ||
GetUserVirtualCurrency: false, | ||
PlayerStatisticNames: null, | ||
ProfileConstraints: null, | ||
TitleDataKeys: null, | ||
UserDataKeys: null, | ||
UserReadOnlyDataKeys: null | ||
}, | ||
PlayerSecret: null, | ||
TitleId: '20CA2', | ||
XboxToken: `XBL3.0 x=${xsts.userHash};${xsts.XSTSToken}` | ||
}) | ||
}) | ||
|
||
const data = await response.json() | ||
|
||
await this.setCachedAccessToken({ pfb: data.data }) | ||
|
||
return data.data | ||
} | ||
} | ||
|
||
module.exports = PlayfabTokenManager |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is dummy data from an existing request, should we generate our own here?