auth.go
946 字节
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package domain
import (
"github.com/dgrijalva/jwt-go"
eggJwt "github.com/linmadan/egglib-go/utils/jwt"
"time"
)
const (
secret = "$^%$Rdefault&*^(*&"
)
const (
AccessTokenExpire = 3600
)
type UserTokenClaim struct {
jwt.StandardClaims
UserId int64 `json:"userId"`
CompanyId int64 `json:"companyId"`
}
func NewUserTokenClaim(userId, companyId int64, expire int64) UserTokenClaim {
return UserTokenClaim{
UserId: userId,
CompanyId: companyId,
StandardClaims: jwt.StandardClaims{ExpiresAt: expire},
}
}
func SignToken(userId int64, companyId int64) string {
claim := NewUserTokenClaim(int64(userId), companyId, time.Now().Add(time.Second*AccessTokenExpire).Unix())
token, _ := eggJwt.Sign(claim, secret, "")
return token
}
func ValidToken(token string, claims *UserTokenClaim) (bool, error) {
result, c, err := eggJwt.Valid(token, claims, secret)
claims = c.Claims.(*UserTokenClaim)
return result, err
}