Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 47 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 14, 2025 1:54am

Copy link

changeset-bot bot commented Apr 22, 2025

🦋 Changeset detected

Latest commit: dbafb8e

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

@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;

@wobsoriano
Copy link
Member Author

!snapshot

@clerk-cookie
Copy link
Collaborator

Hey @wobsoriano - the snapshot version command generated the following package versions:

Package Version
@clerk/agent-toolkit 0.1.0-snapshot.v20250514155045
@clerk/astro 2.8.0-snapshot.v20250514155045
@clerk/backend 2.0.0-snapshot.v20250514155045
@clerk/chrome-extension 2.4.4-snapshot.v20250514155045
@clerk/clerk-js 5.65.1-snapshot.v20250514155045
@clerk/elements 0.23.26-snapshot.v20250514155045
@clerk/clerk-expo 2.11.4-snapshot.v20250514155045
@clerk/expo-passkeys 0.3.3-snapshot.v20250514155045
@clerk/express 1.5.0-snapshot.v20250514155045
@clerk/fastify 2.3.0-snapshot.v20250514155045
@clerk/localizations 3.15.3-snapshot.v20250514155045
@clerk/nextjs 6.20.0-snapshot.v20250514155045
@clerk/nuxt 1.7.0-snapshot.v20250514155045
@clerk/clerk-react 5.31.3-snapshot.v20250514155045
@clerk/react-router 1.5.0-snapshot.v20250514155045
@clerk/remix 4.8.0-snapshot.v20250514155045
@clerk/shared 3.8.3-snapshot.v20250514155045
@clerk/tanstack-react-start 0.16.0-snapshot.v20250514155045
@clerk/testing 1.7.0-snapshot.v20250514155045
@clerk/themes 2.2.44-snapshot.v20250514155045
@clerk/types 4.58.1-snapshot.v20250514155045
@clerk/vue 1.8.1-snapshot.v20250514155045

Tip: Use the snippet copy button below to quickly install the required packages.
@clerk/agent-toolkit

npm i @clerk/agent-toolkit@0.1.0-snapshot.v20250514155045 --save-exact

@clerk/astro

npm i @clerk/astro@2.8.0-snapshot.v20250514155045 --save-exact

@clerk/backend

npm i @clerk/backend@2.0.0-snapshot.v20250514155045 --save-exact

@clerk/chrome-extension

npm i @clerk/chrome-extension@2.4.4-snapshot.v20250514155045 --save-exact

@clerk/clerk-js

npm i @clerk/clerk-js@5.65.1-snapshot.v20250514155045 --save-exact

@clerk/elements

npm i @clerk/elements@0.23.26-snapshot.v20250514155045 --save-exact

@clerk/clerk-expo

npm i @clerk/clerk-expo@2.11.4-snapshot.v20250514155045 --save-exact

@clerk/expo-passkeys

npm i @clerk/expo-passkeys@0.3.3-snapshot.v20250514155045 --save-exact

@clerk/express

npm i @clerk/express@1.5.0-snapshot.v20250514155045 --save-exact

@clerk/fastify

npm i @clerk/fastify@2.3.0-snapshot.v20250514155045 --save-exact

@clerk/localizations

npm i @clerk/localizations@3.15.3-snapshot.v20250514155045 --save-exact

@clerk/nextjs

npm i @clerk/nextjs@6.20.0-snapshot.v20250514155045 --save-exact

@clerk/nuxt

npm i @clerk/nuxt@1.7.0-snapshot.v20250514155045 --save-exact

@clerk/clerk-react

npm i @clerk/clerk-react@5.31.3-snapshot.v20250514155045 --save-exact

@clerk/react-router

npm i @clerk/react-router@1.5.0-snapshot.v20250514155045 --save-exact

@clerk/remix

npm i @clerk/remix@4.8.0-snapshot.v20250514155045 --save-exact

@clerk/shared

npm i @clerk/shared@3.8.3-snapshot.v20250514155045 --save-exact

@clerk/tanstack-react-start

npm i @clerk/tanstack-react-start@0.16.0-snapshot.v20250514155045 --save-exact

@clerk/testing

npm i @clerk/testing@1.7.0-snapshot.v20250514155045 --save-exact

@clerk/themes

npm i @clerk/themes@2.2.44-snapshot.v20250514155045 --save-exact

@clerk/types

npm i @clerk/types@4.58.1-snapshot.v20250514155045 --save-exact

@clerk/vue

npm i @clerk/vue@1.8.1-snapshot.v20250514155045 --save-exact

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

Successfully merging this pull request may close these issues.

5 participants