Skip to content
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

feat: Machine Token Docs #1725

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

feat: Machine Token Docs #1725

wants to merge 18 commits into from

Conversation

Ben2W
Copy link

@Ben2W Ben2W commented Nov 20, 2024

Note to reviewer:
There is not too much content, however I did restructure the docs which is why there are 61 files changed
Let me know if you want me to go about this PR in a different way (ie splitting the PR up or not changing the links around or maybe only change the manifest.json to make it appear the structure was changed, without affecting the links?)

Clerk redirects PR: https://github.com/clerk/clerk/pull/964

Currently all customer-facing tokens are session tokens. We are adding machine tokens which is another customer-facing token. This PR adds documentation on:

  • Understanding machine tokens
  • Creating machine tokens
  • Using machine tokens to authenticate requests
  • authenticating requests with machine tokens.

Additionally because now, all backend requests may no longer have a user + session associated with them, I modified the structure of some of the docs pages, and slightly edited existing pages to help fit the concept of "machine authentication". Read below to see what I changed.

Created the following machine request pages:

/docs/machine-requests/overview
/docs/machine-requests/machine-tokens
/docs/machine-requests/manual-jwt

Moved most of the "Backend Requests" pages to "Session Requests"

Calling this section "backend request" is confusing because machine requests are also backend requests.

This caused most of the "files changed" in the PR because a lot of links were changed from @NWylynko's script.

Old Path New Path
/docs/backend-requests/overview /docs/session-requests/overview
/docs/backend-requests/making/same-origin /docs/session-requests/making/same-origin
/docs/backend-requests/making/cross-origin /docs/session-requests/making/cross-origin
/docs/backend-requests/making/custom-session-token /docs/session-requests/making/custom-session-token
/docs/backend-requests/making/jwt-templates /docs/session-requests/making/jwt-templates
/docs/backend-requests/handling/go /docs/backend-requests/handling/go
/docs/backend-requests/handling/ruby-rails /docs/backend-requests/handling/ruby-rails
/docs/backend-requests/handling/manual-jwt /docs/backend-requests/handling/manual-jwt
/docs/backend-requests/resources/session-tokens /docs/backend-requests/session-tokens

Some pages that were in "Backend Requests" didn't fit under "Session Requests," so they were moved to their own section:

Old Path New Path
/docs/backend-requests/versioning/overview /docs/versioning/overview
/docs/backend-requests/versioning/available-versions /docs/versioning/available-versions
/docs/backend-requests/resources/rate-limits /docs/resources/rate-limits

Modified the following pages to add the concept of machine authentication:

Old Docs Page New Docs Page
https://clerk.com/docs/references/nextjs/auth https://clerk.com/docs/pr/1725/references/nextjs/auth
https://clerk.com/docs/references/backend/authenticate-request https://clerk.com/docs/pr/1725/references/backend/authenticate-request
https://clerk.com/docs/references/backend/types/auth-object https://clerk.com/docs/pr/1725/references/backend/types/auth-object

Slack thread:
https://clerkinc.slack.com/archives/C01QFRQNHSN/p1738141182851989

Checklist

  • I have clicked on "Files changed" and performed a thorough self-review
  • I have added the "deploy-preview" label and added the preview link(s) to this PR description
  • All existing checks pass

Copy link

Hey, here’s your docs preview: https://clerk.com/docs/pr/1725

Copy link

Hey, here’s your docs preview: https://clerk.com/docs/pr/1725

@Ben2W Ben2W marked this pull request as ready for review January 29, 2025 08:50
@Ben2W Ben2W requested a review from a team as a code owner January 29, 2025 08:51
@Ben2W Ben2W marked this pull request as draft January 29, 2025 08:55
@Ben2W Ben2W marked this pull request as ready for review January 29, 2025 08:59
@Ben2W Ben2W force-pushed the ben/machine-tokens branch 2 times, most recently from c43da27 to ee56c1d Compare February 3, 2025 21:42

#### Examples

<Tabs items={['Next.js Middleware', 'Next.js API Route']}>
Copy link
Member

Choose a reason for hiding this comment

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

