Pkg/auth/0.5 - #46
Conversation
… and update references
…hooks, and implement role-based access control in AuthProvider
…el for consistency
… and update references feat: introduce MayRLabsUser utility class, add auth lifecycle event hooks, and implement role-based access control in AuthProvider
There was a problem hiding this comment.
Pull request overview
This PR bumps @mayrlabs/auth and @mayrlabs/auth-nextjs to 0.5.0, standardizes exported payload types, and adds new security/auth utilities (PKCE + CSRF state) alongside a higher-level AuthUser model used by the Next.js wrapper.
Changes:
- Rename
MayRLabsAuth*Payloadtypes toAuth*Payloadand align user claims shape with Account Center (id/username + nullable names). - Add core utilities:
AuthUser, PKCE helpers (generateCodeVerifier,generateCodeChallenge) and random string generation; export them from the package root. - Update Next.js integration to add CSRF state verification, auth lifecycle hooks, role-gated
AuthProvider, and optional session rotation.
Reviewed changes
Copilot reviewed 21 out of 21 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/auth/src/types.ts | Renames/reshapes JWT payload types; adds optional events hooks to ClientConfig. |
| packages/auth/src/index.ts | Exports new core/utils module from package entrypoint. |
| packages/auth/src/core/utils.ts | Introduces AuthUser wrapper + PKCE/random helpers. |
| packages/auth/src/core/_utils.ts | Adds internal crypto-random string generator + charsets. |
| packages/auth/src/core/client.ts | Extends getLoginUrl to accept additional query params. |
| packages/auth/src/core/constants.ts | Splits session cookie constants into client vs issuer keys. |
| packages/auth/src/core/base.ts | Updates payload type names across verification APIs. |
| packages/auth/src/core/issuer.ts | Updates payload type names across signing APIs. |
| packages/auth/README.md | Documents new model/hooks/CSRF/PKCE and updated types. |
| packages/auth/docs/pkce.md | Adds detailed PKCE documentation. |
| packages/auth/package.json | Version bump to 0.5.0. |
| packages/auth/CHANGELOG.md | Changelog entry for 0.5.0 features/breaking changes. |
| packages/auth-nextjs/src/types.ts | Updates Next.js wrapper types to return AuthUser and adds new options. |
| packages/auth-nextjs/src/client/index.tsx | Implements CSRF state verification, redirect params, session rotation, role gating. |
| packages/auth-nextjs/src/client/provider.tsx | Updates client hook to expose AuthUser instance. |
| packages/auth-nextjs/src/issuer/index.ts | Switches issuer session key default and returns AuthUser. |
| packages/auth-nextjs/src/index.test.ts | Adjusts one cookie-key assertion (but broader changes add new untested paths). |
| packages/auth-nextjs/vitest.setup.ts | Updates test session key default. |
| packages/auth-nextjs/README.md | Documents new role gating, CSRF, redirects, session sliding. |
| packages/auth-nextjs/package.json | Version bump to 0.5.0 and uses workspace dependency on @mayrlabs/auth. |
| packages/auth-nextjs/CHANGELOG.md | Changelog entry for 0.5.0 and session key changes. |
Comments suppressed due to low confidence (1)
packages/auth-nextjs/src/client/index.tsx:259
- CSRF state verification in
handleCallbackrequires both astatequery param and amayrlabs-auth-statecookie, butgetUserOrRedirect()redirects withsetup.getLoginUrl()(no state, no cookie). That will cause legitimate SSO callbacks to fail withCLIENT_CSRF_MISMATCHwhen login is initiated via Server Components usinggetUserOrRedirect. Consider generating/storing state (viacookies().set) before callingredirect(...), or routing unauthenticated flows through a helper that can set the state cookie.
const getUserOrRedirect = async (): Promise<AuthUser> => {
const user = await getUser();
if (!user) return redirect(setup.getLoginUrl());
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| function b64url(buffer: Uint8Array): string { | ||
| const binary = String.fromCharCode(...buffer); | ||
| const base64 = btoa(binary); | ||
|
|
||
| return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""); | ||
| } |
There was a problem hiding this comment.
btoa is not available in Node.js, so generateCodeChallenge() will throw at runtime in server contexts (e.g. Next.js route handlers). Use a base64url encoder that works in both browser and Node (e.g. Buffer.from(bytes).toString("base64") when Buffer exists, otherwise btoa), or centralize base64url encoding in a shared utility.
| const user = await getUser(); | ||
|
|
||
| if (!user) return redirect(setup.getLoginUrl()); | ||
|
|
There was a problem hiding this comment.
AuthProvider redirects unauthenticated users with redirect(setup.getLoginUrl()), which does not set the CSRF state cookie or add the state param. With state verification enabled in handleCallback, any login initiated by AuthProvider will fail on callback. Generate/store the state cookie and include state in the login URL before redirecting, or refactor so AuthProvider uses a state-aware redirect helper.
| export class AuthUser { | ||
| constructor(private readonly payload: AuthUserPayload) {} | ||
|
|
||
| /** | ||
| * The unique identifier for the user. | ||
| */ | ||
| get id(): string { | ||
| return this.payload.id; | ||
| } | ||
|
|
||
| /** | ||
| * The user's email address. | ||
| */ | ||
| get email(): string { |
There was a problem hiding this comment.
New public utilities (AuthUser, generateCodeVerifier, generateCodeChallenge, generateRandomString) are introduced here, but there are no accompanying unit tests in packages/auth/src/core/*.test.ts. Add tests to validate PKCE challenge generation (known verifier -> expected challenge), verifier charset/length constraints, and AuthUser role helpers to prevent regressions.
…om hello package, and initialize telegram-service tests
Allow customizing the CSRF state cookie key and improve state handling/security. The Next.js client now reads MAYRLABS_AUTH_STATE_KEY from env and accepts cookie.stateKey in options, deletes the state cookie immediately after consumption, and no longer requires a request argument for redirectToLogin (uses absolute redirect). Types, tests and README updated to reflect behavior and verify deletion and custom key usage. Also: make b64url base64 encoding work in Node and browsers, add core utils tests, and adjust a minor URL param ordering in core client.
… instead of login
…directs instead of absolute SSO URLs
…ronment variable usage for configuration keys
…fined configuration
No description provided.