Validate a user token
Overview
When writing your custom service involving AccelByte Gaming Services (AGS), you may be required to validate a user token before performing any actions. This can be achieved using the validate token function in the AccelByte Extend SDK.
There are two methods of token validation in the AccelByte Extend SDK: remote
and local
. With the remote
token validation, an AGS endpoint will be called every time token validation is requested. However, with the local
token validation, the token is validated against JWKS and the revocation list locally cached by the AccelByte Extend SDK. The difference between these two methods is the local
token validation may be faster than the remote
token validation, but it is possible to get a false result until the revocation list cached locally is refreshed. By default, the remote
token validation is used.
The AccelByte Extend SDK is available in multiple programming languages. Here we will show you how to validate a user token using the Go Extend SDK, with the remote
token validation.
Goals
In this guide you will:
- Import the Validator Package
- Validate a user token using the Go Extend SDK
Prerequisites
In order to start on this guide, you should have:
Installed Go 1.16 or later
Gained access to the AGS demo environment:
- Use
https://prod.gamingservices.accelbyte.io
for theAB_BASE_URL
environment variable. - Follow the steps in the Create an OAuth Client guide using a
confidential
client type. Use theClient ID
andClient Secret
forAB_CLIENT_ID
andAB_CLIENT_SECRET
environment variables respectively.
- Use
You must have added the Go Extend SDK as your project dependency. See Getting Started with the Extend SDK for more details.
Import the Validator Package, and validate a user token
Add the following in your import section.
import (
...
"github.com/AccelByte/accelbyte-go-sdk/services-api/pkg/utils/auth/validator"
...
)Validate a user token using the Go Extend SDK
Initialize the token validator, and validate the user token using the
Validate
function.tokenValidator := validator.NewTokenValidator(authService, time.Hour)
tokenValidator.Initialize()
err = tokenValidator.Validate(accessToken, &requiredPermission, &namespace, nil)
Next steps
- Consider trying the
local
token validation. Refer to the instructions on how to enable local token validation.
Resources
- For more advanced usage of the Go Extend SDK, see the README.