feels weird to me that the authenticateRequest() example is in a whole different tab, but it makes sense because authenticateRequest() is it's own thing, i might be too in the weeds / looking too deep into it

Copy link
Author

Choose a reason for hiding this comment

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

Yea for me I think these examples are how to use machine to machine in backend frameworks.

However, since we only have Next.js, it feels weird that these are our only examples.

docs/machine-requests/overview.mdx Outdated Show resolved Hide resolved
docs/machine-requests/overview.mdx Outdated Show resolved Hide resolved
docs/machine-requests/overview.mdx Outdated Show resolved Hide resolved
docs/machine-requests/overview.mdx Outdated Show resolved Hide resolved
docs/machine-requests/overview.mdx Outdated Show resolved Hide resolved
docs/machine-requests/overview.mdx Outdated Show resolved Hide resolved
docs/machine-requests/manual-jwt.mdx Outdated Show resolved Hide resolved
docs/machine-requests/manual-jwt.mdx Outdated Show resolved Hide resolved
Ben2W added 5 commits February 6, 2025 19:11
running prettier

removing redundant ids

a "valid" id was in fact - not valid

removing unneccessary "s"

reverting

adding pages back in different folder

some minor changes

machine_id has a 255 character limit

Some Colin Notes
improving authenticateRequest docs page

updating auth object type

editing nextjs auth docs page

Removing unnecessary preface
@Ben2W Ben2W force-pushed the ben/machine-tokens branch from ee56c1d to 7e883d0 Compare February 7, 2025 03:11
@Ben2W Ben2W force-pushed the ben/machine-tokens branch from 8ac5001 to d029ef2 Compare February 7, 2025 03:52
@Ben2W Ben2W force-pushed the ben/machine-tokens branch from 3128c79 to 0b036b1 Compare February 7, 2025 06:51
@Ben2W
Copy link
Author

Ben2W commented Feb 7, 2025

@brkalow And @jakobevangelista

I addressed all of the comments (thanks for reviewing it).

Few things:

  • After rebasing the auth() reference doc was changed by someone else, I also found a small issue with authenticateRequest() so I pushed this commit: d7da65d
  • I changed the manifest.json to put "Backend Requests" at the same level as "Configuration.
    Image 2-6-25 at 11 42 PM
    Image 2-6-25 at 11 43 PM

@jakobevangelista
Copy link
Member

lgtm, prolly should ask alexis and/or nick for an extra opinions about the backend section change


#### Examples

<Tabs items={['Next.js API Route', 'Next.js Middleware']}>
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you suggesting these two examples get used in unison? To me I feel that the best option is to have an explicit ignore in the middleware, and then do the check in the route. But this looks to suggest doing one or the other?

const { token } = await clerkClient.machineTokens.create({
machineId: 'mch_cron',
claims: {
permissions: ['read', 'write'],
Copy link
Contributor

Choose a reason for hiding this comment

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

Some explanation around what the purpose of this is would be good, my guess is for the route to check that not only is the POST request authenticated but that its also authorized? I feel it would be beneficial to have down in the example below pulling these claims out and checking against them.


Every machine token you create needs to be associated with a `machine_id`. You can pick any value for the `machine_id` as long as it meets the following requirements:

- It must be prefixed with `mch_`
Copy link
Contributor

Choose a reason for hiding this comment

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

Since I am already explicitly calling clerkClient.machineTokens.create couldn't the sdk just prefix the id with mch_ for me? If it has to be this way then I think this is pretty clearly outlined. The types for this field should definitely be `mch_${string}` to enforce this.

- ❌ `MCH_UPPERCASE`

### Claims

Copy link
Contributor

Choose a reason for hiding this comment

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

An example here of how this works would be helpful, even better if the example showed how to type the claims, or validate them with a schema.


### Machine ID

Every machine token you create needs to be associated with a `machine_id`. You can pick any value for the `machine_id` as long as it meets the following requirements:
Copy link
Contributor

Choose a reason for hiding this comment

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

It's not necessarily clear why a machine_id is required. Is it purely an internal thing for clerk? Does this give us controls in clerk dashboard to "ban" a machine? Are we supposed to validate the machine_id where we are receiving the request?

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.

4 participants