|
123456789101112131415161718192021222324252627282930313233343536373839 |
- package auth
-
- import (
- "errors"
- "github.com/dgrijalva/jwt-go"
- "time"
- )
-
-
- func GenToken(admId int, username string) (string, error) {
-
- c := JWTUser{
- AdmId: admId,
- Username: username,
- StandardClaims: jwt.StandardClaims{
- ExpiresAt: time.Now().Add(TokenExpireDuration).Unix(),
- Issuer: "egg_admin",
- },
- }
-
- token := jwt.NewWithClaims(jwt.SigningMethodHS256, c)
-
- return token.SignedString(Secret)
- }
-
-
- func ParseToken(tokenString string) (*JWTUser, error) {
-
- token, err := jwt.ParseWithClaims(tokenString, &JWTUser{}, func(token *jwt.Token) (i interface{}, err error) {
- return Secret, nil
- })
- if err != nil {
- return nil, err
- }
- if claims, ok := token.Claims.(*JWTUser); ok && token.Valid {
- return claims, nil
- }
- return nil, errors.New("invalid token")
- }
|