-
Notifications
You must be signed in to change notification settings - Fork 468
chore(backend,nextjs): Improve machine auth type inference and hover types #9346
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
Draft
wobsoriano
wants to merge
10
commits into
main
Choose a base branch
from
rob/machine-auth-types-improvement
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.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2f7fc74
chore(backend): Improve machine auth type inference and hover types
wobsoriano 6928994
chore(backend): Export InferAuthObject from internal subpath
wobsoriano 0e8739e
chore(nextjs): Use InferAuthObject helper in protect types
wobsoriano a9cf15a
chore(repo): Drop redundant empty changeset
wobsoriano c86996b
chore(backend): Note why unused delegate types must stay through 3.x
wobsoriano aebaaf0
chore(backend): Deprecate InferAuthObjectFromToken and InferAuthObjec…
wobsoriano 33e95b7
chore(backend): Tighten type test comments
wobsoriano 71cb526
chore(backend): Check machine object construction and fix debug carry…
wobsoriano 41b6633
chore(repo): Mention toAuth narrowing and debug fix in changeset
wobsoriano 03ae364
Merge branch 'main' into rob/machine-auth-types-improvement
wobsoriano 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,9 @@ | ||
| --- | ||
| '@clerk/backend': patch | ||
| --- | ||
|
|
||
| Improve editor hover types for `authenticateRequest()`, `auth()`, and `getAuth()` when using `acceptsToken`. Machine auth results now display as named discriminated unions (e.g. `AuthenticatedMachineObjectFor<"api_key"> | UnauthenticatedMachineObjectFor<"api_key">`) instead of expanded intersection types. | ||
|
|
||
| The internal `InferAuthObjectFromToken` and `InferAuthObjectFromTokenArray` types are deprecated in favor of the new `InferAuthObject` type and will be removed in the next major version. | ||
|
|
||
| Narrowing a `RequestState` by `tokenType` now also narrows the return type of `toAuth()`, and debug data is no longer dropped when an auth object is downgraded because its token type did not match `acceptsToken`. |
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 |
|---|---|---|
| @@ -1,33 +1,99 @@ | ||
| import { expectTypeOf, test } from 'vitest'; | ||
|
|
||
| import type { RequestState, TokenType } from '../../internal'; | ||
| import { authenticateRequest } from '../../tokens/request'; | ||
| import type { AuthenticateRequestOptions, RequestState, TokenType } from '../../internal'; | ||
| import type { AuthenticatedMachineObject, InvalidTokenAuthObject, SignedInAuthObject } from '../authObjects'; | ||
| import type { AuthenticatedState, HandshakeState, UnauthenticatedState } from '../authStatus'; | ||
| import { authenticateRequest } from '../request'; | ||
|
|
||
| test('returns the correct `authenticateRequest()` return type for each accepted token type', () => { | ||
| const request = new Request('https://example.com'); | ||
|
|
||
| // Session token by default | ||
| expectTypeOf(authenticateRequest(request)).toMatchTypeOf<Promise<RequestState>>(); | ||
| expectTypeOf(authenticateRequest(request)).toExtend<Promise<RequestState>>(); | ||
|
|
||
| // Individual token types | ||
| expectTypeOf(authenticateRequest(request, { acceptsToken: 'session_token' })).toMatchTypeOf< | ||
| expectTypeOf(authenticateRequest(request, { acceptsToken: 'session_token' })).toExtend< | ||
| Promise<RequestState<'session_token'>> | ||
| >(); | ||
| expectTypeOf(authenticateRequest(request, { acceptsToken: 'api_key' })).toMatchTypeOf< | ||
| Promise<RequestState<'api_key'>> | ||
| >(); | ||
| expectTypeOf(authenticateRequest(request, { acceptsToken: 'm2m_token' })).toMatchTypeOf< | ||
| expectTypeOf(authenticateRequest(request, { acceptsToken: 'api_key' })).toExtend<Promise<RequestState<'api_key'>>>(); | ||
| expectTypeOf(authenticateRequest(request, { acceptsToken: 'm2m_token' })).toExtend< | ||
| Promise<RequestState<'m2m_token'>> | ||
| >(); | ||
| expectTypeOf(authenticateRequest(request, { acceptsToken: 'oauth_token' })).toMatchTypeOf< | ||
| expectTypeOf(authenticateRequest(request, { acceptsToken: 'oauth_token' })).toExtend< | ||
| Promise<RequestState<'oauth_token'>> | ||
| >(); | ||
|
|
||
| // Array of token types | ||
| expectTypeOf(authenticateRequest(request, { acceptsToken: ['session_token', 'api_key', 'm2m_token'] })).toMatchTypeOf< | ||
| expectTypeOf(authenticateRequest(request, { acceptsToken: ['session_token', 'api_key', 'm2m_token'] })).toExtend< | ||
| Promise<RequestState<'session_token' | 'api_key' | 'm2m_token' | null>> | ||
| >(); | ||
|
|
||
| // Any token type | ||
| expectTypeOf(authenticateRequest(request, { acceptsToken: 'any' })).toMatchTypeOf<Promise<RequestState<TokenType>>>(); | ||
| expectTypeOf(authenticateRequest(request, { acceptsToken: 'any' })).toExtend<Promise<RequestState<TokenType>>>(); | ||
| }); | ||
|
|
||
| test('pins the exact resolved state union per accepted token type', () => { | ||
| const request = new Request('https://example.com'); | ||
|
|
||
| // Session tokens (and the no-options default) include HandshakeState | ||
| expectTypeOf(authenticateRequest(request)).resolves.toEqualTypeOf< | ||
| AuthenticatedState<'session_token'> | UnauthenticatedState<'session_token'> | HandshakeState | ||
| >(); | ||
| expectTypeOf(authenticateRequest(request, { acceptsToken: 'session_token' })).resolves.toEqualTypeOf< | ||
| AuthenticatedState<'session_token'> | UnauthenticatedState<'session_token'> | HandshakeState | ||
| >(); | ||
|
|
||
| // Machine tokens never produce a HandshakeState or a null tokenType | ||
| expectTypeOf(authenticateRequest(request, { acceptsToken: 'api_key' })).resolves.toEqualTypeOf< | ||
| AuthenticatedState<'api_key'> | UnauthenticatedState<'api_key'> | ||
| >(); | ||
| expectTypeOf(authenticateRequest(request, { acceptsToken: 'm2m_token' })).resolves.toEqualTypeOf< | ||
| AuthenticatedState<'m2m_token'> | UnauthenticatedState<'m2m_token'> | ||
| >(); | ||
|
|
||
| // Arrays add `null` to the unauthenticated side (invalid token) and keep | ||
| // HandshakeState only when session_token is a member | ||
| expectTypeOf(authenticateRequest(request, { acceptsToken: ['session_token', 'api_key'] })).resolves.toEqualTypeOf< | ||
| | AuthenticatedState<'session_token' | 'api_key'> | ||
| | UnauthenticatedState<'session_token' | 'api_key' | null> | ||
| | HandshakeState | ||
| >(); | ||
| expectTypeOf(authenticateRequest(request, { acceptsToken: ['m2m_token', 'oauth_token'] })).resolves.toEqualTypeOf< | ||
| AuthenticatedState<'m2m_token' | 'oauth_token'> | UnauthenticatedState<'m2m_token' | 'oauth_token' | null> | ||
| >(); | ||
| }); | ||
|
|
||
| test('accepts widened token type arrays but rejects readonly arrays', () => { | ||
| const request = new Request('https://example.com'); | ||
|
|
||
| const readonlyTokens = ['session_token', 'api_key'] as const; | ||
| // @ts-expect-error acceptsToken is typed as a mutable TokenType[], so `as const` arrays are rejected | ||
| void authenticateRequest(request, { acceptsToken: readonlyTokens }); | ||
|
|
||
| const widenedTokens: TokenType[] = ['session_token', 'api_key']; | ||
| expectTypeOf(authenticateRequest(request, { acceptsToken: widenedTokens })).toExtend< | ||
| Promise<RequestState<TokenType | null>> | ||
| >(); | ||
| }); | ||
|
|
||
| test('pins Parameters/ReturnType extraction to the last overload', () => { | ||
| expectTypeOf<Parameters<typeof authenticateRequest>>().toEqualTypeOf< | ||
| [request: Request, options?: AuthenticateRequestOptions] | ||
| >(); | ||
| expectTypeOf<ReturnType<typeof authenticateRequest>>().toEqualTypeOf<Promise<RequestState<'session_token'>>>(); | ||
| }); | ||
|
|
||
| test('narrowing tokenType on a state narrows the toAuth return type', async () => { | ||
| const request = new Request('https://example.com'); | ||
| const state = await authenticateRequest(request, { acceptsToken: ['session_token', 'api_key'] }); | ||
|
|
||
| if (state.status === 'signed-in' && state.tokenType === 'session_token') { | ||
| expectTypeOf(state.toAuth({ treatPendingAsSignedOut: true })).toEqualTypeOf<SignedInAuthObject>(); | ||
| } | ||
| if (state.status === 'signed-in' && state.tokenType === 'api_key') { | ||
| expectTypeOf(state.toAuth()).toEqualTypeOf<AuthenticatedMachineObject<'api_key'>>(); | ||
| } | ||
| if (state.status === 'signed-out' && state.tokenType === null) { | ||
| expectTypeOf(state.toAuth()).toEqualTypeOf<InvalidTokenAuthObject>(); | ||
| } | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Assert the inverse assignability for direct helper contracts.
These assertions do not fail if either helper returns all machine auth members. Add the inverse assertion, or use
toEqualTypeOf, forApiKey,Mixed, andMachineOnly. This keeps the direct@clerk/backend/internalhelper contract token-specific.🤖 Prompt for AI Agents