Auth Custom
Custom authorization
To get the value of request header x-custom-authorization, use g.req.auth.authCustom.
Whatever your Token Validator function returns is what lands in g.req.auth.authCustom. Return the decoded JWT payload and you get the decoded token there — return your own object and you get that instead.
Why a custom provider?
AM, database, AWS, Azure and Google cover most cases. But sometimes the token is yours — your own claims, your own signing key, your own expiry rules. That is what a Custom Token Generator is for: you write the code that mints the token and the code that checks it, and API Maker handles the rest of the request pipeline.
Below is a complete working example using jose.
1. Install the jose package
Go to Utility → Sandbox Settings → Sandbox Dependencies and add jose.
Once added, it is available in every custom code of API Maker, including the two functions below.
2. Create the auth provider
Go to API Security → Auth Providers, click + and pick Custom Token Generator.
You will get three tabs: Basic Info, Token Generator and Token Validator.
Basic Info
nameis how you will refer to this provider everywhere else — in the get-token request and in theauthProvidersarray of your settings files.- Keep
runOnNativeProcessasfalseso the code runs inside the sandbox, where your npm packages live.
Token Generator
Runs when someone asks for a token. Whatever you return is sent back to the caller as-is.
|
The whole request body reaches this function as g.req.body, so you decide what your login payload looks like.
Token Validator
Runs on every request that carries the x-custom-authorization header. The token is in g.req.body.token.
The rule is simple — truthy means valid. Anything falsy, or a thrown error, means the request is rejected.
3. Generate a token
Request Method: POST
URL
nametells API Maker which auth provider to run. Everything else in the body is yours and is passed straight to your Token Generator asg.req.body.- For more information about this API click here.
Response — exactly what your generator returned:
4. Send the token
Put the access_token in the x-custom-authorization header on every call:
To let an API accept this provider, add its name to authProviders in your settings file:
5. Read it in your custom code
By the time your code runs, the token is already verified — API Maker rejects bad ones before they reach you. So g.req.auth.authCustom is safe to trust.