You've already forked CasaOS
mirror of
https://github.com/IceWhaleTech/CasaOS.git
synced 2025-07-15 23:54:17 +02:00
first commit
This commit is contained in:
46
pkg/utils/jwt/jwt.go
Normal file
46
pkg/utils/jwt/jwt.go
Normal file
@ -0,0 +1,46 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
jwt "github.com/dgrijalva/jwt-go"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Claims struct {
|
||||
UserName string `json:"username"`
|
||||
PassWord string `json:"password"`
|
||||
jwt.StandardClaims
|
||||
}
|
||||
|
||||
var jwtSecret []byte
|
||||
|
||||
//创建token
|
||||
func GenerateToken(username, password string) (string, error) {
|
||||
nowTime := time.Now()
|
||||
expireTime := nowTime.Add(3 * time.Hour)
|
||||
clims := Claims{
|
||||
username,
|
||||
password,
|
||||
jwt.StandardClaims{
|
||||
ExpiresAt: expireTime.Unix(),
|
||||
Issuer: "gin-blog",
|
||||
},
|
||||
}
|
||||
|
||||
tokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, clims)
|
||||
token, err := tokenClaims.SignedString(jwtSecret)
|
||||
return token, err
|
||||
|
||||
}
|
||||
|
||||
//解析token
|
||||
func ParseToken(token string) (*Claims, error) {
|
||||
tokenClaims, err := jwt.ParseWithClaims(token, &Claims{}, func(token *jwt.Token) (interface{}, error) {
|
||||
return jwtSecret, nil
|
||||
})
|
||||
if tokenClaims != nil {
|
||||
if clims, ok := tokenClaims.Claims.(*Claims); ok && tokenClaims.Valid {
|
||||
return clims, nil
|
||||
}
|
||||
}
|
||||
return nil, err
|
||||
}
|
Reference in New Issue
Block a user