messages
resource if users have the manager access level, and a write access to that resource if they have the administrator access level.You can define allowed permissions in the Permissions view of the Auth0 Dashboard’s APIs section. The following example uses the read:messages
scope.Add a go.mod
file to list all the necessary dependencies..env
file within the root of your project directory to store the app configuration. Then, fill in the environment variables:EnsureValidToken
middleware function validates the access token. You can apply this function to any endpoints you wish to protect. If the token is valid, the endpoint releases the resources. If the token is not valid, the API returns a 401 Authorization
error.Set up the go-jwt-middleware middleware to verify access tokens from incoming requests.By default, your API will be set up to use RS256 as the algorithm for signing tokens. Since RS256 works by using a private/public keypair, tokens can be verified against the public key for your Auth0 account. This public key is accessible at https://{yourDomain}/.well-known/jwks.json
.Include a mechanism to check that the token has sufficient scope to access the requested resources.Create a function HasScope
to check and ensure the access token has the correct scope before returning a successful response.In this example, create an /api/public
endpoint that does not use the EnsureToken
middleware as it is accessible to non-authenticated requests.Create an /api/private
endpoint that requires the EnsureToken
middleware as it is only available to authenticated requests containing an access token with no additional scope.Create an /api/private-scoped
endpoint that requires the EnsureToken
middleware and HasScope
as it is only available for authenticated requests containing an access token with the read:messages
scope granted.read:messages
scope is checked by the HasScope
function. You may want to extend it or make it a standalone middleware that accepts multiple scopes to fit your use case.Authorization
header in your requests.GET /api/public
is available for non-authenticated requests.GET /api/private
is available for authenticated requests.GET /api/private-scoped
is available for authenticated requests containing an access token with the read:messages
scope.messages
resource if users have the manager access level, and a write access to that resource if they have the administrator access level.You can define allowed permissions in the Permissions view of the Auth0 Dashboard’s APIs section. The following example uses the read:messages
scope.go.mod
file to list all the necessary dependencies..env
file within the root of your project directory to store the app configuration. Then, fill in the environment variables:EnsureValidToken
middleware function validates the access token. You can apply this function to any endpoints you wish to protect. If the token is valid, the endpoint releases the resources. If the token is not valid, the API returns a 401 Authorization
error.Set up the go-jwt-middleware middleware to verify access tokens from incoming requests.By default, your API will be set up to use RS256 as the algorithm for signing tokens. Since RS256 works by using a private/public keypair, tokens can be verified against the public key for your Auth0 account. This public key is accessible at https://{yourDomain}/.well-known/jwks.json
.Include a mechanism to check that the token has sufficient scope to access the requested resources.Create a function HasScope
to check and ensure the access token has the correct scope before returning a successful response./api/public
endpoint that does not use the EnsureToken
middleware as it is accessible to non-authenticated requests.Create an /api/private
endpoint that requires the EnsureToken
middleware as it is only available to authenticated requests containing an access token with no additional scope.Create an /api/private-scoped
endpoint that requires the EnsureToken
middleware and HasScope
as it is only available for authenticated requests containing an access token with the read:messages
scope granted.read:messages
scope is checked by the HasScope
function. You may want to extend it or make it a standalone middleware that accepts multiple scopes to fit your use case.Authorization
header in your requests.GET /api/public
is available for non-authenticated requests.GET /api/private
is available for authenticated requests.GET /api/private-scoped
is available for authenticated requests containing an access token with the read:messages
scope.