-
Notifications
You must be signed in to change notification settings - Fork 0
fix(cli): Make org identity canonical and forward-compatible #64
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
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| --- | ||
| "@taskless/cli": patch | ||
| --- | ||
|
|
||
| Treat the Taskless organization UUID as the one canonical identity, and become | ||
| forward-compatible with the server's coming identity cleanup. | ||
|
|
||
| - **Stop consuming `installationId`.** It is dropped from the `WhoamiOrg` type | ||
| and from the `taskless rule meta --json` output (it was already optional and | ||
| absent for public repos). The CLI never used it and it should not round-trip | ||
| through us. | ||
| - **Namespace the GitHub org id.** `WhoamiOrg.orgId` is now optional and a new | ||
| `githubOrgId?: number` is added, so a consumer reads `githubOrgId ?? orgId` | ||
| and keeps working across the server's rename. It is a convenience id, never an | ||
| identity. | ||
| - **Identify on the canonical id.** `decodeOrgId` validates the token's `id` | ||
| claim (a UUID string) and the legacy `orgId` claim (numeric, or a numeric | ||
| string) independently: a valid `id` wins, an invalid `id` still lets a valid | ||
| `orgId` through, and a non-numeric `orgId` is rejected rather than smuggled in | ||
| as an identity. PostHog then groups organizations on that canonical id. Tokens | ||
| that don't yet carry an `id` claim fall back to the numeric claim, so grouping | ||
| is unchanged until the server starts sending it. | ||
| - **Always have a known org id.** When neither a matched org nor a token claim | ||
| resolves, the canonical id falls back to the nil UUID | ||
| (`00000000-0000-0000-0000-000000000000`) instead of being absent — so the org | ||
| subject and telemetry group are always a stable, known value and unattributed | ||
| usage lands in one bucket. As a result, a write from a token missing org info | ||
| now sends the nil-UUID subject rather than failing with a re-authenticate | ||
| error. | ||
| - **Tolerate `number | string` on the legacy path.** The canonical `id` stays a | ||
| UUID `string`, but `decodeOrgId` accepts either type since we can't promise | ||
| what a legacy claim carries. |
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 |
|---|---|---|
| @@ -1,19 +1,45 @@ | ||
| import { decodeJwt } from "jose"; | ||
|
|
||
| /** | ||
| * Decode the `orgId` claim from a JWT. | ||
| * Returns `undefined` if the token is not a valid JWT or lacks the claim. | ||
| * No signature verification is performed — the server validates on API calls. | ||
| * Known fallback for the canonical org id — the nil UUID. Used when a token | ||
| * carries no resolvable org so the canonical id is never missing; downstream | ||
| * (the API org subject, PostHog grouping) always has a stable, known value | ||
| * rather than `undefined`, and "unattributed" usage lands in one known bucket. | ||
| */ | ||
| export function decodeOrgId(token: string): number | undefined { | ||
| export const NIL_ORG_ID = "00000000-0000-0000-0000-000000000000"; | ||
|
|
||
| /** | ||
| * Decode the canonical organization id from a JWT. | ||
| * | ||
| * `id` is the canonical, stable Taskless org id — a UUID string. The legacy | ||
| * numeric `orgId` claim is the fallback for tokens minted before the server | ||
| * namespaced its claims. The two are validated INDEPENDENTLY: | ||
| * - a valid `id` (a non-empty string) wins; | ||
| * - otherwise a valid `orgId` (a finite number, or a numeric string, which is | ||
| * coerced) is used — a non-numeric `orgId` is rejected, never smuggled in as | ||
| * an identity, and an invalid `id` (e.g. `""`) does not block this fallback. | ||
| * | ||
| * Returns `undefined` when neither claim is usable (or the token is not a JWT); | ||
| * callers apply `?? NIL_ORG_ID` for the known fallback. No signature | ||
| * verification is performed — the server validates on API calls. | ||
| */ | ||
| export function decodeOrgId(token: string): string | number | undefined { | ||
| let claims: { id?: unknown; orgId?: unknown }; | ||
| try { | ||
| const claims = decodeJwt(token); | ||
| const orgId = claims.orgId; | ||
| if (typeof orgId === "number" && Number.isFinite(orgId)) { | ||
| return orgId; | ||
| } | ||
| return undefined; | ||
| claims = decodeJwt(token) as { id?: unknown; orgId?: unknown }; | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| // Canonical id — a non-empty UUID string. | ||
| if (typeof claims.id === "string" && claims.id.length > 0) { | ||
| return claims.id; | ||
| } | ||
| // Legacy GitHub org id — numeric only (a numeric string is coerced). | ||
| if (typeof claims.orgId === "number" && Number.isFinite(claims.orgId)) { | ||
| return claims.orgId; | ||
| } | ||
| if (typeof claims.orgId === "string" && /^\d+$/.test(claims.orgId)) { | ||
| return Number(claims.orgId); | ||
| } | ||
| 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
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
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.