mirror of
https://github.com/labring/sealos.git
synced 2026-08-28 17:22:42 +08:00
8c2db08fd4
* add credits * create table * change name & add table name * optimize interface * print result err log * fix * add credits for debt * creditsTransaction add region * refactor debt status: Normal\LowBalance\CriticalBalance\Debt\DebtDeletion\Final Period. * refactor debt status: Normal\LowBalance\CriticalBalance\Debt\DebtDeletion\Final Period. * add retry transaction * optimize determine status * support card pay & subscription plan & operator service * add pay notify & redirect url * fix * optimize * add error test log * fix req time * fix * fix * add subscription payment api * only processes the `CAPTURE_RESULT` notify type * fix * fix * fixed notify structure parsing * fix * fix * init a Free initial subscription plan √/ Initialize credits Create √ * init resourcequota wiht subscription * fix * fix * fix * fix * fix * fix * fix * fix * fix card type * fix * Subscribe controller: real-time monitoring subscriptionTransaction processing created/upgrade/downgrade * fix * fix * fix * determine current debt status with subscription * optimize pay api resp * process subscription auto renewal * optimize log * fix * fix * add GetAppResourceCosts api * fix * add GetLastSubscriptionTransaction api * add GetSubscriptionUpgradeAmount api * add /subscription/flush-quota api * fix * add remote flush subscription quota * go mod tidy * fix * add retry flush subscription quota * add custom debt status send email body * fix * fix * fix * fix * fix * add free process subscription * fix * add default pay expire time * optimize * support kyc & optimize * optimiz * add descributed lock with gorm db * optimize debt reconcile * optimize send email * optimize split sms vms code map; handler failure reflush debt status. * optimize * add email username * optimize * skip zero user * optimize flush svc * add convert test func * fix * optimize log * use the k8s client with cache * fix golang ci lint
82 lines
1.9 KiB
Go
82 lines
1.9 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/golang-jwt/jwt"
|
|
)
|
|
|
|
type JWTManager struct {
|
|
secretKey []byte
|
|
tokenDuration time.Duration
|
|
}
|
|
|
|
type UserClaims struct {
|
|
jwt.StandardClaims `json:",inline"`
|
|
JwtUser `json:",inline"`
|
|
}
|
|
|
|
type JwtUser struct {
|
|
Requester string `json:"requester,omitempty"`
|
|
UserUID uuid.UUID `json:"userUid,omitempty"`
|
|
UserCrUID string `json:"userCrUid,omitempty"`
|
|
UserCrName string `json:"userCrName,omitempty"`
|
|
RegionUID string `json:"regionUid,omitempty"`
|
|
UserID string `json:"userId,omitempty"`
|
|
WorkspaceID string `json:"workspaceId,omitempty"`
|
|
WorkspaceUID string `json:"workspaceUid,omitempty"`
|
|
}
|
|
|
|
func NewJWTManager(secretKey string, tokenDuration time.Duration) *JWTManager {
|
|
return &JWTManager{[]byte(secretKey), tokenDuration}
|
|
}
|
|
|
|
func (manager *JWTManager) GenerateToken(user JwtUser) (string, error) {
|
|
claims := UserClaims{
|
|
StandardClaims: jwt.StandardClaims{
|
|
ExpiresAt: time.Now().Add(manager.tokenDuration).Unix(),
|
|
},
|
|
JwtUser: user,
|
|
}
|
|
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
return token.SignedString(manager.secretKey)
|
|
}
|
|
|
|
func (manager *JWTManager) VerifyToken(tokenString string) (*UserClaims, error) {
|
|
token, err := jwt.ParseWithClaims(
|
|
tokenString,
|
|
&UserClaims{},
|
|
func(token *jwt.Token) (interface{}, error) {
|
|
_, ok := token.Method.(*jwt.SigningMethodHMAC)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unexpected token signing method")
|
|
}
|
|
|
|
return manager.secretKey, nil
|
|
},
|
|
)
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid token: %w", err)
|
|
}
|
|
|
|
claims, ok := token.Claims.(*UserClaims)
|
|
if !ok {
|
|
return nil, fmt.Errorf("invalid token claims")
|
|
}
|
|
|
|
return claims, nil
|
|
}
|
|
|
|
func (manager *JWTManager) ParseUser(token string) (*JwtUser, error) {
|
|
claims, err := manager.VerifyToken(token)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid token: %w", err)
|
|
}
|
|
return &claims.JwtUser, nil
|
|
}
|