Add Balance and Withdrawal
This commit is contained in:
@@ -25,6 +25,8 @@ type CryptoConfig interface {
|
||||
AccessTokenExpiresDate() time.Time
|
||||
AccessTokenResetPasswordSecret() string
|
||||
AccessTokenResetPasswordExpire() time.Time
|
||||
AccessTokenWithdrawSecret() string
|
||||
AccessTokenWithdrawExpire() time.Time
|
||||
}
|
||||
|
||||
type CryptoImpl struct {
|
||||
@@ -186,3 +188,54 @@ func (c *CryptoImpl) ValidateResetPassword(tokenString string) (int64, error) {
|
||||
|
||||
return claims.UserID, nil
|
||||
}
|
||||
|
||||
func (c *CryptoImpl) GenerateJWTWithdraw(req *entity.WalletWithdrawRequest) (string, error) {
|
||||
claims := &entity.JWTWithdrawClaims{
|
||||
StandardClaims: jwt.StandardClaims{
|
||||
Subject: strconv.FormatInt(req.ID, 10),
|
||||
ExpiresAt: c.Config.AccessTokenWithdrawExpire().Unix(),
|
||||
IssuedAt: time.Now().Unix(),
|
||||
NotBefore: time.Now().Unix(),
|
||||
},
|
||||
PartnerID: req.PartnerID,
|
||||
Amount: req.Amount,
|
||||
Fee: req.Fee,
|
||||
Total: req.Total,
|
||||
}
|
||||
|
||||
token, err := jwt.
|
||||
NewWithClaims(jwt.SigningMethodHS256, claims).
|
||||
SignedString([]byte(c.Config.AccessTokenWithdrawSecret()))
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func (c *CryptoImpl) ValidateJWTWithdraw(tokenString string) (*entity.WalletWithdrawRequest, error) {
|
||||
token, err := jwt.ParseWithClaims(tokenString, &entity.JWTWithdrawClaims{}, func(token *jwt.Token) (interface{}, error) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
return []byte(c.Config.AccessTokenWithdrawSecret()), nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
claims, ok := token.Claims.(*entity.JWTWithdrawClaims)
|
||||
if !ok || !token.Valid {
|
||||
return nil, fmt.Errorf("invalid token %v", token.Header["alg"])
|
||||
}
|
||||
|
||||
return &entity.WalletWithdrawRequest{
|
||||
ID: claims.ID,
|
||||
PartnerID: claims.PartnerID,
|
||||
Total: claims.Total,
|
||||
Amount: claims.Amount,
|
||||
Fee: claims.Fee,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user