-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
As part of the new Public API efforts, here are new commands to manage API keys.
- Loading branch information
Corentin Mors
authored
Nov 19, 2024
1 parent
f4eb6d2
commit 063ca35
Showing
8 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { createPublicAPIKey, listPublicAPIKeys, revokePublicAPIKey } from '../endpoints'; | ||
import { connectAndPrepare } from '../modules/database'; | ||
|
||
export const createPublicAPIKeyHandler = async (description: string) => { | ||
const { db, localConfiguration } = await connectAndPrepare({ autoSync: false }); | ||
const key = await createPublicAPIKey({ description, localConfiguration }); | ||
db.close(); | ||
|
||
const { accessKey, secretKey, teamUuid } = key; | ||
|
||
const deviceAccountKey = `DLP_${teamUuid}_${accessKey}_${secretKey}`; | ||
|
||
return deviceAccountKey; | ||
}; | ||
|
||
export const listPublicAPIKeysHandler = async () => { | ||
const { db, localConfiguration } = await connectAndPrepare({ autoSync: false }); | ||
const publicAPIKeys = await listPublicAPIKeys({ localConfiguration }); | ||
db.close(); | ||
|
||
return publicAPIKeys; | ||
}; | ||
|
||
export const revokePublicAPIKeyHandler = async (accessKey: string) => { | ||
const { db, localConfiguration } = await connectAndPrepare({ autoSync: false }); | ||
await revokePublicAPIKey({ accessKey, localConfiguration }); | ||
db.close(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { createPublicAPIKeyHandler, listPublicAPIKeysHandler, revokePublicAPIKeyHandler } from '../../command-handlers'; | ||
import { logger } from '../../logger.js'; | ||
import { Command } from 'commander'; | ||
|
||
export const PublicAPICommands = (params: { teamGroup: Command }) => { | ||
const { teamGroup } = params; | ||
|
||
const publicAPIGroup = teamGroup.command('public-api').description('Public API operations'); | ||
|
||
publicAPIGroup | ||
.command('create-key') | ||
.description('Generate a new Public API key (Bearer token)') | ||
.argument('<description>', 'Description') | ||
.action(async (description: string) => { | ||
const publicAPIToken = await createPublicAPIKeyHandler(description); | ||
|
||
logger.success( | ||
'The new Public API key has been generated, you can use it as a Bearer token in your requests:' | ||
); | ||
logger.content(`Bearer ${publicAPIToken}`); | ||
}); | ||
|
||
publicAPIGroup | ||
.command('list-keys') | ||
.description('List all Public API keys') | ||
.option('--json', 'Output in JSON format') | ||
.action(async (options: { json: boolean }) => { | ||
const publicAPIKeys = await listPublicAPIKeysHandler(); | ||
|
||
if (options.json) { | ||
logger.content(JSON.stringify(publicAPIKeys)); | ||
} else { | ||
console.table( | ||
publicAPIKeys.publicAPIKeys.map((key) => ({ | ||
'Creation date': new Date(key.creationDateUnix).toISOString(), | ||
'Update date': new Date(key.updateDateUnix).toISOString(), | ||
'Invalidation date': key.invalidationDateUnix | ||
? new Date(key.invalidationDateUnix).toISOString() | ||
: 'N/A', | ||
'Access key': key.accessKey, | ||
Description: key.description, | ||
Origin: key.origin, | ||
Valid: key.valid ? 'Yes' : 'No', | ||
})) | ||
); | ||
} | ||
}); | ||
|
||
publicAPIGroup | ||
.command('revoke-key') | ||
.description('Revoke a Public API key') | ||
.argument('<accessKey>', 'Access key') | ||
.action(async (accessKey: string) => { | ||
await revokePublicAPIKeyHandler(accessKey); | ||
|
||
logger.success('The Public API key has been revoked'); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { requestUserApi } from '../requestApi.js'; | ||
import { LocalConfiguration } from '../types.js'; | ||
|
||
interface CreatePublicAPIKey { | ||
description: string; | ||
localConfiguration: LocalConfiguration; | ||
} | ||
|
||
export interface CreatePublicAPIKeyOutput { | ||
/** | ||
* Team UUID | ||
*/ | ||
teamUuid: string; | ||
/** | ||
* Public Api access key | ||
*/ | ||
accessKey: string; | ||
/** | ||
* Public Api secret key | ||
*/ | ||
secretKey: string; | ||
} | ||
|
||
export const createPublicAPIKey = (params: CreatePublicAPIKey) => | ||
requestUserApi<CreatePublicAPIKeyOutput>({ | ||
path: 'partners/CreatePublicAPIKey', | ||
login: params.localConfiguration.login, | ||
deviceKeys: { | ||
accessKey: params.localConfiguration.accessKey, | ||
secretKey: params.localConfiguration.secretKey, | ||
}, | ||
payload: { | ||
description: params.description, | ||
origin: 'server_cli', | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { requestUserApi } from '../requestApi.js'; | ||
import { LocalConfiguration } from '../types.js'; | ||
|
||
interface ListPublicAPIKeys { | ||
localConfiguration: LocalConfiguration; | ||
} | ||
|
||
export interface ListPublicAPIKeysOutput { | ||
/** | ||
* List of api keys | ||
*/ | ||
publicAPIKeys: { | ||
/** | ||
* The creation date | ||
*/ | ||
creationDateUnix: number; | ||
/** | ||
* The last modification date | ||
*/ | ||
updateDateUnix: number; | ||
/** | ||
* The last invalidation date | ||
*/ | ||
invalidationDateUnix: null | number; | ||
/** | ||
* The access key | ||
*/ | ||
accessKey: string; | ||
/** | ||
* The description of the api key | ||
*/ | ||
description: string; | ||
/** | ||
* Platform used to create the public api key | ||
*/ | ||
origin: string; | ||
/** | ||
* Is the public api key activated or not | ||
*/ | ||
valid: boolean; | ||
}[]; | ||
} | ||
|
||
export const listPublicAPIKeys = (params: ListPublicAPIKeys) => | ||
requestUserApi<ListPublicAPIKeysOutput>({ | ||
path: 'partners/ListPublicAPIKeys', | ||
login: params.localConfiguration.login, | ||
deviceKeys: { | ||
accessKey: params.localConfiguration.accessKey, | ||
secretKey: params.localConfiguration.secretKey, | ||
}, | ||
payload: {}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { requestUserApi } from '../requestApi.js'; | ||
import { LocalConfiguration } from '../types.js'; | ||
|
||
interface RevokePublicAPIKey { | ||
accessKey: string; | ||
localConfiguration: LocalConfiguration; | ||
} | ||
|
||
export interface RevokePublicAPIKeyOutput {} | ||
|
||
export const revokePublicAPIKey = (params: RevokePublicAPIKey) => | ||
requestUserApi<RevokePublicAPIKeyOutput>({ | ||
path: 'partners/RevokePublicAPIKey', | ||
login: params.localConfiguration.login, | ||
deviceKeys: { | ||
accessKey: params.localConfiguration.accessKey, | ||
secretKey: params.localConfiguration.secretKey, | ||
}, | ||
payload: { | ||
accessKey: params.accessKey, | ||
}, | ||
}); |