@trycourier/cli 2.6.1 → 2.7.0
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/dist/commands/UserToken.d.ts +3 -0
- package/dist/commands/UserToken.js +71 -0
- package/dist/mappings.js +23 -0
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Text } from 'ink';
|
|
2
|
+
import _ from 'lodash';
|
|
3
|
+
import React, { useEffect, useState } from 'react';
|
|
4
|
+
import { useBoolean } from 'usehooks-ts';
|
|
5
|
+
import { useCliContext } from '../components/Context.js';
|
|
6
|
+
import Spinner from '../components/Spinner.js';
|
|
7
|
+
import UhOh from '../components/UhOh.js';
|
|
8
|
+
import { stdout } from 'process';
|
|
9
|
+
const VALID_SCOPE_PREFIXES = [
|
|
10
|
+
'read:messages',
|
|
11
|
+
'read:user-tokens',
|
|
12
|
+
'write:user-tokens',
|
|
13
|
+
'read:brands',
|
|
14
|
+
'write:brands',
|
|
15
|
+
'inbox:read:messages',
|
|
16
|
+
'inbox:write:events',
|
|
17
|
+
'read:preferences',
|
|
18
|
+
'write:preferences',
|
|
19
|
+
];
|
|
20
|
+
const UserToken = () => {
|
|
21
|
+
const { parsedParams, getJWT } = useCliContext();
|
|
22
|
+
const running = useBoolean(true);
|
|
23
|
+
const [jwt, setJWT] = useState();
|
|
24
|
+
const [error, setError] = useState();
|
|
25
|
+
const { expiration, scopes, _: [user_id], } = parsedParams;
|
|
26
|
+
useEffect(() => {
|
|
27
|
+
getUserJWT();
|
|
28
|
+
}, []);
|
|
29
|
+
const getUserJWT = async () => {
|
|
30
|
+
const exp = Number(expiration);
|
|
31
|
+
const sc = (scopes?.split(',') || []).map(s => s.trim());
|
|
32
|
+
const invalid_scopes = sc.filter(scope => !_.some(VALID_SCOPE_PREFIXES, val => scope.startsWith(val)));
|
|
33
|
+
if (!user_id) {
|
|
34
|
+
setError('No User Specified');
|
|
35
|
+
}
|
|
36
|
+
else if (!(sc && sc.length)) {
|
|
37
|
+
setError('No scopes provided. They are required');
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
else if (invalid_scopes.length) {
|
|
41
|
+
setError(`Found invalid scopes (${invalid_scopes.join(', ')})`);
|
|
42
|
+
}
|
|
43
|
+
else if (exp && Number.isNaN(exp)) {
|
|
44
|
+
setError('Not a valid number');
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
try {
|
|
48
|
+
const r = await getJWT(user_id, sc, {
|
|
49
|
+
expires_in: `${exp || 5} mins`,
|
|
50
|
+
write_brands: [],
|
|
51
|
+
});
|
|
52
|
+
setJWT(r.token);
|
|
53
|
+
}
|
|
54
|
+
catch (e) {
|
|
55
|
+
setError(String(e));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
running.setFalse();
|
|
59
|
+
};
|
|
60
|
+
if (error?.length) {
|
|
61
|
+
return React.createElement(UhOh, { text: error });
|
|
62
|
+
}
|
|
63
|
+
else if (running.value) {
|
|
64
|
+
return React.createElement(Spinner, { text: `Fetching JWT` });
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
stdout.write(jwt ?? '');
|
|
68
|
+
return React.createElement(Text, null, jwt);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
export default UserToken;
|
package/dist/mappings.js
CHANGED
|
@@ -23,6 +23,7 @@ import TenantsMembershipBulk from './commands/TenantsMembershipBulk.js';
|
|
|
23
23
|
import AudienceSearch from './commands/AudienceSearch.js';
|
|
24
24
|
import UsersTokensBulk from './commands/UsersTokensBulk.js';
|
|
25
25
|
import TenantsGetMembership from './commands/TenantsGetMembership.js';
|
|
26
|
+
import UserToken from './commands/UserToken.js';
|
|
26
27
|
const mappings = new Map();
|
|
27
28
|
export const COMMON_OPTIONS = [
|
|
28
29
|
{
|
|
@@ -529,4 +530,26 @@ mappings.set('audiences:search', {
|
|
|
529
530
|
'courier audiences:search --id 1234 --json --filename=audience1234.json',
|
|
530
531
|
],
|
|
531
532
|
});
|
|
533
|
+
mappings.set('users:jwt', {
|
|
534
|
+
params: '<user_id>',
|
|
535
|
+
instructions: 'Create a JWT for a user',
|
|
536
|
+
options: [
|
|
537
|
+
{
|
|
538
|
+
option: '--scopes',
|
|
539
|
+
value: 'Required. The scopes to attach to the JWT. We will provide the user_id scope automatically, all others will be comma seperated (https://www.courier.com/docs/reference/auth/issue-token/#available-scopes).',
|
|
540
|
+
},
|
|
541
|
+
{
|
|
542
|
+
option: '--expiration',
|
|
543
|
+
value: 'How long in minutes this JWT is valid for? Default is 5 minutes',
|
|
544
|
+
},
|
|
545
|
+
],
|
|
546
|
+
example: [
|
|
547
|
+
'courier test-user123 --scopes=inbox:read:messages,inbox:write:events',
|
|
548
|
+
'courier test-user123 --scopes=read:user-tokens,write:user-tokens --expiration=5',
|
|
549
|
+
'courier test-user123 --scopes=inbox:read:messages,inbox:write:events,read:preferences,write:preferences,read:user-tokens,write:user-tokens',
|
|
550
|
+
],
|
|
551
|
+
component: () => {
|
|
552
|
+
return React.createElement(UserToken, null);
|
|
553
|
+
},
|
|
554
|
+
});
|
|
532
555
|
export default mappings;
|
package/dist/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
let VERSION = '2.
|
|
1
|
+
let VERSION = '2.7.0';
|
|
2
2
|
export default VERSION;
|