Skip to content

Commit

Permalink
refactor(middlewares/auth/jwt): Clamis.BuildRefresh 可自定义时间
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Mar 19, 2024
1 parent 7ecc3c0 commit 14d33e1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
11 changes: 8 additions & 3 deletions middlewares/auth/jwt/claims.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@

package jwt

import "github.com/golang-jwt/jwt/v5"
import (
"time"

"github.com/golang-jwt/jwt/v5"
)

// Claims Claims 对象需要实现的接口
type Claims interface {
jwt.Claims

// BuildRefresh 根据令牌 token 生成刷新令牌的 [Claims]
//
// 返回对象也是 [Claims] 的实现,可以通过返回对象的 [Claims.BaseToken] 获得关联的基础令牌。
BuildRefresh(string) Claims
// token 生成的刷新令牌与此令牌相关联,此值可通过返回对象的 [Claims.BaseToken] 返回;
// created 表示刷新令牌的刷新时间,一般为 [time.Now];
BuildRefresh(token string, created time.Time) Claims

// BaseToken 刷新令牌关联的令牌
//
Expand Down
4 changes: 2 additions & 2 deletions middlewares/auth/jwt/claims_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type testClaims struct {

func (c *testClaims) BaseToken() string { return c.Token }

func (c *testClaims) BuildRefresh(token string) Claims {
return &testClaims{Token: token, Created: time.Now(), ID: c.ID}
func (c *testClaims) BuildRefresh(token string, created time.Time) Claims {
return &testClaims{Token: token, Created: created, ID: c.ID}
}

func (c *testClaims) Valid() error { return nil }
2 changes: 1 addition & 1 deletion middlewares/auth/jwt/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (s *Signer) Render(ctx *web.Context, status int, accessClaims Claims) web.R

var refreshToken string
if s.refresh {
refreshToken, err = s.Sign(accessClaims.BuildRefresh(accessToken))
refreshToken, err = s.Sign(accessClaims.BuildRefresh(accessToken, ctx.Begin()))
if err != nil {
return ctx.Error(err, "")
}
Expand Down

0 comments on commit 14d33e1

Please sign in to comment.