code refactor (#1786)

* implement LDAP mock client

Signed-off-by: hongming <talonwan@yunify.com>

* update

Signed-off-by: hongming <talonwan@yunify.com>

* update

Signed-off-by: hongming <talonwan@yunify.com>

* resolve conflict

Signed-off-by: hongming <talonwan@yunify.com>
This commit is contained in:
hongming
2020-02-24 15:39:36 +08:00
committed by GitHub
parent 96aee0e60b
commit abf9fee845
39 changed files with 1338 additions and 2467 deletions

View File

@@ -20,19 +20,26 @@ package jwtutil
import (
"fmt"
"github.com/dgrijalva/jwt-go"
"os"
)
const secretEnv = "JWT_SECRET"
var secret []byte
var secretKey []byte
func Setup(key string) {
secret = []byte(key)
func init() {
if secret := os.Getenv(secretEnv); secret != "" {
Setup(secret)
}
}
func Setup(secret string) {
secretKey = []byte(secret)
}
func MustSigned(claims jwt.MapClaims) string {
uToken := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
token, err := uToken.SignedString(secret)
token, err := uToken.SignedString(secretKey)
if err != nil {
panic(err)
}
@@ -41,7 +48,7 @@ func MustSigned(claims jwt.MapClaims) string {
func provideKey(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); ok {
return secret, nil
return secretKey, nil
} else {
return nil, fmt.Errorf("expect token signed with HMAC but got %v", token.Header["alg"])
}