Skip to content

Commit

Permalink
Update jwt method
Browse files Browse the repository at this point in the history
  • Loading branch information
abericyang@gmail.com committed May 15, 2019
1 parent 6bee7dd commit b7ce169
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 27 deletions.
72 changes: 51 additions & 21 deletions utils/jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,64 @@ import (
"go.uber.org/zap"
)

var key []byte = []byte("Hello World!This is secret!")

func Build(sub, iss, jti string, iat, exp, nbf int64) (string, error) {
// "sub": "1", 该JWT所面向的用户
// "iss": "http://localhost:8000/user/sign_up", 该JWT的签发者
// "iat": 1451888119, 在什么时候签发的token
// "exp": 1454516119, token什么时候过期
// "nbf": 1451888119, token在此时间之前不能被接收处理
// "jti": "37c107e4609ddbcc9c096ea5ee76c667" token提供唯一标识
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.StandardClaims{
Subject: sub,
Issuer: iss,
Id: jti,
IssuedAt: iat,
NotBefore: nbf,
ExpiresAt: exp,
})
const (
SigningMethodHS256 = iota
SigningMethodHS384
SigningMethodHS512
)

type JWT struct {
Token *jwt.Token
}

// "sub": "1", 该JWT所面向的用户
// "iss": "http://localhost:8000/user/sign_up", 该JWT的签发者
// "iat": 1451888119, 在什么时候签发的token
// "exp": 1454516119, token什么时候过期
// "nbf": 1451888119, token在此时间之前不能被接收处理
// "jti": "37c107e4609ddbcc9c096ea5ee76c667" token提供唯一标识
func Build(method int, key interface{}, sub, iss, jti string, iat, nbf, exp int64) (string, error) {
var jwtMethod jwt.SigningMethod
switch method {
case SigningMethodHS256:
jwtMethod = jwt.SigningMethodHS256
case SigningMethodHS384:
jwtMethod = jwt.SigningMethodHS384
case SigningMethodHS512:
jwtMethod = jwt.SigningMethodHS512
default:
jwtMethod = jwt.SigningMethodHS256
}
return token(jwtMethod, key, sub, iss, jti, iat, nbf, exp)
}

func token(jwtMethod jwt.SigningMethod, key interface{}, sub, iss, jti string, iat, nbf, exp int64) (tokenString string, err error) {
token := &jwt.Token{
Header: map[string]interface{}{
"typ": "JWT",
"alg": jwtMethod.Alg(),
},
Claims: jwt.StandardClaims{
Subject: sub,
Issuer: iss,
Id: jti,
IssuedAt: iat,
NotBefore: nbf,
ExpiresAt: exp,
},
Method: jwtMethod,
}

// Sign and get the complete encoded token as a string using the secret
tokenString, err := token.SignedString(key)
tokenString, err = token.SignedString(key)

log.Common.Info("result", zap.String("token", tokenString), zap.Error(err))
return tokenString, err
return
}

func Check(token string) bool {
func Check(key interface{}, token string) bool {
_, err := jwt.Parse(token, func(*jwt.Token) (interface{}, error) {
return []byte("userMD5"), nil
return key, nil
})
if err != nil {
log.Common.Info("parase with claims failed.", zap.Error(err))
Expand Down
21 changes: 15 additions & 6 deletions utils/jwt/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,20 @@ import (
)

func TestBuild(t *testing.T) {
token, err := Build("1", "rivet", "userMD5", time.Now().Unix(), time.Now().Unix(), time.Now().Unix()+1000)
if nil == err {
bo := Check(token)
fmt.Println("bo = ", bo)
} else {
fmt.Println("err = ", err)
key := []byte("Hello World!This is secret!")
tokenString1, err1 := Build(SigningMethodHS256, key, "1", "rivet", "userMD5", time.Now().Unix(), time.Now().Unix(), time.Now().Unix()+1000)
tokenString2, err2 := Build(SigningMethodHS384, key, "1", "rivet", "userMD5", time.Now().Unix(), time.Now().Unix(), time.Now().Unix()+1000)
tokenString3, err3 := Build(SigningMethodHS512, key, "1", "rivet", "userMD5", time.Now().Unix(), time.Now().Unix(), time.Now().Unix()+1000)
if nil == err1 && nil == err2 && nil == err3 {
fmt.Println("tokenString1 = ", tokenString1)
fmt.Println("tokenString2 = ", tokenString2)
fmt.Println("tokenString3 = ", tokenString3)
time.Sleep(1 * time.Second)
bo1 := Check(key, tokenString1)
bo2 := Check(key, tokenString2)
bo3 := Check(key, tokenString3)
fmt.Println("bo1 = ", bo1)
fmt.Println("bo2 = ", bo2)
fmt.Println("bo3 = ", bo3)
}
}

0 comments on commit b7ce169

Please sign in to comment.