Go でユーザートークンを検証する
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 is locally cached by the AccelByte Extend SDK. The difference between these two methods is that the local token validation may be faster than the remote token validation. However, it is possible to get a false result until the locally cached revocation list is refreshed. By default, the remote token validation is used.
The AccelByte Extend SDK is available in multiple programming languages. This guide will show you how to validate a user token using the Go Modular Extend SDK with the remote token validation.
Goals
This guide will show you how to:
- Import the validator package
- Validate a user token using the Go Modular Extend SDK
Prerequisites
To perform the tasks in this guide, you must have:
- Installed Go 1.18 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 the Client ID and Client 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 Go Extend SDK for more details.
Import the validator package and validate a user token
Import the validator package.
Add the following in your import section.
import (
...
"github.com/AccelByte/accelbyte-go-modular-sdk/services-api/pkg/utils/auth/validator"
...
)Validate a user token using the Go Modular 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
- As a next step, you may consider trying the
local
token validation. To learn how, refer to the AccelByte Go Modular SDK guide.
Resources
- For more advanced usage of the Go Modular Extend SDK, refer to the Operations docs.