5 Star 1 Fork 0

光大期货 / gd-auth-sdk-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
jwt.go 1.31 KB
一键复制 编辑 原始数据 按行查看 历史
刘宝良 提交于 2022-08-01 11:36 . 首次检入casdoor-go-sdk
// Copyright 2021 The casbin Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package casdoorsdk
import (
"fmt"
"github.com/golang-jwt/jwt/v4"
)
type Claims struct {
User
AccessToken string `json:"accessToken"`
jwt.RegisteredClaims
}
func ParseJwtToken(token string) (*Claims, error) {
t, err := jwt.ParseWithClaims(token, &Claims{}, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
publicKey, err := jwt.ParseRSAPublicKeyFromPEM([]byte(authConfig.Certificate))
if err != nil {
return nil, err
}
return publicKey, nil
})
if t != nil {
if claims, ok := t.Claims.(*Claims); ok && t.Valid {
return claims, nil
}
}
return nil, err
}
Go
1
https://gitee.com/everbright_futures/gd-auth-sdk-go.git
git@gitee.com:everbright_futures/gd-auth-sdk-go.git
everbright_futures
gd-auth-sdk-go
gd-auth-sdk-go
v0.1.3

搜索帮助