Skip to content

feat(auth): ask for more OAuth scopes later with requestScopes and withScope - #42

Open
spacedevin wants to merge 1 commit into
mainfrom
feat/incremental-scopes
Open

spacedevin wants to merge 1 commit into
mainfrom
feat/incremental-scopes

Conversation

@spacedevin

Copy link
Copy Markdown
Member

Hypery's authorize endpoint already supports incremental consent (granted scopes sign in silently, new ones show consent, the grant is merged). The SDK now uses it.

  • InsufficientScopeError (code, status: 403, requiredScopes) from a Hypery 403 { error: { code: 'insufficient_scope', message: 'OAuth token requires the X scope' } }. Scopes come from error.scope(s), an RFC 6750 WWW-Authenticate: Bearer error="insufficient_scope", scope="…", or the message. parseInsufficientScope, throwIfInsufficientScope, isInsufficientScopeError.
  • requestScopes(scopes, opts): PKCE + state authorize for the union of current and requested scopes, by 'popup', 'redirect' or 'external' (a host openUrl(authorizeUrl, redirectUri) => Promise<callbackUrl | null>, e.g. Tauri's hypery_auth_start). The new token replaces the stored one.
  • withScope(scope, fn): on a thrown InsufficientScopeError or a 403 Response, requests once and retries.
  • grantedScopes on the context, and AuthTokens.scopes from the token response's scope (kept across a refresh that omits it).
  • Provider: config.openAuthUrl; context grantedScopes, requestScopes, withScope; the popup callback now relays error so a denial closes the popup.

Docs in docs/SCOPES.md (+ PROVIDER, README), example in examples/scopes/narrate.tsx, tests in src/lib/__tests__/scopes.test.ts.

Local: bun test src 121 pass, tsc --noEmit, npm run typecheck:examples, npm run test:tish, npm run build.

…thScope

Hypery's authorize endpoint already signs in silently for granted scopes,
shows consent only for new ones and merges the grant, but the SDK had no way
to use it. A 403 insufficient_scope now parses into a typed
InsufficientScopeError carrying the required scopes; requestScopes runs the
PKCE + state flow for the union of granted and new scopes by popup, redirect
or a host-supplied openAuthUrl (native shells), and replaces the stored token;
withScope retries once after the upgrade; grantedScopes exposes the token
response's scope.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant