Skip to content

Commit ae2e6b7

Browse files
authored
Adds entitlement and profile checking, fixes other complaints (#7)
* entitlements and profile * fix relyingParty sometimes undefined * remove Endpoints from index * fix lint * stop trying to commit my cache * examples * await the await * spice up the readme! * lint examples * Move options to function parameter * update example * lint * stupid typescript * remove jsdoc
1 parent 0330b6e commit ae2e6b7

File tree

14 files changed

+221
-109
lines changed

14 files changed

+221
-109
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
node_modules/
22
package-lock.json
33
.vscode
4+
5+
*-cache.json

README.md

Lines changed: 41 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -11,121 +11,70 @@ Quickly and easily obtain an xbox token to authenticate with Minecraft/Mojang
1111
npm install prismarine-auth
1212
```
1313

14-
## Getting A Minecraft Java Token
14+
## Usage
1515

16-
### Device Code Authentication
17-
```js
18-
const { Authflow } = require('prismarine-auth');
16+
**Parameters**
17+
- username {String} - Username for authentication
18+
- cacheDirectory {String} - Where we will store your tokens
19+
- options {Object?}
20+
- [password] {string} - If passed we will do password based authentication.
21+
- [authTitle] {string} - Required to switch to live.com authentication and do title authentication. Needed for accounts with a date of birth under 18 years old.
22+
- onMsaCode {Function} - What we should do when we get the code. Useful for passing the code to another function.
1923

20-
const doAuth = async() => {
21-
const flow = new Authflow('[email protected]', './')
22-
const XSTSToken = await flow.getMinecraftJavaToken()
23-
console.log(XSTSToken)
24-
}
24+
[View more examples](https://github.com/PrismarineJS/prismarine-auth/tree/master/examples)
2525

26-
doAuth()
27-
```
2826

29-
### Password-based Authentication
27+
### Device Code Example
3028
```js
3129
const { Authflow } = require('prismarine-auth');
3230

3331
const doAuth = async() => {
34-
const flow = new Authflow('[email protected]', './', { password: 'thisIsAFakePassword123'})
35-
const XSTSToken = await flow.getMinecraftJavaToken()
36-
console.log(XSTSToken)
32+
const flow = new Authflow('[email protected]', process.cwd(), { fetchProfile: true })
33+
const response = await flow.getMinecraftJavaToken()
34+
console.log(response)
3735
}
3836

3937
doAuth()
4038
```
4139

42-
## Getting A Minecraft Bedrock Token
43-
When authenticating for minecraft bedrock edition, you have to generate and send a signed certificate to the bedrock server.
44-
This function handles logging into Xbox Live, posting its public key to the Mojang API, and using the returned certificate signed by Mojang.
45-
46-
### Device Code Authentication
47-
```js
48-
const { Authflow } = require('prismarine-auth');
49-
const crypto = require('crypto')
50-
const curve = 'secp384r1'
51-
52-
const keypair = crypto.generateKeyPairSync('ec', { namedCurve: curve }).toString('base64')
53-
const doAuth = async() => {
54-
const flow = new Authflow('[email protected]', './')
55-
const XSTSToken = await flow.getMinecraftBedrockToken(keypair)
56-
console.log(XSTSToken)
40+
### Expected Response
41+
```json
42+
{
43+
"token": "ey....................",
44+
"entitlements": {},
45+
"profile": {
46+
"id": "b945b6ed99b548675309473a69661b9a",
47+
"name": "Usname",
48+
"skins": [ [Object] ],
49+
"capes": []
50+
}
5751
}
58-
59-
doAuth()
6052
```
6153

62-
### Password Authentication:
63-
```js
64-
const { Authflow } = require('prismarine-auth');
65-
const crypto = require('crypto')
66-
const curve = 'secp384r1'
67-
68-
const keypair = crypto.generateKeyPairSync('ec', { namedCurve: curve }).toString('base64')
54+
## Debugging
6955

70-
const doAuth = async() => {
71-
const flow = new Authflow('[email protected]', './', { password: 'thisIsAFakePassword123'})
72-
const XSTSToken = await flow.getMinecraftBedrockToken(keypair)
73-
console.log(XSTSToken)
74-
}
56+
You can enable some debugging output using the `DEBUG` enviroment variable.
7557

76-
doAuth()
58+
**Linux**
59+
```bash
60+
DEBUG="prismarine-auth" node [...]
7761
```
7862

79-
## Live.com Authentication with Titles
80-
81-
### Device Code Authentication
82-
```js
83-
const { Authflow, Titles } = require('prismarine-auth');
84-
const crypto = require('crypto')
85-
const curve = 'secp384r1'
86-
87-
const keypair = crypto.generateKeyPairSync('ec', { namedCurve: curve }).toString('base64')
88-
const doAuth = async() => {
89-
const flow = new Authflow('[email protected]', './', { authTitle: Titles.MinecraftNintendoSwitch })
90-
const XSTSToken = await flow.getMinecraftBedrockToken(keypair)
91-
console.log(XSTSToken)
92-
}
93-
94-
doAuth()
63+
**Windows Powershell**
64+
```powershell
65+
$env:DEBUG = "prismarine-auth";node [...]
9566
```
9667

97-
### Password Authentication
98-
```js
99-
const { Authflow , Titles} = require('prismarine-auth');
100-
const crypto = require('crypto')
101-
const curve = 'secp384r1'
102-
103-
const keypair = crypto.generateKeyPairSync('ec', { namedCurve: curve }).toString('base64')
68+
**Windows CMD**
69+
```cmd
70+
set DEBUG="prismarine-auth"
71+
node [...]
72+
```
10473

105-
const doAuth = async() => {
106-
const flow = new Authflow('[email protected]', './', { password: 'thisIsAFakePassword123', authTitle: Titles.MinecraftJava })
107-
const XSTSToken = await flow.getMinecraftBedrockToken(keypair)
108-
console.log(XSTSToken)
109-
}
74+
## Testing
11075

111-
doAuth()
112-
```
76+
Simply run `npm test` or `yarn test`
11377

114-
### Expected Response
115-
```php
116-
{
117-
"userXUID": "2584878536129841", // May be null
118-
"userHash": "3218841136841218711",
119-
"XSTSToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiWGJveFJlcGxheS5uZXQifQ.c2UraxPmZ4STYozrjFEW8SBqU0WjnIV0h-jjnfsKtrA",
120-
"expiresOn": "2020-04-13T05:43:32.6275675Z"
121-
}
122-
```
78+
## License
12379

124-
### Parameters
125-
Authflow class
126-
- username {String} - Username for authentication
127-
- cacheDirectory {String} - Where we will store your tokens
128-
- options {Object?}
129-
- [password] {string} - If passed we will do password based authentication.
130-
- [authTitle] {string} - Required to switch to live.com authentication and do title authentication. Needed for accounts with a date of birth under 18 years old.
131-
- onMsaCode {Function} - What we should do when we get the code. Useful for passing the code to another function.
80+
[MIT](LICENSE)

examples/bedrock/deviceCode.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { Authflow } = require('prismarine-auth')
2+
const crypto = require('crypto')
3+
const curve = 'secp384r1'
4+
5+
if (process.argv.length !== 4) {
6+
console.log('Usage: node deviceCode.js <username> <cacheDirectory>')
7+
process.exit(1)
8+
}
9+
10+
const keypair = crypto.generateKeyPairSync('ec', { namedCurve: curve }).toString('base64')
11+
const doAuth = async () => {
12+
const flow = new Authflow(process.argv[2], process.argv[3])
13+
const XSTSToken = await flow.getMinecraftBedrockToken(keypair)
14+
console.log(XSTSToken)
15+
}
16+
17+
doAuth()

examples/bedrock/password.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { Authflow } = require('prismarine-auth')
2+
const crypto = require('crypto')
3+
const curve = 'secp384r1'
4+
5+
if (process.argv.length !== 5) {
6+
console.log('Usage: node password.js <username> <password> <cacheDirectory>')
7+
process.exit(1)
8+
}
9+
10+
const keypair = crypto.generateKeyPairSync('ec', { namedCurve: curve }).toString('base64')
11+
const doAuth = async () => {
12+
const flow = new Authflow(process.argv[2], process.argv[4], { password: process.argv[3] })
13+
const XSTSToken = await flow.getMinecraftBedrockToken(keypair)
14+
console.log(XSTSToken)
15+
}
16+
17+
doAuth()

examples/deviceCode.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const { Authflow } = require('../index')
2+
3+
if (process.argv.length !== 4) {
4+
console.log('Usage: node deviceCode.js <username> <cacheDirectory>')
5+
process.exit(1)
6+
}
7+
8+
const doAuth = async () => {
9+
const flow = new Authflow(process.argv[2], process.argv[3], { fetchProfile: true, fetchEntitlements: true })
10+
const response = await flow.getMinecraftJavaToken({ fetchEntitlements: true, fetchProfile: true })
11+
console.log(response)
12+
}
13+
14+
doAuth()

examples/live/deviceCode.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { Authflow, Titles } = require('prismarine-auth')
2+
const crypto = require('crypto')
3+
const curve = 'secp384r1'
4+
5+
if (process.argv.length !== 4) {
6+
console.log('Usage: node deviceCode.js <username> <cacheDirectory>')
7+
process.exit(1)
8+
}
9+
10+
const keypair = crypto.generateKeyPairSync('ec', { namedCurve: curve }).toString('base64')
11+
const doAuth = async () => {
12+
const flow = new Authflow(process.argv[2], process.argv[3], { authTitle: Titles.MinecraftNintendoSwitch })
13+
const XSTSToken = await flow.getMinecraftBedrockToken(keypair)
14+
console.log(XSTSToken)
15+
}
16+
17+
doAuth()

examples/live/password.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { Authflow, Titles } = require('prismarine-auth')
2+
const crypto = require('crypto')
3+
const curve = 'secp384r1'
4+
5+
if (process.argv.length !== 5) {
6+
console.log('Usage: node password.js <username> <password> <cacheDirectory>')
7+
process.exit(1)
8+
}
9+
10+
const keypair = crypto.generateKeyPairSync('ec', { namedCurve: curve }).toString('base64')
11+
const doAuth = async () => {
12+
const flow = new Authflow(process.argv[2], process.argv[4], { password: process.argv[3], authTitle: Titles.MinecraftJava })
13+
const XSTSToken = await flow.getMinecraftBedrockToken(keypair)
14+
console.log(XSTSToken)
15+
}
16+
17+
doAuth()

examples/password.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const { Authflow } = require('../index')
2+
3+
if (process.argv.length !== 5) {
4+
console.log('Usage: node password.js <username> <password> <cacheDirectory>')
5+
process.exit(1)
6+
}
7+
8+
const doAuth = async () => {
9+
const flow = new Authflow(process.argv[2], process.argv[4], { password: process.argv[3] })
10+
const response = await flow.getMinecraftJavaToken()
11+
console.log(response)
12+
}
13+
14+
doAuth()

index.d.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/// <reference types="node" />
2+
3+
declare module 'prismarine-auth' {
4+
export class MicrosoftAuthFlow {
5+
constructor(username: string, cache: string, options?: MicrosoftAuthFlowOptions, codeCallback?: Function)
6+
initTokenCaches?: (username: string, cache: string) => void
7+
resetTokenCaches?: (cache: string) => boolean
8+
getMsaToken?: () => string
9+
getXboxToken?: () => string
10+
getMinecraftJavaToken?: (MinecraftJavaToken: object) => string
11+
getMinecraftBedrockToken?: (publicKey: string) => string
12+
}
13+
export interface MinecraftJavaToken {
14+
fetchEntitlements: boolean = false
15+
fetchProfile: boolean = false
16+
}
17+
18+
export interface MicrosoftAuthFlowOptions {
19+
relyingParty?: RelyingParty
20+
authTitle?: Titles
21+
password?: String
22+
}
23+
24+
export enum Titles {
25+
MinecraftNintendoSwitch = '00000000441cc96b',
26+
MinecraftJava = '00000000402b5328'
27+
}
28+
29+
export enum RelyingParty {
30+
PCXSTSRelyingParty = 'rp://api.minecraftservices.com/',
31+
BedrockXSTSRelyingParty = 'https://multiplayer.minecraft.net/',
32+
XboxXSTSRelyingParty = 'http://auth.xboxlive.com/'
33+
}
34+
35+
}

index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ if (typeof process !== 'undefined' && parseInt(process.versions.node.split('.')[
66

77
module.exports = {
88
Authflow: require('./src/MicrosoftAuthFlow'),
9-
Titles: require('./src/common/Titles'),
10-
Endpoints: require('./src/common/Constants').Endpoints
9+
Titles: require('./src/common/Titles')
1110
}

0 commit comments

Comments
 (0)