Skip to content

Commit

Permalink
Release 1.0.0 (#6)
Browse files Browse the repository at this point in the history
* Prismarine-Auth 1.0.0

* fix "nitpick"

* Expose Commonly Used Endpoints For Other Packages

* Make variable represent actual function

* fix(password authentication)

* oopsie
  • Loading branch information
kashalls authored Aug 5, 2021
1 parent dd79f9d commit 0330b6e
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 55 deletions.
6 changes: 3 additions & 3 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ assignees: ''

---

- [ ] The [readme](https://github.com/PrismarineJS/prismarine-template/README.md) doesn't contain a resolution to my issue
- [ ] The [example](https://github.com/PrismarineJS/prismarine-template/example.js) doesn't contain a resolution to my issue
- [ ] The [readme](https://github.com/PrismarineJS/prismarine-auth/README.md) doesn't contain a resolution to my issue
- [ ] The [example](https://github.com/PrismarineJS/prismarine-auth/example.js) doesn't contain a resolution to my issue

<!-- To mark the checkbox, insert `x` into it: [x] -->

## Versions
- node: #.#.#
- prismarine-template: #.#.#
- prismarine-auth: #.#.#

## Detailed description of a problem
A clear and concise description of what the problem is.
Expand Down
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/question.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ assignees: ''

---

- [ ] The [readme](https://github.com/PrismarineJS/prismarine-template/README.md) doesn't contain a resolution to my issue
- [ ] The [example](https://github.com/PrismarineJS/prismarine-template/example.js) doesn't contain a resolution to my issue
- [ ] The [readme](https://github.com/PrismarineJS/prismarine-auth/README.md) doesn't contain a resolution to my issue
- [ ] The [example](https://github.com/PrismarineJS/prismarine-auth/example.js) doesn't contain a resolution to my issue


<!-- To mark the checkbox, insert `x` into it: [x] -->
Expand Down
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# XboxLive-Auth
[![NPM version](https://img.shields.io/npm/v/prismarine-template.svg)](http://npmjs.com/package/prismarine-template)
[![Build Status](https://github.com/PrismarineJS/prismarine-template/workflows/CI/badge.svg)](https://github.com/PrismarineJS/prismarine-template/actions?query=workflow%3A%22CI%22)
# prismarine-auth
[![NPM version](https://img.shields.io/npm/v/prismarine-auth.svg)](http://npmjs.com/package/prismarine-auth)
[![Build Status](https://github.com/PrismarineJS/prismarine-auth/workflows/CI/badge.svg)](https://github.com/PrismarineJS/prismarine-auth/actions?query=workflow%3A%22CI%22)
[![Discord](https://img.shields.io/badge/chat-on%20discord-brightgreen.svg)](https://discord.gg/GsEFRM8)
[![Try it on gitpod](https://img.shields.io/badge/try-on%20gitpod-brightgreen.svg)](https://gitpod.io/#https://github.com/PrismarineJS/prismarine-template)
[![Try it on gitpod](https://img.shields.io/badge/try-on%20gitpod-brightgreen.svg)](https://gitpod.io/#https://github.com/PrismarineJS/prismarine-auth)

Quickly and easily obtain an xbox token to authenticate with Minecraft/Mojang

## Installation
```shell
npm install xboxlive-auth
npm install prismarine-auth
```

## Getting A Minecraft Java Token

### Device Code Authentication
```js
const { Authflow } = require('@prismarinejs/xboxlive-auth');
const { Authflow } = require('prismarine-auth');

const doAuth = async() => {
const flow = new Authflow('[email protected]', './')
Expand All @@ -28,7 +28,7 @@ doAuth()

### Password-based Authentication
```js
const { Authflow } = require('@prismarinejs/xboxlive-auth');
const { Authflow } = require('prismarine-auth');

const doAuth = async() => {
const flow = new Authflow('[email protected]', './', { password: 'thisIsAFakePassword123'})
Expand All @@ -45,7 +45,7 @@ This function handles logging into Xbox Live, posting its public key to the Moja

### Device Code Authentication
```js
const { Authflow } = require('@prismarinejs/xboxlive-auth');
const { Authflow } = require('prismarine-auth');
const crypto = require('crypto')
const curve = 'secp384r1'

Expand All @@ -61,7 +61,7 @@ doAuth()

### Password Authentication:
```js
const { Authflow } = require('@prismarinejs/xboxlive-auth');
const { Authflow } = require('prismarine-auth');
const crypto = require('crypto')
const curve = 'secp384r1'

Expand All @@ -80,7 +80,7 @@ doAuth()

### Device Code Authentication
```js
const { Authflow, Titles } = require('@prismarinejs/xboxlive-auth');
const { Authflow, Titles } = require('prismarine-auth');
const crypto = require('crypto')
const curve = 'secp384r1'

Expand All @@ -96,7 +96,7 @@ doAuth()

### Password Authentication
```js
const { Authflow , Titles} = require('@prismarinejs/xboxlive-auth');
const { Authflow , Titles} = require('prismarine-auth');
const crypto = require('crypto')
const curve = 'secp384r1'

Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ if (typeof process !== 'undefined' && parseInt(process.versions.node.split('.')[

module.exports = {
Authflow: require('./src/MicrosoftAuthFlow'),
Titles: require('./src/common/Titles')
Titles: require('./src/common/Titles'),
Endpoints: require('./src/common/Constants').Endpoints
}
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@prismarinejs/xboxlive-auth",
"version": "0.0.1",
"name": "prismarine-auth",
"version": "1.0.0",
"description": "A package to authenticate with xbox live and microsoft + cache tokens.",
"main": "index.js",
"scripts": {
Expand All @@ -11,7 +11,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/PrismarineJS/xboxlive-auth.git"
"url": "git+https://github.com/PrismarineJS/prismarine-auth.git"
},
"keywords": [
"prismarine",
Expand All @@ -20,15 +20,15 @@
"author": "Romain Beaumont",
"license": "MIT",
"bugs": {
"url": "https://github.com/PrismarineJS/xboxlive-auth/issues"
"url": "https://github.com/PrismarineJS/prismarine-auth/issues"
},
"homepage": "https://github.com/PrismarineJS/xboxlive-auth#readme",
"homepage": "https://github.com/PrismarineJS/prismarine-auth#readme",
"devDependencies": {
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"mocha": "^9.0.1",
"standard": "^16.0.1",
"@prismarinejs/xboxlive-auth": "file:."
"prismarine-auth": "file:."
},
"dependencies": {
"@azure/msal-node": "^1.1.0",
Expand Down
11 changes: 6 additions & 5 deletions src/MicrosoftAuthFlow.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ const fs = require('fs')
const path = require('path')
const crypto = require('crypto')

const debug = require('debug')('xboxlive-auth')
const debug = require('debug')('prismarine-auth')

const { Authentication, msalConfig } = require('./common/Constants')
const { Endpoints, msalConfig } = require('./common/Constants')

const LiveTokenManager = require('./TokenManagers/LiveTokenManager')
const JavaTokenManager = require('./TokenManagers/MinecraftJavaTokenManager')
Expand Down Expand Up @@ -33,6 +33,7 @@ class MicrosoftAuthFlow {
this.options = options
this.initTokenCaches(username, cache)
this.codeCallback = codeCallback
this.relyingParty = options.relyingParty ?? Endpoints.BedrockXSTSRelyingParty
}

initTokenCaches (username, cache) {
Expand Down Expand Up @@ -65,7 +66,7 @@ class MicrosoftAuthFlow {
}

const keyPair = crypto.generateKeyPairSync('ec', { namedCurve: 'P-256' })
this.xbl = new XboxTokenManager(Authentication.BedrockXSTSRelyingParty, keyPair, cachePaths.xbl)
this.xbl = new XboxTokenManager(keyPair, cachePaths.xbl)
this.mba = new BedrockTokenManager(cachePaths.bed)
this.mca = new JavaTokenManager(cachePaths.mca)
}
Expand Down Expand Up @@ -138,7 +139,7 @@ class MicrosoftAuthFlow {
debug('[mc] Using existing tokens')
return this.mca.getCachedAccessToken().token
} else {
this.xbl.relyingParty = Authentication.PCXSTSRelyingParty
this.xbl.relyingParty = Endpoints.PCXSTSRelyingParty
debug('[mc] Need to obtain tokens')
return await retry(async () => {
const xsts = await this.getXboxToken()
Expand All @@ -156,7 +157,7 @@ class MicrosoftAuthFlow {
return this.mba.getCachedAccessToken().chain
} else {
if (!publicKey) throw new Error('Need to specifiy a ECDH x509 URL encoded public key')
this.xbl.relyingParty = Authentication.BedrockXSTSRelyingParty
this.xbl.relyingParty = Endpoints.BedrockXSTSRelyingParty
debug('[mc] Need to obtain tokens')
return await retry(async () => {
const xsts = await this.getXboxToken()
Expand Down
10 changes: 5 additions & 5 deletions src/TokenManagers/LiveTokenManager.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const fs = require('fs')
const debug = require('debug')('xboxlive-auth')
const debug = require('debug')('prismarine-auth')
const fetch = require('node-fetch')

const { Authentication } = require('../common/Constants')
const { Endpoints } = require('../common/Constants')
const { checkStatus } = require('../common/Util')

class LiveTokenManager {
Expand Down Expand Up @@ -58,7 +58,7 @@ class LiveTokenManager {
credentials: 'include' // This cookie handler does not work on node-fetch ...
}

const token = await fetch(Authentication.LiveTokenRequest, codeRequest).then(checkStatus)
const token = await fetch(Endpoints.LiveTokenRequest, codeRequest).then(checkStatus)
this.updateCache(token)
return token
}
Expand Down Expand Up @@ -100,7 +100,7 @@ class LiveTokenManager {

const cookies = []

const res = await fetch(Authentication.LiveDeviceCodeRequest, codeRequest)
const res = await fetch(Endpoints.LiveDeviceCodeRequest, codeRequest)
.then(res => {
if (res.status !== 200) {
res.text().then(console.warn)
Expand Down Expand Up @@ -136,7 +136,7 @@ class LiveTokenManager {
}).toString()
}

const token = await fetch(Authentication.LiveTokenRequest + '?client_id=' + this.clientId, verifi)
const token = await fetch(Endpoints.LiveTokenRequest + '?client_id=' + this.clientId, verifi)
.then(res => res.json()).then(res => {
if (res.error) {
if (res.error === 'authorization_pending') {
Expand Down
6 changes: 3 additions & 3 deletions src/TokenManagers/MinecraftBedrockTokenManager.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const fs = require('fs')
const path = require('path')
const debug = require('debug')('xboxlive-auth')
const debug = require('debug')('prismarine-auth')
const fetch = require('node-fetch')

const { Authentication } = require('../common/Constants')
const { Endpoints } = require('../common/Constants')
const { checkStatus } = require('../common/Util')

class BedrockTokenManager {
Expand Down Expand Up @@ -56,7 +56,7 @@ class BedrockTokenManager {
'User-Agent': 'node-minecraft-protocol',
Authorization: `XBL3.0 x=${xsts.userHash};${xsts.XSTSToken}`
}
const MineServicesResponse = await fetch(Authentication.BedrockAuth, {
const MineServicesResponse = await fetch(Endpoints.BedrockAuth, {
method: 'post',
headers,
body: JSON.stringify({ identityPublicKey: clientPublicKey })
Expand Down
6 changes: 3 additions & 3 deletions src/TokenManagers/MinecraftJavaTokenManager.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const fs = require('fs')
const path = require('path')
const debug = require('debug')('xboxlive-auth')
const debug = require('debug')('prismarine-auth')
const fetch = require('node-fetch')

const { Authentication } = require('../common/Constants')
const { Endpoints } = require('../common/Constants')
const { checkStatus } = require('../common/Util')

class MinecraftJavaTokenManager {
Expand Down Expand Up @@ -53,7 +53,7 @@ class MinecraftJavaTokenManager {
}
}

const MineServicesResponse = await fetch(Authentication.MinecraftServicesLogWithXbox, {
const MineServicesResponse = await fetch(Endpoints.MinecraftServicesLogWithXbox, {
method: 'post',
...getFetchOptions,
body: JSON.stringify({ identityToken: `XBL3.0 x=${xsts.userHash};${xsts.XSTSToken}` })
Expand Down
2 changes: 1 addition & 1 deletion src/TokenManagers/MsaTokenManager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('fs')
const path = require('path')
const msal = require('@azure/msal-node')
const debug = require('debug')('xboxlive-auth')
const debug = require('debug')('prismarine-auth')

class MsaTokenManager {
constructor (msalConfig, scopes, cacheLocation) {
Expand Down
28 changes: 15 additions & 13 deletions src/TokenManagers/XboxTokenManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ const path = require('path')
const crypto = require('crypto')

const XboxLiveAuth = require('@xboxreplay/xboxlive-auth')
const debug = require('debug')('xboxlive-auth')
const debug = require('debug')('prismarine-auth')
const { SmartBuffer } = require('smart-buffer')
const jose = require('jose-node-cjs-runtime/jwk/from_key_like')
const fetch = require('node-fetch')

const { Authentication } = require('../common/Constants')
const { Endpoints } = require('../common/Constants')
const { checkStatus } = require('../common/Util')

const UUID = require('uuid-1345')
const nextUUID = () => UUID.v3({ namespace: '6ba7b811-9dad-11d1-80b4-00c04fd430c8', name: Date.now().toString() })

// Manages Xbox Live tokens for xboxlive.com
class XboxTokenManager {
constructor (relyingParty, ecKey, cacheLocation) {
this.relyingParty = relyingParty
constructor (ecKey, cacheLocation) {
this.relyingParty = null
this.key = ecKey
jose.fromKeyLike(ecKey.publicKey).then(jwk => {
this.jwk = { ...jwk, alg: 'ES256', use: 'sig' }
Expand Down Expand Up @@ -126,11 +126,13 @@ class XboxTokenManager {
try {
const preAuthResponse = await XboxLiveAuth.preAuth()
const logUserResponse = await XboxLiveAuth.logUser(preAuthResponse, { email, password })
const exchangeRpsTicketForUserToken = await this.getUserToken(logUserResponse.access_token, true)
const xsts = await this.getXSTSToken(exchangeRpsTicketForUserToken)
const xblUserToken = await XboxLiveAuth.exchangeRpsTicketForUserToken(logUserResponse.access_token)
this.setCachedUserToken(xblUserToken)
debug('[xbl] user token:', xblUserToken)
const xsts = await this.getXSTSToken(xblUserToken)
return xsts
} catch (error) {
debug('[XboxLive-Auth] Authentication using a password has failed.')
debug('Authentication using a password has failed.')
debug(error)
throw error
}
Expand Down Expand Up @@ -168,11 +170,11 @@ class XboxTokenManager {
}

const body = JSON.stringify(payload)
const signature = this.sign(Authentication.XstsAuthorize, '', body).toString('base64')
const signature = this.sign(Endpoints.XstsAuthorize, '', body).toString('base64')

const headers = { ...this.headers, Signature: signature }

const ret = await fetch(Authentication.XstsAuthorize, { method: 'post', headers, body }).then(checkStatus)
const ret = await fetch(Endpoints.XstsAuthorize, { method: 'post', headers, body }).then(checkStatus)
const xsts = {
userXUID: ret.DisplayClaims.xui[0].xid || null,
userHash: ret.DisplayClaims.xui[0].uhs,
Expand Down Expand Up @@ -204,10 +206,10 @@ class XboxTokenManager {
}

const body = JSON.stringify(payload)
const signature = this.sign(Authentication.XboxDeviceAuth, '', body).toString('base64')
const signature = this.sign(Endpoints.XboxDeviceAuth, '', body).toString('base64')
const headers = { ...this.headers, Signature: signature }

const ret = await fetch(Authentication.XboxDeviceAuth, { method: 'post', headers, body }).then(checkStatus)
const ret = await fetch(Endpoints.XboxDeviceAuth, { method: 'post', headers, body }).then(checkStatus)
debug('Xbox Device Token', ret)
return ret.Token
}
Expand All @@ -226,11 +228,11 @@ class XboxTokenManager {
TokenType: 'JWT'
}
const body = JSON.stringify(payload)
const signature = this.sign(Authentication.XboxTitleAuth, '', body).toString('base64')
const signature = this.sign(Endpoints.XboxTitleAuth, '', body).toString('base64')

const headers = { ...this.headers, Signature: signature }

const ret = await fetch(Authentication.XboxTitleAuth, { method: 'post', headers, body }).then(checkStatus)
const ret = await fetch(Endpoints.XboxTitleAuth, { method: 'post', headers, body }).then(checkStatus)
debug('Xbox Title Token', ret)
return ret.Token
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/Constants.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
Authentication: {
Endpoints: {
PCXSTSRelyingParty: 'rp://api.minecraftservices.com/',
BedrockXSTSRelyingParty: 'https://multiplayer.minecraft.net/',
BedrockAuth: 'https://multiplayer.minecraft.net/authentication',
Expand Down
2 changes: 1 addition & 1 deletion src/common/Util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const debug = require('debug')('xboxlive-auth')
const debug = require('debug')('prismarine-auth')

function checkStatus (res) {
if (res.ok) { // res.status >= 200 && res.status < 300
Expand Down

0 comments on commit 0330b6e

Please sign in to comment.