Define the API endpoints
npm init
. This sets up your package.json
file.Leave the default settings or change them as you see fit.Our sample’s package.json
looks like the following:expressJwtSecret
, we can generate a secret provider that provides the right signing key to express-jwt
based on the kid
in the JWT header. To learn more, refer to the node-jwks-rsa GitHub repository.req.body
as something easier with which to interface.server.js
file. Your code needs to:node server
and make an HTTP POST request to localhost:8080/timesheets
. You should see a JSON response with the message This is the POST /timesheets endpoint
.So now we have our endpoint but anyone can call it. Continue to the next step to see how we can fix this.Secure the API endpoints
jwt
function, provided by the express-jwt middleware, and the jwks-rsa
to retrieve our secret. The libraries do the following:express-jwt
decodes the token and pass the request, the header, and the payload to jwksRsa.expressJwtSecret
.jwks-rsa
downloads all signing keys from the JWKS endpoint and see if a one of the signing keys matches the kid
in the header of the JWT. If none of the signing keys match the incoming kid
, an error will be thrown. If we have a match, pass the right signing key to express-jwt
.express-jwt
continues its own logic to validate the signature of the token, the expiration, audience
and the issuer
.localhost:8080/timesheets
we should get the error message Missing or invalid token
(which is accurate since we didn’t send an access token in our request).In order to test the working scenario as well we need to:Authorization
header to our request with the value Bearer ACCESS_TOKEN
(where ACCESS_TOKEN
is the value of the token we retrieved in the first step).Check the app permissions
batch:upload
.In order to do this, we make use of the express-jwt-authz
Node.js package, so add that to your project:jwtAuthz(...)
to your middleware to ensure that the JWT contain a particular scope in order to execute a particular endpoint.We add an additional dependency. The express-jwt-authz library, which is used in conjunction with express-jwt, validates the JWT and ensures it bears the correct permissions to call the desired endpoint. For more information, refer to the express-jwt-authz GitHub repository.This is our sample implementation (some code is omitted for brevity):403
. You can test this by removing this scope from your API.Determine User Identity
express-jwt
middleware that is used to validate the JWT also sets req.user
with the information contained in the JWT. If you want to use the sub
claim to identify the user uniquely, you can use req.user.sub
. For the timesheets application, we want to use the email address of the user as a unique identifier.Add email to access token
), select the Login / Post Login trigger, and select Create.namespace
is used to ensure the claim has a unique name that does not clash with standard OIDC claims or internal services. To learn more about restrictions and guidelines with namespaced and non-namespaced claims, read Create Custom Claims.req.auth
. Use that value as the unique user identifier to associate with timesheet entries.