Skip to content

Commit

Permalink
Merge pull request #26 from trycourier/bryan/adding-all
Browse files Browse the repository at this point in the history
adding all to users:jwt
  • Loading branch information
bwebs authored Nov 5, 2024
2 parents 0cef972 + d8c302a commit afe36c3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
42 changes: 36 additions & 6 deletions source/commands/UserToken.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {Text} from 'ink';
import {Box, Text} from 'ink';
import _ from 'lodash';
import React, {useEffect, useState} from 'react';
import {useBoolean} from 'usehooks-ts';
import {useCliContext} from '../components/Context.js';
import Spinner from '../components/Spinner.js';
import UhOh from '../components/UhOh.js';
import {stdout} from 'process';

interface IParams {
_: string[];
scopes?: string;
expiration?: string | number;
all?: boolean;
}

const VALID_SCOPE_PREFIXES = [
Expand All @@ -25,14 +25,19 @@ const VALID_SCOPE_PREFIXES = [
'write:preferences',
];

const ALL = VALID_SCOPE_PREFIXES.filter(s => !s.endsWith('brand'));

const UserToken = () => {
const {parsedParams, getJWT} = useCliContext();
const running = useBoolean(true);
const [jwt, setJWT] = useState<string>();
const [final_scopes, setFinalScopes] = useState<string[]>([]);
const [error, setError] = useState<string | undefined>();

const {
expiration,
scopes,
all,
_: [user_id],
} = parsedParams as IParams;

Expand All @@ -42,11 +47,13 @@ const UserToken = () => {

const getUserJWT = async () => {
const exp = Number(expiration);
const sc = (scopes?.split(',') || []).map(s => s.trim());

const scope_input = scopes?.split(',') || [];
const sc = [...new Set([...scope_input, ...(all ? ALL : [])])];
const invalid_scopes = sc.filter(
scope => !_.some(VALID_SCOPE_PREFIXES, val => scope.startsWith(val)),
);

setFinalScopes([`user_id:${user_id}`, ...sc]);
if (!user_id) {
setError('No User Specified');
} else if (!(sc && sc.length)) {
Expand Down Expand Up @@ -75,8 +82,31 @@ const UserToken = () => {
} else if (running.value) {
return <Spinner text={`Fetching JWT`} />;
} else {
stdout.write(jwt ?? '');
return <Text>{jwt}</Text>;
return (
<>
<Text>Token has the following scopes:</Text>
<Text>{final_scopes.join(' ')}</Text>
<Box
flexDirection="column"
marginY={1}
borderColor="gray"
borderStyle={'single'}
borderTop={false}
borderLeft={false}
borderRight={false}
></Box>
<Text>{jwt}</Text>
<Box
flexDirection="column"
marginY={1}
borderColor="gray"
borderStyle={'single'}
borderBottom={false}
borderLeft={false}
borderRight={false}
></Box>
</>
);
}
};

Expand Down
4 changes: 2 additions & 2 deletions source/components/Version.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {useCliContext} from './Context.js';
import constants from '../constants.js';

const Version = () => {
const {version, setVersion, map} = useCliContext();
const {version, setVersion, map, parsedParams} = useCliContext();
useEffect(() => {
getVersion();
}, []);
Expand Down Expand Up @@ -37,7 +37,7 @@ const Version = () => {
: undefined;

if (version_text?.length) {
if (map === 'upgrade') {
if (map === 'upgrade' || _.get(parsedParams, ['quiet'], false)) {
return <></>;
} else {
return (
Expand Down
6 changes: 5 additions & 1 deletion source/mappings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,11 @@ mappings.set('users:jwt', {
{
option: '--scopes',
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).',
'Required if not using all. 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).',
},
{
option: '--all',
value: 'Include all scopes besides brand scopes.',
},
{
option: '--expiration',
Expand Down

0 comments on commit afe36c3

Please sign in to comment.