feat(backend): *WIP* Add machine authentication support #5689
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.
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
, andsession_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 theacceptsToken
option.Key changes:
SignedInState
andSignedOutState
in favor ofAuthenticatedState
andUnauthenticatedState
to better represent both session and machine authentication states. They still return the same properties, with an addedtokenType
andisAuthenticated
properties (deprecatingisSignedIn
).toAuth()
method now returns a different value if thetokenType
is not asession_token
. For now, we landed on theid
,name
,subject
andclaims
property for machine auth tokens.authenticateRequest
:authenticateAnyRequestWithTokenInHeader
andauthenticateMachineRequestWithTokenInHeader
to handle machine authentication.signedIn
andsignedOut
functions have been updated to accommodate machine auth.MachineTokenVerificationErrorCode
)APIKeysApi
,IdPOAuthAccessTokenApi
, andMachineTokensApi
) used inside a newverifyMachineAuthToken
function to validate tokens against their respective endpointsHere's an example usage pattern with API key:
Say C1 wants to protect their endpoints in a Hono app:
Then C2 can access it by passing the api_key:
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!
Checklist
pnpm test
runs as expected.pnpm build
runs as expected.Type of change