-
Notifications
You must be signed in to change notification settings - Fork 94
feat(stdio): reuse Sentry CLI auth tokens #831
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
Open
dcramer
wants to merge
8
commits into
main
Choose a base branch
from
feat/stdio-cli-auth-fallback
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e94a08d
feat(stdio): Reuse Sentry CLI auth tokens
dcramer 63ea5d3
fix(stdio): Address auth review feedback
dcramer 6f0c527
docs(stdio): Link Sentry CLI auth flow
dcramer 3d0ea98
fix(stdio): Narrow implicit auth inputs
dcramer 1cea660
docs(stdio): Mention CLI auth fallback in site docs
dcramer f20b659
ref(stdio): Share CLI auth DB reader
dcramer 7695155
test(stdio): Cover auth token precedence
dcramer 746cb31
ref(stdio): Share better-sqlite3 typings
dcramer 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
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
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,64 @@ | ||
| import Database from "better-sqlite3"; | ||
| import { homedir } from "node:os"; | ||
| import { join } from "node:path"; | ||
|
|
||
| const TOKEN_EXPIRY_BUFFER_MS = 5 * 60 * 1000; | ||
|
|
||
| export type SentryCliAccessTokenSource = "sentry_cli_db"; | ||
|
|
||
| type AuthRow = { | ||
| token: string | null; | ||
| expires_at: number | null; | ||
| }; | ||
|
|
||
| export function normalizeAccessToken( | ||
| token?: string | null, | ||
| ): string | undefined { | ||
| const trimmed = token?.trim(); | ||
| return trimmed ? trimmed : undefined; | ||
| } | ||
|
|
||
| export function getSentryCliDbPath(homeDir = homedir()): string { | ||
| return join(homeDir, ".sentry", "cli.db"); | ||
| } | ||
|
|
||
| export function readAccessTokenFromSentryCliDb({ | ||
| nowMs, | ||
| homeDir, | ||
| }: { | ||
| nowMs: number; | ||
| homeDir?: string; | ||
| }): string | undefined { | ||
| const cliDbPath = getSentryCliDbPath(homeDir); | ||
|
|
||
| try { | ||
| const db = new Database(cliDbPath, { | ||
| readonly: true, | ||
| fileMustExist: true, | ||
| }); | ||
|
|
||
| try { | ||
| const row = db | ||
| .prepare("SELECT token, expires_at FROM auth WHERE id = 1") | ||
| .get() as AuthRow | undefined; | ||
| const token = normalizeAccessToken(row?.token); | ||
|
|
||
| if (!token) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if ( | ||
| typeof row?.expires_at === "number" && | ||
| nowMs + TOKEN_EXPIRY_BUFFER_MS >= row.expires_at | ||
| ) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return token; | ||
| } finally { | ||
| db.close(); | ||
| } | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| } |
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
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,20 @@ | ||
| declare module "better-sqlite3" { | ||
| export type DatabaseOptions = { | ||
| readonly?: boolean; | ||
| fileMustExist?: boolean; | ||
| }; | ||
|
|
||
| export type Statement<Result = Record<string, unknown>> = { | ||
| get(...params: unknown[]): Result | undefined; | ||
| run(...params: unknown[]): unknown; | ||
| }; | ||
|
|
||
| export default class Database { | ||
| constructor(filename: string, options?: DatabaseOptions); | ||
| prepare<Result = Record<string, unknown>>( | ||
| source: string, | ||
| ): Statement<Result>; | ||
| exec(source: string): this; | ||
| close(): this; | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.