feat: OAuth login flow with stored credentials#4
Open
timgl wants to merge 1 commit into
Open
Conversation
Introduces `posthog-definitions login` / `logout` commands. Login opens a browser, completes PKCE OAuth against the chosen PostHog host using the CIMD (Client ID Metadata Document) pattern, prompts for a project, and persists credentials to $XDG_CONFIG_HOME/posthog-definitions/config.json with mode 0600. Auth precedence in loadConfig: CLI flag > env var > stored config. POSTHOG_PERSONAL_API_KEY still wins so CI keeps working unchanged. The HTTP client now resolves the Authorization header per request via an AuthHandler, auto-refreshes OAuth tokens nearing expiry, and retries once on 401 after asking the handler to refresh. Generated-By: PostHog Code Task-Id: 23898cd3-e63c-40ac-a759-a4d69db06be9
| tokens = await performOAuthFlow({ host, clientId }); | ||
| } catch (err) { | ||
| if (err instanceof OAuthError) { | ||
| console.error(`error: ${err.message}`); |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
posthog-definitions login/logoutcommands. Login opens a browser, runs PKCE OAuth against the chosen host (US/EU/custom), prompts for a project, and saves the result to$XDG_CONFIG_HOME/posthog-definitions/config.jsonwith mode 0600.posthog/wizard. No DB row needs to be created up-front; the OAuth server fetches the metadata doc on first authorize.Authorizationheader per request through anAuthHandler, auto-refreshes tokens nearing expiry, and retries once on 401 after a refresh.POSTHOG_PERSONAL_API_KEYenv var still wins over the stored token so CI/automation keep working unchanged. Resolution: CLI flag > env var > store > default.Test plan
pnpm typecheckpassespnpm lintcleanpnpm test— all 119 unit tests passposthog-definitions loginagainsthttps://us.posthog.com, pick a project, thenapply --dry-runworks withoutPOSTHOG_*env varsposthog-definitions login --switch-projectreuses the existing tokenposthog-definitions logoutclears the filePOSTHOG_PERSONAL_API_KEYoverrides the stored OAuth tokenapply/pullCreated with PostHog Code