Skip to content

Commit b29ed05

Browse files
committed
feat(minecraft): support to auth with Microsoft auth, added docs
1 parent 5eb6b82 commit b29ed05

File tree

3 files changed

+55
-14
lines changed

3 files changed

+55
-14
lines changed

services/minecraft/.env

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,25 @@ OPENAI_REASONING_MODEL='deepseek-reasoner'
66
BOT_USERNAME=''
77
BOT_HOSTNAME=''
88
BOT_PORT=''
9-
BOT_PASSWORD=''
9+
# # For all online accounts, un-comment the following line to toggle Microsoft authentication.
10+
# # Link for authentication will popup when the bot starts.
11+
# #
12+
# # After signed in, according to [how Minecraft protocol was implemented](https://github.com/PrismarineJS/node-minecraft-protocol/blob/bf89f7e86526c54d8c43f555d8f6dfa4948fd2d9/src/client/microsoftAuth.js#L7-L16)
13+
# # and also, [authentication flow implemented here](https://github.com/PrismarineJS/prismarine-auth/blob/1aef6e1387d94fca839f2811d17ac6659ae556b4/src/MicrosoftAuthFlow.js#L59-L69),
14+
# # the token will be cached with [the cache IDs specified here](https://github.com/PrismarineJS/prismarine-auth/blob/1aef6e1387d94fca839f2811d17ac6659ae556b4/src/MicrosoftAuthFlow.js#L88-L93)
15+
# # in split files:
16+
# #
17+
# # - ${hash}_live-cache.json
18+
# # - ${hash}_mca-cache.json
19+
# # - ${hash}_xbl-cache.json
20+
# #
21+
# # inside of the directory provided by [`minecraft-folder-path`](https://github.com/simonmeusel/minecraft-folder-path)
22+
# #
23+
# # Linux: ~/.minecraft
24+
# # macOS: ~/Library/Application Support/minecraft
25+
# # Windows: %appdata%/.minecraft
26+
# #
27+
# # where ${hash} is the sha1 hash of the username you signing in with (as Minecraft username).
28+
# #
29+
# BOT_AUTH='microsoft'
1030
BOT_VERSION=''

services/minecraft/README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,39 @@ pnpm install
4646

4747
3. Create a `.env.local` file with your configuration:
4848

49+
> [!NOTE]
50+
> For all online accounts, un-comment the following line to toggle Microsoft authentication.
51+
> Link for authentication will popup when the bot starts.
52+
>
53+
> After signed in, according to [how Minecraft protocol was implemented](https://github.com/PrismarineJS/node-minecraft-protocol/blob/bf89f7e86526c54d8c43f555d8f6dfa4948fd2d9/src/client/microsoftAuth.js#L7-L16)
54+
> and also, [authentication flow implemented here](https://github.com/PrismarineJS/prismarine-auth/blob/1aef6e1387d94fca839f2811d17ac6659ae556b4/src/MicrosoftAuthFlow.js#L59-L69),
55+
> the token will be cached with [the cache IDs specified here](https://github.com/PrismarineJS/prismarine-auth/blob/1aef6e1387d94fca839f2811d17ac6659ae556b4/src/MicrosoftAuthFlow.js#L88-L93)
56+
> in split files:
57+
>
58+
> - `${hash}_live-cache.json`
59+
> - `${hash}_mca-cache.json`
60+
> - `${hash}_xbl-cache.json`
61+
>
62+
> inside of the directory provided by [`minecraft-folder-path`](https://github.com/simonmeusel/minecraft-folder-path)
63+
>
64+
> Linux: `~/.minecraft/nmp-cache/`
65+
> macOS: `~/Library/Application Support/minecraft/nmp-cache/`
66+
> Windows: `%appdata%/.minecraft/nmp-cache/`
67+
>
68+
> where `${hash}` is the `sha1` hash of the username you signing in with (as Minecraft username).
69+
4970
```env
5071
OPENAI_API_KEY=your_openai_api_key
5172
OPENAI_API_BASEURL=your_openai_api_baseurl
5273
5374
BOT_USERNAME=your_bot_username
5475
BOT_HOSTNAME=localhost
5576
BOT_PORT=25565
56-
BOT_PASSWORD=optional_password
77+
BOT_AUTH='microsoft' # comment if you use offline mode
5778
BOT_VERSION=1.20
5879
```
5980

60-
4. Start the bot:
81+
1. Start the bot:
6182

6283
```bash
6384
pnpm dev

services/minecraft/src/composables/config.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ interface Config {
2626
}
2727

2828
// Helper functions for type-safe environment variable parsing
29-
function getEnvVar(key: string, defaultValue: string): string {
30-
return env[key] || defaultValue
29+
function getEnvVar<V extends string>(key: string, defaultValue?: V): V | undefined {
30+
return (env[key] || defaultValue) as V | undefined
3131
}
3232

3333
function getEnvNumber(key: string, defaultValue: number): number {
@@ -64,23 +64,23 @@ export function initEnv(): void {
6464

6565
// Update config with environment variables
6666
config.openai = {
67-
apiKey: getEnvVar('OPENAI_API_KEY', defaultConfig.openai.apiKey),
68-
baseUrl: getEnvVar('OPENAI_API_BASEURL', defaultConfig.openai.baseUrl),
69-
model: getEnvVar('OPENAI_MODEL', defaultConfig.openai.model),
70-
reasoningModel: getEnvVar('OPENAI_REASONING_MODEL', defaultConfig.openai.reasoningModel),
67+
apiKey: getEnvVar('OPENAI_API_KEY', defaultConfig.openai.apiKey)!,
68+
baseUrl: getEnvVar('OPENAI_API_BASEURL', defaultConfig.openai.baseUrl)!,
69+
model: getEnvVar('OPENAI_MODEL', defaultConfig.openai.model)!,
70+
reasoningModel: getEnvVar('OPENAI_REASONING_MODEL', defaultConfig.openai.reasoningModel)!,
7171
}
7272

7373
config.bot = {
74-
username: getEnvVar('BOT_USERNAME', defaultConfig.bot.username as string),
75-
host: getEnvVar('BOT_HOSTNAME', defaultConfig.bot.host as string),
74+
username: getEnvVar('BOT_USERNAME', defaultConfig.bot.username as string)!,
75+
host: getEnvVar('BOT_HOSTNAME', defaultConfig.bot.host as string)!,
7676
port: getEnvNumber('BOT_PORT', defaultConfig.bot.port as number),
77-
password: getEnvVar('BOT_PASSWORD', defaultConfig.bot.password as string),
77+
auth: getEnvVar('BOT_AUTH', defaultConfig.bot.auth as string | undefined) as BotOptions['auth'],
7878
version: getEnvVar('BOT_VERSION', defaultConfig.bot.version as string),
7979
}
8080

8181
config.airi = {
82-
wsBaseUrl: getEnvVar('AIRI_WS_BASEURL', defaultConfig.airi.wsBaseUrl),
83-
clientName: getEnvVar('AIRI_CLIENT_NAME', defaultConfig.airi.clientName),
82+
wsBaseUrl: getEnvVar('AIRI_WS_BASEURL', defaultConfig.airi.wsBaseUrl)!,
83+
clientName: getEnvVar('AIRI_CLIENT_NAME', defaultConfig.airi.clientName)!,
8484
}
8585

8686
logger.withFields({ config }).log('Environment variables initialized')

0 commit comments

Comments
 (0)