JWT 返回过期时间

This commit is contained in:
Max 2021-11-21 22:52:58 +08:00
parent 5fd1c739ac
commit be9b0007cb
2 changed files with 9 additions and 5 deletions

View file

@ -38,7 +38,7 @@ func JwtValidate(tokenString string) map[string]interface{} {
// JwtMake 生成 JWT
// subject options[0], audience options[1], issuer options[1]
func JwtMake(id int, data map[string]interface{}, timeout int64, options ...string) string {
func JwtMake(id int, data map[string]interface{}, timeout int64, options ...string) map[string]interface{} {
now := time.Now().Unix()
expiresAt := now + timeout
uid := fmt.Sprintf("%d", id)
@ -73,7 +73,10 @@ func JwtMake(id int, data map[string]interface{}, timeout int64, options ...stri
if err != nil {
exception.New("生成令牌失败", 500).Ctx(err).Throw()
}
return tokenString
return map[string]interface{}{
"token": tokenString,
"expires_at": expiresAt,
}
}
// ProcessJwtMake xiang.helper.JwtMake 生成JWT

View file

@ -10,7 +10,8 @@ import (
func TestJwt(t *testing.T) {
data := map[string]interface{}{"hello": "world", "id": 1}
tokenString := JwtMake(1, data, 1, "Unit Test", "Test", "UnitTest")
token := JwtMake(1, data, 1, "Unit Test", "Test", "UnitTest")
tokenString := token["token"].(string)
res := JwtValidate(tokenString)
assert.Equal(t, float64(1), res["id"])
assert.Equal(t, "world", res["hello"])
@ -22,8 +23,8 @@ func TestProcessJwt(t *testing.T) {
data := map[string]interface{}{"hello": "world", "id": 1}
args := []interface{}{1, data, 1, "Unit Test", "Test", "UnitTest"}
process := gou.NewProcess("xiang.helper.JwtMake", args...)
token := process.Run()
tokenString := token.(string)
token := process.Run().(map[string]interface{})
tokenString := token["token"].(string)
res := gou.NewProcess("xiang.helper.JwtValidate", tokenString).Run().(map[string]interface{})
assert.Equal(t, float64(1), res["id"])
assert.Equal(t, "world", res["hello"])