add order data

This commit is contained in:
daniel.w 2024-08-27 13:35:08 +08:00
parent 26a14a8a9c
commit 0db1affc10
4 changed files with 211 additions and 0 deletions

67
jwt/claims.go Normal file
View File

@ -0,0 +1,67 @@
package jwt
type DataClaims map[string]string
const (
idCode = "id"
roleCode = "role"
deviceIdCode = "device_id"
scopeCode = "scope"
uidCode = "uid"
)
// ============ 使用具體的 setter ============
// Set 通用的 setter 方法
func (c DataClaims) Set(key, value string) {
c[key] = value
}
func (c DataClaims) SetID(id string) {
c.Set(idCode, id)
}
func (c DataClaims) SetRole(role string) {
c.Set(roleCode, role)
}
func (c DataClaims) SetDeviceID(deviceID string) {
c.Set(deviceIdCode, deviceID)
}
func (c DataClaims) SetScope(scope string) {
c.Set(scopeCode, scope)
}
func (c DataClaims) SetUID(uid string) {
c.Set(uidCode, uid)
}
// ============ 使用具體的 getter ============
func (c DataClaims) Get(key string) string {
if val, ok := c[key]; ok {
return val
}
return ""
}
func (c DataClaims) Scope() {
c.Get(scopeCode)
}
func (c DataClaims) Role() string {
return c.Get(roleCode)
}
func (c DataClaims) ID() string {
return c.Get(idCode)
}
func (c DataClaims) DeviceID() string {
return c.Get(deviceIdCode)
}
func (c DataClaims) UID() string {
return c.Get(uidCode)
}

51
jwt/define.go Normal file
View File

@ -0,0 +1,51 @@
package jwt
import (
"github.com/golang-jwt/jwt/v4"
"time"
)
type Token struct {
ID string `json:"id"`
UID string `json:"uid"`
DeviceID string `json:"device_id"`
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
AccessCreateAt time.Time `json:"access_create_at"`
RefreshToken string `json:"refresh_token"`
RefreshExpiresIn int `json:"refresh_expires_in"`
RefreshCreateAt time.Time `json:"refresh_create_at"`
}
func (t *Token) AccessTokenExpires() time.Duration {
return time.Duration(t.ExpiresIn) * time.Second
}
func (t *Token) RefreshTokenExpires() time.Duration {
return time.Duration(t.RefreshExpiresIn) * time.Second
}
func (t *Token) RefreshTokenExpiresUnix() int64 {
return time.Now().Add(t.RefreshTokenExpires()).Unix()
}
func (t *Token) IsExpires() bool {
return t.AccessCreateAt.Add(t.AccessTokenExpires()).Before(time.Now())
}
func (t *Token) RedisExpiredSec() int64 {
sec := time.Unix(int64(t.ExpiresIn), 0).Sub(time.Now().UTC())
return int64(sec.Seconds())
}
func (t *Token) RedisRefreshExpiredSec() int64 {
sec := time.Unix(int64(t.RefreshExpiresIn), 0).Sub(time.Now().UTC())
return int64(sec.Seconds())
}
type Claims struct {
jwt.RegisteredClaims
Data interface{} `json:"data"`
}

5
jwt/go.mod Normal file
View File

@ -0,0 +1,5 @@
module code.30cm.net/digimon/library-go/jwt
go 1.22.3
require github.com/golang-jwt/jwt/v4 v4.5.0

88
jwt/token.go Normal file
View File

@ -0,0 +1,88 @@
package jwt
import (
"fmt"
"github.com/golang-jwt/jwt/v4"
"time"
)
func GenerateAccessToken(token Token, data any, sign string, issuer string) (string, error) {
claim := Claims{
Data: data,
RegisteredClaims: jwt.RegisteredClaims{
ID: token.ID,
ExpiresAt: jwt.NewNumericDate(time.Unix(int64(token.ExpiresIn), 0)),
Issuer: issuer,
},
}
accessToken, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claim).
SignedString([]byte(sign))
if err != nil {
return "", err
}
return accessToken, nil
}
func ParseToken(accessToken string, secret string, validate bool) (jwt.MapClaims, error) {
// 跳過驗證的解析
var token *jwt.Token
var err error
if validate {
token, err = jwt.Parse(accessToken, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("token unexpected signing method: %v", token.Header["alg"])
}
return []byte(secret), nil
})
if err != nil {
return jwt.MapClaims{}, err
}
} else {
parser := jwt.NewParser(jwt.WithoutClaimsValidation())
token, err = parser.Parse(accessToken, func(token *jwt.Token) (interface{}, error) {
return []byte(secret), nil
})
if err != nil {
return jwt.MapClaims{}, err
}
}
claims, ok := token.Claims.(jwt.MapClaims)
if !ok && token.Valid {
return jwt.MapClaims{}, fmt.Errorf("token valid error")
}
return claims, nil
}
func ParseClaims(accessToken string, secret string, validate bool) (DataClaims, error) {
claimMap, err := ParseToken(accessToken, secret, validate)
if err != nil {
return DataClaims{}, err
}
claimsData, ok := claimMap["data"].(map[string]any)
if ok {
return convertMap(claimsData), nil
}
return DataClaims{}, fmt.Errorf("get data from claim map error")
}
func convertMap(input map[string]interface{}) map[string]string {
output := make(map[string]string)
for key, value := range input {
switch v := value.(type) {
case string:
output[key] = v
case fmt.Stringer:
output[key] = v.String()
default:
output[key] = fmt.Sprintf("%v", value)
}
}
return output
}