87 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			87 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package jwt
 | |
| 
 | |
| import (
 | |
| 	"math/rand"
 | |
| 	"time"
 | |
| 
 | |
| 	"github.com/dgrijalva/jwt-go"
 | |
| )
 | |
| 
 | |
| type Claims struct {
 | |
| 	ID       uint   `json:"id"`
 | |
| 	Account  string `json:"account"`
 | |
| 	Domain   string `json:"domain"`
 | |
| 	NickName string `json:"nickName"`
 | |
| 	Phone    string `json:"phone"`
 | |
| 	jwt.StandardClaims
 | |
| }
 | |
| 
 | |
| type RefreshClaims struct {
 | |
| 	ID  uint `json:"id"`
 | |
| 	RId int  `json:"rid"`
 | |
| 	jwt.StandardClaims
 | |
| }
 | |
| 
 | |
| // GenerateToken 签发用户Token
 | |
| func GenerateToken(id uint, account string, domain string, nickName string, hour int, phone string, jwtSecret []byte) (string, error) {
 | |
| 	nowTime := time.Now()
 | |
| 	expireTime := nowTime.Add(time.Duration(hour) * time.Hour)
 | |
| 	claims := Claims{
 | |
| 		ID:       id,
 | |
| 		Account:  account,
 | |
| 		Domain:   domain,
 | |
| 		NickName: nickName,
 | |
| 		Phone:    phone,
 | |
| 		StandardClaims: jwt.StandardClaims{
 | |
| 			ExpiresAt: expireTime.Unix(),
 | |
| 			Issuer:    domain,
 | |
| 		},
 | |
| 	}
 | |
| 	tokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
 | |
| 	token, err := tokenClaims.SignedString(jwtSecret)
 | |
| 	return token, err
 | |
| }
 | |
| 
 | |
| // ParseToken 验证用户token
 | |
| func ParseToken(token string, jwtSecret []byte) (*Claims, error) {
 | |
| 	tokenClaims, err := jwt.ParseWithClaims(token, &Claims{}, func(token *jwt.Token) (interface{}, error) {
 | |
| 		return jwtSecret, nil
 | |
| 	})
 | |
| 	if tokenClaims != nil {
 | |
| 		if claims, ok := tokenClaims.Claims.(*Claims); ok && tokenClaims.Valid {
 | |
| 			return claims, nil
 | |
| 		}
 | |
| 	}
 | |
| 	return nil, err
 | |
| }
 | |
| 
 | |
| // GenerateRefreshToken 签发用户Token
 | |
| func GenerateRefreshToken(id uint, domain string, hour int, jwtSecret []byte) (string, error) {
 | |
| 	nowTime := time.Now()
 | |
| 	expireTime := nowTime.Add(time.Duration(hour) * time.Hour)
 | |
| 	claims := RefreshClaims{
 | |
| 		ID:  id,
 | |
| 		RId: rand.Intn(999),
 | |
| 		StandardClaims: jwt.StandardClaims{
 | |
| 			ExpiresAt: expireTime.Unix(),
 | |
| 			Issuer:    domain,
 | |
| 		},
 | |
| 	}
 | |
| 	tokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
 | |
| 	token, err := tokenClaims.SignedString(jwtSecret)
 | |
| 	return token, err
 | |
| }
 | |
| 
 | |
| // ParseRefreshToken 验证用户token
 | |
| func ParseRefreshToken(token string, jwtSecret []byte) (*RefreshClaims, error) {
 | |
| 	tokenClaims, err := jwt.ParseWithClaims(token, &RefreshClaims{}, func(token *jwt.Token) (interface{}, error) {
 | |
| 		return jwtSecret, nil
 | |
| 	})
 | |
| 	if tokenClaims != nil {
 | |
| 		if claims, ok := tokenClaims.Claims.(*RefreshClaims); ok && tokenClaims.Valid {
 | |
| 			return claims, nil
 | |
| 		}
 | |
| 	}
 | |
| 	return nil, err
 | |
| }
 |