我正在学习围棋,我想用Auth0设置一个简单的应用程序. 通过使用their个教程,我能够为我的API端点设置基本身份验证. 现在,我想使用JWT令牌添加权限处理. 因此,我为API-Endpoint激活了RBAC并添加了权限. 我将教程中的流程用于定制声明,但用它编写了自己的中间件,并对其进行了调整以与杜松子wine 一起工作.
func NeedsPermission(expectedScope string) gin.HandlerFunc {
return func(context *gin.Context) {
token := context.Request.Context().Value(jwtmiddleware.ContextKey{}).(*validator.ValidatedClaims)
claims := token.CustomClaims.(*CustomClaims)
if !claims.HasScope(expectedScope) {
context.AbortWithStatus(403)
}
context.Next()
}
}
问题是,令牌中没有自定义声明,只有默认的:OpenID、个人资料和邮箱声明.
以下是令牌内容:
{
"iss": "https://dev-****.us.auth0.com/",
"sub": "google-oauth2|****",
"aud": [
"localhost:3000/books",
"https://dev-****.us.auth0.com/userinfo"
],
"iat": 1701789297,
"exp": 1701875697,
"azp": "***",
"scope": "openid profile email",
"permissions": [
"read:books"
]
}
所以它确实有一个字段权限,但是我如何使用auth 0/go-jwt-middleware访问它,或者我必须先以某种方式解码它?