Docs

Go: Authorization

View on Github

Go: Authorization

Gravatar for jim.anderson@auth0.com
By Jim Anderson

This tutorial demonstrates how to add authorization to a Go API. We recommend you to Log in to follow this quickstart with examples configured for your account.

Or

I want to explore a sample app

2 minutes

Get a sample configured with your account settings or check it out on Github.

View on Github
System requirements: Go 1.8.2

New to Auth0? Learn how Auth0 works and read about implementing API authentication and authorization using the OAuth 2.0 framework.

User app_metadata schema

Configure Auth0 APIs

File example

Create an API

In the APIs section of the Auth0 dashboard, click Create API. Provide a name and an identifier for your API, for example https://quickstarts/api. You will use the identifier as an audience later, when you are configuring the Access Token verification. Leave the Signing Algorithm as RS256.

Create API

By default, your API uses RS256 as the algorithm for signing tokens. Since RS256 uses a private/public keypair, it verifies the tokens against the public key for your Auth0 account. The public key is in the JSON Web Key Set (JWKS) format, and can be accessed here.

Keep reading

Define Permissions

Permissions let you define how resources can be accessed on behalf of the user with a given access token. For example, you might choose to grant read access to the 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 tab of the Auth0 Dashboard's APIs section.

Configure Permissions

This example uses the read:messages scope.

This example demonstrates:

  • How to check for a JSON Web Token (JWT) in the Authorization header of an incoming HTTP request.

  • How to check if the token is valid, using the JSON Web Key Set (JWKS) for your Auth0 account. To learn more about validating Access Tokens, read the Validate an Access Token tutorial.

Validate Access Tokens

Install dependencies

The dgrijalva/jwt-go package can be used to verify incoming JWTs. The auth0/go-jwt-middleware library can be used alongside it to fetch your Auth0 public key and complete the verification process. Finally, we'll use the gorilla/mux package to handle our routes and codegangsta/negroni for HTTP middleware.

Create a middleware to validate Access Tokens

The Access Token validation will be done in the checkJwt middleware function which can be applied to any endpoints you wish to protect. If the token is valid, the resources which are served by the endpoint can be released, otherwise a 401 Authorization error will be returned.

Setup go-jwt-middleware middleware to verify Access Token from incoming requests.

By default, your API uses RS256 as the algorithm for signing tokens. Since RS256 uses a private/public keypair, it verifies the tokens against the public key for your Auth0 account. The public key is in the JSON Web Key Set (JWKS) format, and can be accessed here.

Create the function to get the remote JWKS for your Auth0 account and return the certificate with the public key in PEM format.

Protect API Endpoints

To protect individual routes pass the instance of go-jwt-middleware defined above to the negroni handler.

Validate scopes

The go-jwt-middleware middleware above verifies that the Access Token included in the request is valid; however, it doesn't yet include any mechanism for checking that the token has the sufficient scope to access the requested resources.

Let's create a function to check and ensure the Access Token has the correct scope before returning a successful response.

We will use this function in the endpoint that requires the scope read:messages.

In our example we only checked for the read:messages scope. You may want to extend the checkScope function or make it a standalone middleware that accepts multiple roles to fit your use case.

Use Auth0 for FREE