@appsemble/node-utils 0.22.1 → 0.22.2
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/README.md +3 -3
- package/authentication.d.ts +12 -0
- package/authentication.js +77 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +5 -3
package/README.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
#  Appsemble Node Utilities
|
|
2
2
|
|
|
3
3
|
> NodeJS utilities used by Appsemble internally.
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/@appsemble/node-utils)
|
|
6
|
-
[](https://gitlab.com/appsemble/appsemble/-/releases/0.22.2)
|
|
7
7
|
[](https://prettier.io)
|
|
8
8
|
|
|
9
9
|
## Table of Contents
|
|
@@ -26,5 +26,5 @@ compatibility is not guaranteed.
|
|
|
26
26
|
|
|
27
27
|
## License
|
|
28
28
|
|
|
29
|
-
[LGPL-3.0-only](https://gitlab.com/appsemble/appsemble/-/blob/0.22.
|
|
29
|
+
[LGPL-3.0-only](https://gitlab.com/appsemble/appsemble/-/blob/0.22.2/LICENSE.md) ©
|
|
30
30
|
[Appsemble](https://appsemble.com)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare const CREDENTIALS_ENV_VAR = "APPSEMBLE_CLIENT_CREDENTIALS";
|
|
2
|
+
export declare function getService(remote: string): string;
|
|
3
|
+
export declare function getKeytar(): Promise<typeof import('keytar')>;
|
|
4
|
+
/**
|
|
5
|
+
* Login to the server using OAuth2 client credentials.
|
|
6
|
+
*
|
|
7
|
+
* @param remote Host to fetch token from.
|
|
8
|
+
* @param scope The OAuth2 scope to request. This may be space separated to request
|
|
9
|
+
* multiple scopes.
|
|
10
|
+
* @param inputCredentials Client credentials passed from the command line.
|
|
11
|
+
*/
|
|
12
|
+
export declare function authenticate(remote: string, scope: string, inputCredentials: string): Promise<void>;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { AppsembleError, logger } from '@appsemble/node-utils';
|
|
2
|
+
import axios from 'axios';
|
|
3
|
+
import inquirer from 'inquirer';
|
|
4
|
+
export const CREDENTIALS_ENV_VAR = 'APPSEMBLE_CLIENT_CREDENTIALS';
|
|
5
|
+
const authorizedRemotes = new Set();
|
|
6
|
+
export function getService(remote) {
|
|
7
|
+
return `appsemble://${new URL(remote).host}`;
|
|
8
|
+
}
|
|
9
|
+
export async function getKeytar() {
|
|
10
|
+
try {
|
|
11
|
+
const { default: keytar } = await import('keytar');
|
|
12
|
+
return keytar;
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
throw new AppsembleError('Couldn’t find module keytar. Either install libsecret and reinstall @appsemble/cli, or pass --client-credentials on the command line.');
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
async function getClientCredentials(remote, inputCredentials) {
|
|
19
|
+
if (inputCredentials) {
|
|
20
|
+
return inputCredentials;
|
|
21
|
+
}
|
|
22
|
+
const envCredentials = process.env[CREDENTIALS_ENV_VAR];
|
|
23
|
+
if (envCredentials) {
|
|
24
|
+
logger.info(`Detected client credentials from ${CREDENTIALS_ENV_VAR} environment variable`);
|
|
25
|
+
return envCredentials;
|
|
26
|
+
}
|
|
27
|
+
const { findCredentials } = await getKeytar();
|
|
28
|
+
const choices = await findCredentials(getService(remote));
|
|
29
|
+
if (choices.length === 0) {
|
|
30
|
+
throw new AppsembleError(`No client credentials found. Register them using:\n\nappsemble login --remote ${remote}`);
|
|
31
|
+
}
|
|
32
|
+
let choice;
|
|
33
|
+
if (choices.length === 1) {
|
|
34
|
+
[choice] = choices;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
({ choice } = await inquirer.prompt([
|
|
38
|
+
{
|
|
39
|
+
name: 'choice',
|
|
40
|
+
message: 'Select client id to use',
|
|
41
|
+
choices: choices.map((value) => ({ name: value.account, value })),
|
|
42
|
+
type: 'list',
|
|
43
|
+
},
|
|
44
|
+
]));
|
|
45
|
+
}
|
|
46
|
+
return `${choice.account}:${choice.password}`;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Login to the server using OAuth2 client credentials.
|
|
50
|
+
*
|
|
51
|
+
* @param remote Host to fetch token from.
|
|
52
|
+
* @param scope The OAuth2 scope to request. This may be space separated to request
|
|
53
|
+
* multiple scopes.
|
|
54
|
+
* @param inputCredentials Client credentials passed from the command line.
|
|
55
|
+
*/
|
|
56
|
+
export async function authenticate(remote, scope, inputCredentials) {
|
|
57
|
+
const credentials = await getClientCredentials(remote, inputCredentials);
|
|
58
|
+
if (authorizedRemotes.has(remote)) {
|
|
59
|
+
logger.verbose(`Already logged in to ${remote}`);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
logger.verbose(`Logging in to ${remote}`);
|
|
63
|
+
const { data } = await axios.post('/oauth2/token', new URLSearchParams({ grant_type: 'client_credentials', scope }), {
|
|
64
|
+
headers: { authorization: `Basic ${Buffer.from(credentials).toString('base64')}` },
|
|
65
|
+
baseURL: remote,
|
|
66
|
+
});
|
|
67
|
+
authorizedRemotes.add(remote);
|
|
68
|
+
axios.interceptors.request.use((config) => {
|
|
69
|
+
if (config.baseURL === remote) {
|
|
70
|
+
config.headers.set('authorization', `Bearer ${data.access_token}`);
|
|
71
|
+
}
|
|
72
|
+
return config;
|
|
73
|
+
});
|
|
74
|
+
logger.info(`Logged in to ${remote} successfully`);
|
|
75
|
+
logger.verbose(`Login scope: ${scope}`);
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=authentication.js.map
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@appsemble/node-utils",
|
|
3
|
-
"version": "0.22.
|
|
3
|
+
"version": "0.22.2",
|
|
4
4
|
"description": "NodeJS utilities used by Appsemble internally.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"app",
|
|
@@ -37,8 +37,8 @@
|
|
|
37
37
|
"test": "vitest"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@appsemble/types": "0.22.
|
|
41
|
-
"@appsemble/utils": "0.22.
|
|
40
|
+
"@appsemble/types": "0.22.2",
|
|
41
|
+
"@appsemble/utils": "0.22.2",
|
|
42
42
|
"@formatjs/fast-memoize": "^2.0.0",
|
|
43
43
|
"@fortawesome/fontawesome-free": "^6.0.0",
|
|
44
44
|
"@koa/cors": "^4.0.0",
|
|
@@ -87,6 +87,8 @@
|
|
|
87
87
|
"webpack": "^5.0.0",
|
|
88
88
|
"webpack-dev-middleware": "^6.0.0",
|
|
89
89
|
"winston": "^3.0.0",
|
|
90
|
+
"keytar": "^7.0.0",
|
|
91
|
+
"inquirer": "^9.0.0",
|
|
90
92
|
"yaml": "^2.0.0",
|
|
91
93
|
"yargs": "^17.0.0"
|
|
92
94
|
},
|