-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add token commands #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+563
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,55 @@ | ||
| import { getHost, getToken } from "../auth"; | ||
| import { | ||
| createOAuthAuthorization, | ||
| createOAuthApp, | ||
| createWriteToken, | ||
| getOAuthApps, | ||
| } from "../clients/wroom"; | ||
| import { CommandError, createCommand, type CommandConfig } from "../lib/command"; | ||
| import { getRepositoryName } from "../project"; | ||
|
|
||
| const CLI_APP_NAME = "Prismic CLI"; | ||
|
|
||
| const config = { | ||
| name: "prismic token create", | ||
| description: ` | ||
| Create a new API token for a Prismic repository. | ||
|
|
||
| By default, this command reads the repository from prismic.config.json at the | ||
| project root. | ||
| `, | ||
| options: { | ||
| write: { type: "boolean", description: "Create a write token" }, | ||
| "allow-releases": { | ||
| type: "boolean", | ||
| description: "Allow access to releases (read tokens only)", | ||
| }, | ||
| repo: { type: "string", short: "r", description: "Repository domain" }, | ||
| }, | ||
| } satisfies CommandConfig; | ||
|
|
||
| export default createCommand(config, async ({ values }) => { | ||
| const { repo = await getRepositoryName(), write, "allow-releases": allowReleases } = values; | ||
|
|
||
| if (write && allowReleases) { | ||
| throw new CommandError("--allow-releases is only valid for access tokens (not with --write)"); | ||
| } | ||
|
|
||
| const token = await getToken(); | ||
| const host = await getHost(); | ||
|
|
||
| if (write) { | ||
| const writeToken = await createWriteToken(CLI_APP_NAME, { repo, token, host }); | ||
| console.info(`Token created: ${writeToken.token}`); | ||
| } else { | ||
| const scope = allowReleases ? "master+releases" : "master"; | ||
|
|
||
| // Find or create the CLI OAuth app. | ||
| const apps = await getOAuthApps({ repo, token, host }); | ||
| let app = apps.find((a) => a.name === CLI_APP_NAME); | ||
| if (!app) app = await createOAuthApp(CLI_APP_NAME, { repo, token, host }); | ||
|
|
||
| const accessToken = await createOAuthAuthorization(app.id, scope, { repo, token, host }); | ||
| console.info(`Token created: ${accessToken.token}`); | ||
| } | ||
| }); |
This file contains hidden or 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,61 @@ | ||
| import { getHost, getToken } from "../auth"; | ||
| import { | ||
| deleteOAuthAuthorization, | ||
| deleteWriteToken, | ||
| getOAuthApps, | ||
| getWriteTokens, | ||
| } from "../clients/wroom"; | ||
| import { CommandError, createCommand, type CommandConfig } from "../lib/command"; | ||
| import { getRepositoryName } from "../project"; | ||
|
|
||
| const config = { | ||
| name: "prismic token delete", | ||
| description: ` | ||
| Delete a token from a Prismic repository. | ||
|
|
||
| By default, this command reads the repository from prismic.config.json at the | ||
| project root. | ||
| `, | ||
| positionals: { | ||
| token: { description: "Token value" }, | ||
| }, | ||
| options: { | ||
| repo: { type: "string", short: "r", description: "Repository domain" }, | ||
| }, | ||
| } satisfies CommandConfig; | ||
|
|
||
| export default createCommand(config, async ({ positionals, values }) => { | ||
| const [tokenValue] = positionals; | ||
| const { repo = await getRepositoryName() } = values; | ||
|
|
||
| if (!tokenValue) { | ||
| throw new CommandError("Missing required argument: <token>"); | ||
| } | ||
|
|
||
| const token = await getToken(); | ||
| const host = await getHost(); | ||
|
|
||
| const [apps, writeTokensInfo] = await Promise.all([ | ||
| getOAuthApps({ repo, token, host }), | ||
| getWriteTokens({ repo, token, host }), | ||
| ]); | ||
|
|
||
| // Search access tokens | ||
| const accessTokenAuths = apps.flatMap((app) => app.wroom_auths); | ||
| const accessToken = accessTokenAuths.find((auth) => auth.token === tokenValue); | ||
| if (accessToken) { | ||
| await deleteOAuthAuthorization(accessToken.id, { repo, token, host }); | ||
| console.info("Token deleted"); | ||
| return; | ||
| } | ||
|
|
||
| // Search write tokens | ||
| const writeToken = writeTokensInfo.tokens.find((t) => t.token === tokenValue); | ||
| if (writeToken) { | ||
| await deleteWriteToken(writeToken.token, { repo, token, host }); | ||
| console.info("Token deleted"); | ||
| return; | ||
| } | ||
|
|
||
| throw new CommandError(`Token not found: ${tokenValue}`); | ||
| }); |
This file contains hidden or 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,67 @@ | ||
| import { getHost, getToken } from "../auth"; | ||
| import { getOAuthApps, getWriteTokens } from "../clients/wroom"; | ||
| import { createCommand, type CommandConfig } from "../lib/command"; | ||
| import { stringify } from "../lib/json"; | ||
| import { getRepositoryName } from "../project"; | ||
|
|
||
| const config = { | ||
| name: "prismic token list", | ||
| description: ` | ||
| List all API tokens for a Prismic repository. | ||
|
|
||
| By default, this command reads the repository from prismic.config.json at the | ||
| project root. | ||
| `, | ||
| options: { | ||
| json: { type: "boolean", description: "Output as JSON" }, | ||
| repo: { type: "string", short: "r", description: "Repository domain" }, | ||
| }, | ||
| } satisfies CommandConfig; | ||
|
|
||
| export default createCommand(config, async ({ values }) => { | ||
| const { repo = await getRepositoryName(), json } = values; | ||
|
|
||
| const token = await getToken(); | ||
| const host = await getHost(); | ||
|
|
||
| const [apps, writeTokensInfo] = await Promise.all([ | ||
| getOAuthApps({ repo, token, host }), | ||
| getWriteTokens({ repo, token, host }), | ||
| ]); | ||
|
|
||
| const accessTokens = apps.flatMap((app) => | ||
| app.wroom_auths.map((auth) => ({ | ||
| name: app.name, | ||
| scope: auth.scope, | ||
| token: auth.token, | ||
| createdAt: new Date(auth.created_at.$date).toISOString().split("T")[0], | ||
| })), | ||
| ); | ||
| const writeTokens = writeTokensInfo.tokens; | ||
|
|
||
| if (json) { | ||
| console.info(stringify({ accessTokens, writeTokens })); | ||
| return; | ||
| } | ||
|
|
||
| if (accessTokens.length > 0) { | ||
| console.info("ACCESS TOKENS"); | ||
| for (const accessToken of accessTokens) { | ||
| console.info(` ${accessToken.name} ${accessToken.scope} ${accessToken.token} ${accessToken.createdAt}`); | ||
| } | ||
| } else { | ||
| console.info("ACCESS TOKENS (none)"); | ||
| } | ||
|
|
||
| console.info(""); | ||
|
|
||
| if (writeTokens.length > 0) { | ||
| console.info("WRITE TOKENS"); | ||
| for (const writeToken of writeTokens) { | ||
| const date = new Date(writeToken.timestamp * 1000).toISOString().split("T")[0]; | ||
| console.info(` ${writeToken.app_name} ${writeToken.token} ${date}`); | ||
| } | ||
| } else { | ||
| console.info("WRITE TOKENS (none)"); | ||
| } | ||
| }); | ||
This file contains hidden or 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,23 @@ | ||
| import { createCommandRouter } from "../lib/command"; | ||
| import tokenCreate from "./token-create"; | ||
| import tokenDelete from "./token-delete"; | ||
| import tokenList from "./token-list"; | ||
|
|
||
| export default createCommandRouter({ | ||
| name: "prismic token", | ||
| description: "Manage API tokens for a Prismic repository.", | ||
| commands: { | ||
| list: { | ||
| handler: tokenList, | ||
| description: "List all tokens", | ||
| }, | ||
| create: { | ||
| handler: tokenCreate, | ||
| description: "Create a new token", | ||
| }, | ||
| delete: { | ||
| handler: tokenDelete, | ||
| description: "Delete a token", | ||
| }, | ||
| }, | ||
| }); |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSON output has inconsistent key naming and formats
Low Severity
The
--jsonoutput mixes two different schemas within the same response object.accessTokensentries are reformatted into camelCase keys with a formatted date string (name,createdAt), whilewriteTokensentries are passed through raw from the API with snake_case keys and a raw Unix timestamp (app_name,timestamp). Consumers of the JSON output need to handle two different naming conventions and date formats in a single response.Additional Locations (1)
src/commands/token-list.ts#L31-L39