Skip to content

Commit

Permalink
Add support data jwt
Browse files Browse the repository at this point in the history
  • Loading branch information
huuhait committed Sep 26, 2022
1 parent d2e5095 commit eb04b27
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@ import (

// Auth struct represents parsed jwt information.
type Auth struct {
UID string `json:"uid"`
State string `json:"state"`
Email string `json:"email"`
Username string `json:"username"`
Role string `json:"role"`
ReferralUID null.String `json:"referral_uid,omitempty"`
HasPhone bool `json:"has_phone"`
OTP bool `json:"otp"`
Level int `json:"level"`
Audience []string `json:"aud,omitempty"`
UID string `json:"uid"`
State string `json:"state"`
Email string `json:"email"`
Username string `json:"username"`
Role string `json:"role"`
ReferralUID null.String `json:"referral_uid,omitempty"`
HasPhone bool `json:"has_phone"`
OTP bool `json:"otp"`
Level int `json:"level"`
Audience []string `json:"aud,omitempty"`
Data map[string]interface{} `json:"data,omitempty"`

jwt.StandardClaims
}
Expand Down Expand Up @@ -53,14 +54,14 @@ func appendClaims(defaultClaims, customClaims jwt.MapClaims) jwt.MapClaims {
}

// ForgeToken creates a valid JWT signed by the given private key
func ForgeToken(uid, email, role string, referralUID null.String, level int64, otp bool, hasPhone bool, key *rsa.PrivateKey, customClaims jwt.MapClaims) (string, error) {
func ForgeToken(uid, email, role string, referralUID null.String, level int64, otp bool, hasPhone bool, data map[string]interface{}, key *rsa.PrivateKey, customClaims jwt.MapClaims) (string, error) {
claims := appendClaims(jwt.MapClaims{
"iat": time.Now().Unix(),
"jti": strconv.FormatInt(time.Now().Unix(), 10),
"exp": time.Now().UTC().Add(time.Hour).Unix(),
"sub": "session",
"iss": "barong",
"aud": [3]string{"peatio", "barong", "kouda"},
"aud": [4]string{"peatio", "barong", "kouda", "quantex"},
"uid": uid,
"email": email,
"role": role,
Expand All @@ -69,6 +70,7 @@ func ForgeToken(uid, email, role string, referralUID null.String, level int64, o
"referral_uid": referralUID,
"has_phone": hasPhone,
"otp": otp,
"data": data,
}, customClaims)

t := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
Expand Down

0 comments on commit eb04b27

Please sign in to comment.