Skip to content

feat(backend,nextjs): Introduce machine authentication #5689

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

Open
wants to merge 45 commits into
base: main
Choose a base branch
from

Conversation

wobsoriano
Copy link
Member

@wobsoriano wobsoriano commented Apr 22, 2025

Description

This PR adds machine authentication support (atm only in the backend SDK) by introducing support for 4 token types: api_key, oauth_token, machine_token, and session_token. To maintain backwards compatibility, session_token remains the default authentication method when no specific token type is specified. This ensures existing apps continue to work without modification while allowing new applications to opt-in to machine authentication methods through the acceptsToken option.

Key changes:

  • Deprecated SignedInState and SignedOutState in favor of AuthenticatedState and UnauthenticatedState to better represent both session and machine authentication states. They still return the same properties, with an added tokenType and isAuthenticated properties (deprecating isSignedIn).
  • The toAuth() method now returns a different value if the tokenType is not a session_token. For now, we landed on the id, name, subject, claims and scopes property for machine auth tokens.
  • Added two new internal functions in authenticateRequest: authenticateAnyRequestWithTokenInHeader and authenticateMachineRequestWithTokenInHeader to handle machine authentication.
  • The internal signedIn and signedOut functions have been updated to accommodate machine auth.
  • Added new error types and codes specific to machine token verification (MachineTokenVerificationErrorCode)
  • Added new APIs (APIKeysApi, IdPOAuthAccessTokenApi, and MachineTokensApi) used inside a new verifyMachineAuthToken function to validate tokens against their respective endpoints
  • Added test for various scenarios for token validation, handling different token types, token mismatch, and proper error responses when verification fails

Here's an example usage pattern with API key:

Say C1 wants to protect their endpoints in a Hono app:

import { serve } from '@hono/node-server'
import { createMiddleware } from 'hono/factory'
import { Hono } from 'hono'
import { clerkClient } from './client'
import { HTTPException } from 'hono/http-exception'

const app = new Hono()

const clerkMiddleware = createMiddleware(async (c, next) => {
  const authReq = await clerkClient.authenticateRequest(c.req.raw, {
    acceptsToken: 'api_key'
  })

  if (!authReq.isAuthenticated) {
    throw new HTTPException(401, { message: 'Unauthorized' })
  }

  await next()
})

app.post('/api/protected', clerkMiddleware, async (c, next) => {
  return c.text('Hello from /api/protected')
})

Then C2 can access it by passing the api_key:

const resp = await fetch('http://localhost:3000/api/protected', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${process.env.API_KEY}`
  },
})

const data = await resp.text()

P.S. I attempted to break this down into smaller PRs but the changes are tightly coupled 😞. So sorry and thank you in advance reviewer! I believe 30-40% of the total changes are from the test files.

Resolves ROBO-36

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

Copy link

vercel bot commented Apr 22, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
clerk-js-sandbox ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 7, 2025 9:33pm

Copy link

changeset-bot bot commented Apr 22, 2025

🦋 Changeset detected

Latest commit: 178e823

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 11 packages
Name Type
@clerk/backend Major
@clerk/tanstack-react-start Minor
@clerk/agent-toolkit Minor
@clerk/react-router Minor
@clerk/express Minor
@clerk/fastify Minor
@clerk/astro Minor
@clerk/remix Minor
@clerk/nuxt Minor
@clerk/nextjs Minor
@clerk/testing Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

@jescalan jescalan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking great so far!

});

it('returns false for tokens without a recognized prefix', () => {
expect(isMachineToken('unknown_prefix_token')).toBe(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wanna note that we do plan to allow custom prefixes in the future - likely these end up being prepended to the token type prefix so i think it should be a fairly straightforward change

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a note to cover this in the future.

});

// Test each token type with parameterized tests
const tokenTypes = ['api_key', 'oauth_token', 'machine_token'] as const;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm mildly confused by the typecasting here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah the as const is needed here so TS knows these are literal types that match the keys in our mock objects, otherwise it would just see it as string[]

const { sessionTokenInHeader } = authenticateContext;
if (!sessionTokenInHeader) {
return handleError(new Error('No token in header'), 'header');
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something seems weird about this logic...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe in practice this shouldn't be hit, as we check the existence of the header token before calling this method.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah either that or we remove and do non-null assertions

const authenticateContext = await createAuthenticateContext(createClerkRequest(request), options);
assertValidSecretKey(authenticateContext.secretKey);

// Default tokenType is session_token for backwards compatibility.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So do we want/need to add a TODO to change this behavior in the future?

Copy link
Member Author

@wobsoriano wobsoriano May 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there are plans to change the behavior in the future yet. But I will update as soon as I get more info!

* The type of token to accept.
* @default 'session_token'
*/
acceptsToken?: TokenType | TokenType[] | 'any';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally prefer APIs where there is no mix between string and array of strings.

So default would be ['session_token']. Then I don't need to wrap it with an array when I quickly want to switch to two token types.

@wobsoriano wobsoriano changed the title feat(backend): Introduce machine authentication feat(backend,nextjs): Introduce machine authentication May 6, 2025
@wobsoriano wobsoriano changed the title feat(backend,nextjs): Introduce machine authentication feat(backend,nextjs,tanstack-react-start,nuxt,astro,remix,react-router): Introduce machine authentication May 6, 2025
@wobsoriano wobsoriano changed the title feat(backend,nextjs,tanstack-react-start,nuxt,astro,remix,react-router): Introduce machine authentication feat(backend,nextjs,tanstack-react-start,nuxt,astro,remix,react-router,agent-toolkit): Introduce machine authentication May 6, 2025
@wobsoriano wobsoriano changed the title feat(backend,nextjs,tanstack-react-start,nuxt,astro,remix,react-router,agent-toolkit): Introduce machine authentication feat(backend,nextjs): Introduce machine authentication May 6, 2025
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a minor update for these packages since we changed the auth type from AuthObject to SignedInAuthObject | SignedOutAuthObject for backwards compat

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though this is a major update, the only public API that is breaking is AuthObject. Previously, it's a union of SignedInAuthObject | SignedOutAuthObject but now it's

export type AuthObject =
  | SignedInAuthObject
  | SignedOutAuthObject
  | AuthenticatedMachineObject
  | UnauthenticatedMachineObject;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants