Skip to content

feat(backend): 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 36 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.

Note: The Nextjs part will be handled in #5710. Changesets to follow.

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:

Sorry, something went wrong.

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 2, 2025 5:21pm

Copy link

changeset-bot bot commented Apr 22, 2025

⚠️ No Changeset found

Latest commit: 572854e

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a 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): Add machine authentication support feat(backend): Introduce machine authentication May 1, 2025
@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.0.29-snapshot.v20250501225444
@clerk/astro 2.6.11-snapshot.v20250501225444
@clerk/backend 1.31.0-snapshot.v20250501225444
@clerk/chrome-extension 2.3.5-snapshot.v20250501225444
@clerk/clerk-js 5.63.3-snapshot.v20250501225444
@clerk/clerk-expo 2.10.5-snapshot.v20250501225444
@clerk/express 1.4.12-snapshot.v20250501225444
@clerk/fastify 2.2.12-snapshot.v20250501225444
@clerk/nextjs 6.18.3-snapshot.v20250501225444
@clerk/nuxt 1.5.12-snapshot.v20250501225444
@clerk/react-router 1.3.3-snapshot.v20250501225444
@clerk/remix 4.6.3-snapshot.v20250501225444
@clerk/tanstack-react-start 0.14.3-snapshot.v20250501225444
@clerk/testing 1.6.2-snapshot.v20250501225444

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

npm i @clerk/[email protected] --save-exact

@clerk/astro

npm i @clerk/[email protected] --save-exact

@clerk/backend

npm i @clerk/[email protected] --save-exact

@clerk/chrome-extension

npm i @clerk/[email protected] --save-exact

@clerk/clerk-js

npm i @clerk/[email protected] --save-exact

@clerk/clerk-expo

npm i @clerk/[email protected] --save-exact

@clerk/express

npm i @clerk/[email protected] --save-exact

@clerk/fastify

npm i @clerk/[email protected] --save-exact

@clerk/nextjs

npm i @clerk/[email protected] --save-exact

@clerk/nuxt

npm i @clerk/[email protected] --save-exact

@clerk/react-router

npm i @clerk/[email protected] --save-exact

@clerk/remix

npm i @clerk/[email protected] --save-exact

@clerk/tanstack-react-start

npm i @clerk/[email protected] --save-exact

@clerk/testing

npm i @clerk/[email protected] --save-exact

Copy link
Member

@brkalow brkalow left a comment

Choose a reason for hiding this comment

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

Looking good! Nice work

Comment on lines +53 to +55
// Using "/" instead of an actual version since they're bapi-proxy endpoints.
// bapi-proxy connects directly to C1 without URL versioning,
// while API versioning is handled through the Clerk-API-Version header.
Copy link
Member

Choose a reason for hiding this comment

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

Huh, this is surprising to me, I would expect the path structure at edge to mirror origin 🤔 I'll start a thread in Slack

Copy link
Member Author

Choose a reason for hiding this comment

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

I saw the thread. Yeah, I also thought of creating a separate buildRequest function for bapi-proxy but that'd be too much considering the logic we already have when building a request. For now I'm passing "/" to bypass the api version during URL normalization

export * from './BetaFeaturesApi';
export * from './BlocklistIdentifierApi';
export * from './ClientApi';
export * from './DomainApi';
export * from './EmailAddressApi';
export * from './IdPOAuthAccessTokenApi';
Copy link
Member

Choose a reason for hiding this comment

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

Why does this one start with IdP?

Copy link
Member Author

@wobsoriano wobsoriano May 2, 2025

Choose a reason for hiding this comment

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

So.. this was hard to name. We already have:

  1. OauthAccessToken which is used for retrieving the OAuth access token of a user
  2. OAuthApplication for OAuth applications

I named it like that based on the object name used (clerk_idp_oauth_access_token) for response deserialization but suggestions welcome!

Comment on lines +76 to +86
type MachineObjectExtendedProperties<TAuthenticated extends boolean> = {
api_key: {
name: TAuthenticated extends true ? string : null;
claims: TAuthenticated extends true ? Claims | null : null;
};
machine_token: {
name: TAuthenticated extends true ? string : null;
claims: TAuthenticated extends true ? Claims | null : null;
};
oauth_token: object;
};
Copy link
Member

Choose a reason for hiding this comment

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

clever 👀

Comment on lines +48 to 51
/**
* @deprecated Use `isAuthenticated` instead.
*/
isSignedIn: true;
Copy link
Member

Choose a reason for hiding this comment

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

isSignedIn feels more natural when dealing with session tokens 🤔 I'm wondering if we're weighing machine token usage too heavily here when considering renaming this API...

Copy link
Member Author

@wobsoriano wobsoriano May 2, 2025

Choose a reason for hiding this comment

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

Great point! I think we relied too much on having unified property, but most of our users are still thinking in terms of user sessions so it's best to still have it for sessions 🤔

I think we also named it like that for reasons that a tokenType property can have an array of tokens, and that would mean both properties (isSignedIn and isAuthenticated) will show up in the result

@@ -69,13 +72,64 @@ function isRequestEligibleForRefresh(
);
}

export async function authenticateRequest(
function maybeHandleTokenTypeMismatch(
Copy link
Member

Choose a reason for hiding this comment

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

Why is this maybe?

Copy link
Member Author

Choose a reason for hiding this comment

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

I originally named it to match the handleMaybeHandshakeStatus pattern, but looking back, that function actually returns either a signed out or handshake state, while maybeHandleTokenTypeMismatch only returns a signed out state or null. It's vague 😬 - renamed it to checkTokenTypeMismatch. Suggestions welcome!

Comment on lines 651 to 653
if (!sessionOrMachineTokenInHeader) {
return handleError(new Error('Missing token in header'), 'header');
}
Copy link
Member

Choose a reason for hiding this comment

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

This shouldn't be possible to hit right? I see it's called inside a conditional header token check.

Copy link
Member Author

Choose a reason for hiding this comment

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

Correct! This or we do non-null assertions which I'm trying to avoid mostly

const { sessionOrMachineTokenInHeader } = authenticateContext;
// Use session token error handling if no token in header (default behavior)
if (!sessionOrMachineTokenInHeader) {
return handleError(new Error('Missing token in header'), 'header');
Copy link
Member

Choose a reason for hiding this comment

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

consider renaming handleError to handleSessionTokenError() to distinguish between the machine handler.

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated!

const { sessionTokenInHeader } = authenticateContext;
if (!sessionTokenInHeader) {
return handleError(new Error('No token in header'), 'header');
}
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.

const verifiedToken = await client.machineTokens.verifySecret(secret);
return { data: verifiedToken, tokenType: TokenType.MachineToken, errors: undefined };
} catch (err: any) {
return handleClerkAPIError('machine_token', err, 'Machine token not found');
Copy link
Member

Choose a reason for hiding this comment

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

Use the enum 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.

Updated!

@@ -131,7 +132,7 @@ export const auth: AuthFn = async () => {
};

return Object.assign(authObject, { redirectToSignIn, redirectToSignUp });
};
}) as AuthFn;
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need to cast here now?

Copy link
Member Author

@wobsoriano wobsoriano May 2, 2025

Choose a reason for hiding this comment

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

Same as my comment here, but for this PR, it's just to make TS happy as getAuthDataFromRequest returns a AuthObject type and we want to force it to just the session types.

The full context is in the Nextjs companion PR #5710

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.

None yet

4 participants