Skip to content

Commit

Permalink
Add command to list team credentials
Browse files Browse the repository at this point in the history
Fix #129
  • Loading branch information
Mikescops committed Aug 1, 2023
1 parent dc78cdf commit efbe814
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 17 deletions.
24 changes: 12 additions & 12 deletions src/command-handlers/devices.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { connectAndPrepare } from '../database';
import { deactivateDevices, listDevices, ListDevicesOutput } from '../endpoints';
import { reset } from '../middleware';
import { askConfirmReset } from '../utils';
import { askConfirmReset, unixTimestampToHumanReadable } from '../utils';

type OutputDevice = ListDevicesOutput['devices'][number] & {
isCurrentDevice: boolean;
Expand All @@ -25,17 +25,17 @@ export async function listAllDevices(options: { json: boolean }) {
// for human consumption
result.sort((a, b) => a.lastActivityDateUnix - b.lastActivityDateUnix);

// print results
for (const device of result) {
console.log(
[
device.deviceId,
device.deviceName,
device.devicePlatform,
device.isCurrentDevice ? 'current' : 'other',
].join('\t')
);
}
const printableResult = result.map((device) => {
return {
id: device.deviceId,
name: device.deviceName,
platform: device.devicePlatform,
lastActivity: unixTimestampToHumanReadable(device.lastActivityDateUnix),
current: device.isCurrentDevice,
};
});

console.table(printableResult);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/command-handlers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './devices';
export * from './teamDevices';
42 changes: 42 additions & 0 deletions src/command-handlers/teamDevices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { connectAndPrepare } from '../database';
import { listTeamDevices } from '../endpoints';
import { unixTimestampToHumanReadable } from '../utils';

export async function listAllTeamDevices(options: { json: boolean }) {
const { db, secrets } = await connectAndPrepare({ autoSync: false });

const listTeamDevicesResponse = await listTeamDevices({ secrets });

db.close();

if (options.json) {
const result = listTeamDevicesResponse.teamDevices.map((device) => {
return {
accessKey: device.accessKey,
deviceName: device.deviceName,
platform: device.platform,
creationDateUnix: device.creationDateUnix,
updateDateUnix: device.updateDateUnix,
lastActivityDateUnix: device.lastActivityDateUnix,
};
});

console.log(JSON.stringify(result));
} else {
const result = listTeamDevicesResponse.teamDevices
.sort((a, b) => a.creationDateUnix - b.creationDateUnix)
.map((device) => {
return {
accessKey: device.accessKey,
name: device.deviceName,
platform: device.platform,
creationDate: unixTimestampToHumanReadable(device.creationDateUnix),
updateDate: unixTimestampToHumanReadable(device.updateDateUnix),
lastActivityDate: unixTimestampToHumanReadable(device.lastActivityDateUnix),
};
});

// print results
console.table(result);
}
}
1 change: 1 addition & 0 deletions src/endpoints/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export * from './requestDeviceRegistration';
export * from './getAuditLogs';
export * from './getTeamReport';
export * from './listDevices';
export * from './listTeamDevices';
export * from './deactivateDevices';
38 changes: 38 additions & 0 deletions src/endpoints/listTeamDevices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { requestUserApi } from '../requestApi';
import { Secrets } from '../types';

interface ListTeamDevicesParams {
secrets: Secrets;
}

export interface ListTeamDevicesOutput {
teamDevices: {
creationDateUnix: number;
updateDateUnix: number;
teamId: number;
deviceName: string | null;
platform: string;
version: string | null;
activated: boolean;
accessKey: string;
configVersion: number | null;
hasDraftConfig: boolean;
lastStartDateUnix: number | null;
hosting: string | null;
media: string | null;
hasLatestVersion: boolean;
hasLatestConfig: boolean;
lastActivityDateUnix: number | null;
}[];
}

export const listTeamDevices = (params: ListTeamDevicesParams) =>
requestUserApi<ListTeamDevicesOutput>({
path: 'teams/ListTeamDevices',
login: params.secrets.login,
deviceKeys: {
accessKey: params.secrets.accessKey,
secretKey: params.secrets.secretKey,
},
payload: {},
});
18 changes: 13 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from './middleware';
import { cliVersionToString, CLI_VERSION } from './cliVersion';
import { registerTeamDevice } from './endpoints/registerTeamDevice';
import { listAllDevices, removeAllDevices } from './command-handlers';
import { listAllDevices, removeAllDevices, listAllTeamDevices } from './command-handlers';
import { deactivateTeamDevice } from './endpoints/deactivateTeamDevice';

const teamDeviceCredentials = getTeamDeviceCredentialsFromEnv();
Expand Down Expand Up @@ -153,8 +153,10 @@ Use generate-credentials to generate some team credentials (requires to be a tea
);
}

teamGroup
.command('generate-credentials')
const teamCredentialsGroup = teamGroup.command('credentials').alias('c').description('Team credentials operations');

teamCredentialsGroup
.command('generate')
.option('--json', 'Output in JSON format')
.description('Generate new team credentials')
.action(async (options: { json: boolean }) => {
Expand All @@ -177,8 +179,14 @@ teamGroup
}
});

teamGroup
.command('revoke-credentials')
teamCredentialsGroup
.command('list')
.option('--json', 'Output in JSON format')
.description('List all team credentials')
.action(listAllTeamDevices);

teamCredentialsGroup
.command('revoke')
.description('Revoke credentials by access key')
.argument('<accessKey>', 'Access key of the credentials to revoke')
.action(async (accessKey: string) => {
Expand Down
5 changes: 5 additions & 0 deletions src/utils/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,8 @@ export const removeUnderscoresAndCapitalize = (string: string): string => {
.map((word) => word[0].toUpperCase() + word.slice(1))
.join(' ');
};

/** Unix timestamp to human readable string */
export const unixTimestampToHumanReadable = (timestamp: number | null): string => {
return timestamp ? new Date(timestamp * 1000).toLocaleString() : 'N/A';
};

0 comments on commit efbe814

Please sign in to comment.