@metaplay/metaplay-auth 1.1.4 → 1.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/dist/auth.js +27 -19
- package/dist/auth.js.map +1 -1
- package/dist/index.js +1 -1
- package/package.json +5 -7
- package/src/auth.ts +38 -22
- package/src/index.ts +1 -1
- package/README.INTERNAL.md +0 -39
package/CHANGELOG.md
CHANGED
package/dist/auth.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-misused-promises */
|
|
2
|
-
import
|
|
2
|
+
import { createServer } from 'node:http';
|
|
3
|
+
import { toNodeListener, createApp, defineEventHandler, getQuery, sendError } from 'h3';
|
|
3
4
|
import open from 'open';
|
|
4
5
|
import { randomBytes, createHash } from 'node:crypto';
|
|
5
6
|
import jwt from 'jsonwebtoken';
|
|
6
7
|
import jwkToPem from 'jwk-to-pem';
|
|
7
8
|
import { Configuration, WellknownApi } from '@ory/client';
|
|
8
9
|
import { setSecret, getSecret, removeSecret } from './secret_store.js';
|
|
9
|
-
import { createServer } from 'net';
|
|
10
10
|
import { logger } from './logging.js';
|
|
11
11
|
// oauth2 client details (maybe move these to be discovered from some online location to make changes easier to manage?)
|
|
12
12
|
const clientId = 'c16ea663-ced3-46c6-8f85-38c9681fe1f0';
|
|
@@ -65,29 +65,36 @@ async function findAvailablePort() {
|
|
|
65
65
|
export async function loginAndSaveTokens() {
|
|
66
66
|
// Find an available port to listen on.
|
|
67
67
|
const availablePort = await findAvailablePort();
|
|
68
|
-
const app =
|
|
68
|
+
const app = createApp();
|
|
69
69
|
const redirectUri = `http://localhost:${availablePort}/callback`;
|
|
70
70
|
const { verifier, challenge } = generateCodeVerifierAndChallenge();
|
|
71
71
|
const state = randomBytes(16).toString('hex');
|
|
72
72
|
// Create a /callback endpoint that exchanges the code for tokens.
|
|
73
|
-
app.
|
|
74
|
-
//
|
|
75
|
-
const error =
|
|
76
|
-
|
|
73
|
+
app.use('/callback', defineEventHandler(async (event) => {
|
|
74
|
+
// Read the query parameters.
|
|
75
|
+
const { error, error_description: errorDescription, code } = getQuery(event);
|
|
76
|
+
// Raise an error if the query parameters contain an error message.
|
|
77
77
|
if (error) {
|
|
78
|
-
console.error(`Error logging in. Received the following error:\n\n${error}: ${errorDescription}`);
|
|
79
|
-
|
|
78
|
+
console.error(`Error logging in. Received the following error:\n\n${String(error)}: ${String(errorDescription)}`);
|
|
79
|
+
sendError(event, new Error(`Authentication failed: ${String(error)}: ${String(errorDescription)}`));
|
|
80
80
|
server.close();
|
|
81
81
|
process.exit(1);
|
|
82
82
|
}
|
|
83
|
+
// Raise an error if the query parameters do not contain a code.
|
|
84
|
+
if (typeof code !== 'string') {
|
|
85
|
+
console.error('Error logging in. No code received.');
|
|
86
|
+
sendError(event, new Error('Authentication failed: No code received.'));
|
|
87
|
+
server.close();
|
|
88
|
+
process.exit(1);
|
|
89
|
+
}
|
|
90
|
+
// Exchange the code for tokens.
|
|
83
91
|
try {
|
|
84
|
-
const code = req.query.code;
|
|
85
92
|
logger.debug(`Received callback request with code ${code}. Preparing to exchange for tokens...`);
|
|
86
93
|
const tokens = await getTokensWithAuthorizationCode(state, redirectUri, verifier, code);
|
|
87
|
-
// TODO: Could return a pre-generated HTML page instead of text?
|
|
88
|
-
res.send('Authentication successful! You can close this window.');
|
|
89
94
|
await saveTokens(tokens);
|
|
90
95
|
console.log('You are now logged in and can call the other commands.');
|
|
96
|
+
// TODO: Could return a pre-generated HTML page instead of text?
|
|
97
|
+
return 'Authentication successful! You can close this window.';
|
|
91
98
|
}
|
|
92
99
|
catch (error) {
|
|
93
100
|
if (error instanceof Error) {
|
|
@@ -98,14 +105,15 @@ export async function loginAndSaveTokens() {
|
|
|
98
105
|
server.close();
|
|
99
106
|
}
|
|
100
107
|
process.exit(0);
|
|
101
|
-
});
|
|
108
|
+
}));
|
|
102
109
|
// Start the server.
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
});
|
|
110
|
+
// We use use H3 and adapt it to Node.js's http server.
|
|
111
|
+
const server = createServer(toNodeListener(app)).listen(availablePort);
|
|
112
|
+
logger.debug(`Listening on port ${availablePort} and waiting for callback...`);
|
|
113
|
+
// Open the browser to log in.
|
|
114
|
+
const authorizationUrl = `${authorizationEndpoint}?response_type=code&client_id=${clientId}&redirect_uri=${encodeURIComponent(redirectUri)}&code_challenge=${challenge}&code_challenge_method=S256&scope=${encodeURIComponent('openid offline_access')}&state=${encodeURIComponent(state)}`;
|
|
115
|
+
console.log(`Attempting to open a browser to log in. If a browser did not open up, you can copy-paste the following URL to authenticate:\n\n${authorizationUrl}\n`);
|
|
116
|
+
void open(authorizationUrl);
|
|
109
117
|
}
|
|
110
118
|
/**
|
|
111
119
|
* Refresh access and ID token with a refresh token.
|
package/dist/auth.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAAA,2DAA2D;
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAAA,2DAA2D;AAE3D,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAA;AACxC,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,kBAAkB,EAAE,QAAQ,EAAE,SAAS,EAAE,MAAM,IAAI,CAAA;AAEvF,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACrD,OAAO,GAAG,MAAM,cAAc,CAAA;AAC9B,OAAO,QAAQ,MAAM,YAAY,CAAA;AACjC,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AACzD,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAEtE,O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,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAA;IAE/D,oBAAoB;IACpB,IAAI,CAAC,iBAAiB,EAAE;QACtB,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAA;KACtC;IAED,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,iBAAiB,CAAC,CAAC,CAAA;AACjD,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -8,7 +8,7 @@ const program = new Command();
|
|
|
8
8
|
program
|
|
9
9
|
.name('metaplay-auth')
|
|
10
10
|
.description('Authenticate with Metaplay and get AWS and Kubernetes credentials for game servers.')
|
|
11
|
-
.version('1.1.
|
|
11
|
+
.version('1.1.5')
|
|
12
12
|
.option('-d, --debug', 'enable debug output')
|
|
13
13
|
.hook('preAction', (thisCommand) => {
|
|
14
14
|
// Handle debug flag for all commands.
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@metaplay/metaplay-auth",
|
|
3
3
|
"description": "Utility CLI for authenticating with the Metaplay Auth and making authenticated calls to infrastructure endpoints.",
|
|
4
|
-
"version": "1.1.
|
|
4
|
+
"version": "1.1.5",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "SEE LICENSE IN LICENSE",
|
|
7
7
|
"homepage": "https://metaplay.io",
|
|
@@ -20,18 +20,16 @@
|
|
|
20
20
|
"@types/jsonwebtoken": "^9.0.5",
|
|
21
21
|
"@types/jwk-to-pem": "^2.0.3",
|
|
22
22
|
"@types/node": "^20.11.20",
|
|
23
|
-
"
|
|
24
|
-
"
|
|
23
|
+
"tsx": "^4.7.1",
|
|
24
|
+
"typescript": "^5.1.6"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@ory/client": "^1.6.2",
|
|
28
|
+
"commander": "^12.0.0",
|
|
29
|
+
"h3": "^1.10.2",
|
|
28
30
|
"jsonwebtoken": "^9.0.2",
|
|
29
31
|
"jwk-to-pem": "^2.0.5",
|
|
30
|
-
"commander": "^12.0.0",
|
|
31
|
-
"express": "^4.18.2",
|
|
32
|
-
"net": "^1.0.2",
|
|
33
32
|
"open": "^10.0.2",
|
|
34
|
-
"process": "^0.11.10",
|
|
35
33
|
"tslog": "^4.9.2"
|
|
36
34
|
}
|
|
37
35
|
}
|
package/src/auth.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-misused-promises */
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
import { createServer } from 'node:http'
|
|
4
|
+
import { toNodeListener, createApp, defineEventHandler, getQuery, sendError } from 'h3'
|
|
5
|
+
|
|
3
6
|
import open from 'open'
|
|
4
7
|
import { randomBytes, createHash } from 'node:crypto'
|
|
5
8
|
import jwt from 'jsonwebtoken'
|
|
6
9
|
import jwkToPem from 'jwk-to-pem'
|
|
7
10
|
import { Configuration, WellknownApi } from '@ory/client'
|
|
8
11
|
import { setSecret, getSecret, removeSecret } from './secret_store.js'
|
|
9
|
-
|
|
12
|
+
|
|
10
13
|
import { logger } from './logging.js'
|
|
11
14
|
|
|
12
15
|
// oauth2 client details (maybe move these to be discovered from some online location to make changes easier to manage?)
|
|
@@ -74,35 +77,46 @@ export async function loginAndSaveTokens () {
|
|
|
74
77
|
// Find an available port to listen on.
|
|
75
78
|
const availablePort = await findAvailablePort()
|
|
76
79
|
|
|
77
|
-
const app =
|
|
80
|
+
const app = createApp()
|
|
78
81
|
const redirectUri = `http://localhost:${availablePort}/callback`
|
|
79
82
|
const { verifier, challenge } = generateCodeVerifierAndChallenge()
|
|
80
83
|
const state = randomBytes(16).toString('hex')
|
|
81
84
|
|
|
82
85
|
// Create a /callback endpoint that exchanges the code for tokens.
|
|
83
|
-
app.
|
|
84
|
-
//
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
86
|
+
app.use('/callback', defineEventHandler(async event => {
|
|
87
|
+
// Read the query parameters.
|
|
88
|
+
const {
|
|
89
|
+
error,
|
|
90
|
+
error_description: errorDescription,
|
|
91
|
+
code
|
|
92
|
+
} = getQuery(event)
|
|
93
|
+
|
|
94
|
+
// Raise an error if the query parameters contain an error message.
|
|
88
95
|
if (error) {
|
|
89
|
-
console.error(`Error logging in. Received the following error:\n\n${error}: ${errorDescription}`)
|
|
90
|
-
|
|
96
|
+
console.error(`Error logging in. Received the following error:\n\n${String(error)}: ${String(errorDescription)}`)
|
|
97
|
+
sendError(event, new Error(`Authentication failed: ${String(error)}: ${String(errorDescription)}`))
|
|
98
|
+
server.close()
|
|
99
|
+
process.exit(1)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Raise an error if the query parameters do not contain a code.
|
|
103
|
+
if (typeof code !== 'string') {
|
|
104
|
+
console.error('Error logging in. No code received.')
|
|
105
|
+
sendError(event, new Error('Authentication failed: No code received.'))
|
|
91
106
|
server.close()
|
|
92
107
|
process.exit(1)
|
|
93
108
|
}
|
|
94
109
|
|
|
110
|
+
// Exchange the code for tokens.
|
|
95
111
|
try {
|
|
96
|
-
const code: string | undefined = req.query.code as string
|
|
97
112
|
logger.debug(`Received callback request with code ${code}. Preparing to exchange for tokens...`)
|
|
98
|
-
|
|
99
113
|
const tokens = await getTokensWithAuthorizationCode(state, redirectUri, verifier, code)
|
|
100
|
-
// TODO: Could return a pre-generated HTML page instead of text?
|
|
101
|
-
res.send('Authentication successful! You can close this window.')
|
|
102
|
-
|
|
103
114
|
await saveTokens(tokens)
|
|
104
115
|
|
|
105
116
|
console.log('You are now logged in and can call the other commands.')
|
|
117
|
+
|
|
118
|
+
// TODO: Could return a pre-generated HTML page instead of text?
|
|
119
|
+
return 'Authentication successful! You can close this window.'
|
|
106
120
|
} catch (error) {
|
|
107
121
|
if (error instanceof Error) {
|
|
108
122
|
console.error(`Error: ${error.message}`)
|
|
@@ -112,16 +126,18 @@ export async function loginAndSaveTokens () {
|
|
|
112
126
|
}
|
|
113
127
|
|
|
114
128
|
process.exit(0)
|
|
115
|
-
})
|
|
129
|
+
}))
|
|
116
130
|
|
|
117
131
|
// Start the server.
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
console.log(`Attempting to open a browser to log in. If a browser did not open up, you can copy-paste the following URL to authenticate:\n\n${authorizationUrl}\n`)
|
|
121
|
-
void open(authorizationUrl)
|
|
132
|
+
// We use use H3 and adapt it to Node.js's http server.
|
|
133
|
+
const server = createServer(toNodeListener(app)).listen(availablePort)
|
|
122
134
|
|
|
123
|
-
|
|
124
|
-
|
|
135
|
+
logger.debug(`Listening on port ${availablePort} and waiting for callback...`)
|
|
136
|
+
|
|
137
|
+
// Open the browser to log in.
|
|
138
|
+
const authorizationUrl: string = `${authorizationEndpoint}?response_type=code&client_id=${clientId}&redirect_uri=${encodeURIComponent(redirectUri)}&code_challenge=${challenge}&code_challenge_method=S256&scope=${encodeURIComponent('openid offline_access')}&state=${encodeURIComponent(state)}`
|
|
139
|
+
console.log(`Attempting to open a browser to log in. If a browser did not open up, you can copy-paste the following URL to authenticate:\n\n${authorizationUrl}\n`)
|
|
140
|
+
void open(authorizationUrl)
|
|
125
141
|
}
|
|
126
142
|
|
|
127
143
|
/**
|
package/src/index.ts
CHANGED
|
@@ -10,7 +10,7 @@ const program = new Command()
|
|
|
10
10
|
program
|
|
11
11
|
.name('metaplay-auth')
|
|
12
12
|
.description('Authenticate with Metaplay and get AWS and Kubernetes credentials for game servers.')
|
|
13
|
-
.version('1.1.
|
|
13
|
+
.version('1.1.5')
|
|
14
14
|
.option('-d, --debug', 'enable debug output')
|
|
15
15
|
.hook('preAction', (thisCommand) => {
|
|
16
16
|
// Handle debug flag for all commands.
|
package/README.INTERNAL.md
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
## Guide for package publishing
|
|
2
|
-
|
|
3
|
-
### Bump package version
|
|
4
|
-
|
|
5
|
-
There are three places where you need to bump the version number:
|
|
6
|
-
- packages.json
|
|
7
|
-
- CHANGELOG
|
|
8
|
-
- src/index.ts
|
|
9
|
-
|
|
10
|
-
### Upgrade outdated dependencies
|
|
11
|
-
|
|
12
|
-
Rememeber to upgrade any outdated dependencies whenever possible to avoid security issues and improve performance.
|
|
13
|
-
|
|
14
|
-
```bash
|
|
15
|
-
# list outdated deps
|
|
16
|
-
pnpm outdated
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
### Build the package
|
|
20
|
-
|
|
21
|
-
```bash
|
|
22
|
-
# through moon tasks (prefered)
|
|
23
|
-
# moon will automatically do lint/typecheck before building the package
|
|
24
|
-
moon run AuthCLI:build
|
|
25
|
-
|
|
26
|
-
```
|
|
27
|
-
|
|
28
|
-
### Publish package
|
|
29
|
-
|
|
30
|
-
```bash
|
|
31
|
-
# need to define the branch name (if not in main/master) for publishing new version of package
|
|
32
|
-
pnpm publish --publish-branch develop
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
## TODOs
|
|
36
|
-
|
|
37
|
-
### Automatic CI/CD
|
|
38
|
-
|
|
39
|
-
Need a GitHub Action which handles the build/publish flow when new release is ready. This can only be done once an Ory machine account is created.
|