Skip to content

Commit

Permalink
refactor: math/rand 改为 v2
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Apr 24, 2024
1 parent 41c1fcf commit 5edf5f4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
4 changes: 2 additions & 2 deletions middlewares/auth/jwt/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package jwt
import (
"fmt"
"io/fs"
"math/rand"
"math/rand/v2"
"slices"
"time"

Expand Down Expand Up @@ -110,7 +110,7 @@ func (s *Signer) Sign(claims Claims) (string, error) {
case 1:
k = s.keys[0]
default:
k = s.keys[rand.Intn(l)]
k = s.keys[rand.IntN(l)]
}

t := jwt.NewWithClaims(k.sign, claims)
Expand Down
20 changes: 15 additions & 5 deletions middlewares/auth/jwt/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,28 @@ func (j *Verifier[T]) Middleware(next web.HandlerFunc) web.HandlerFunc {
return ctx.Problem(web.ProblemUnauthorized)
}

if baseToken := claims.BaseToken(); baseToken != "" { // 刷新令牌
if nbf, err := claims.GetNotBefore(); err == nil && nbf.After(ctx.Begin()) {
return ctx.Problem(web.ProblemForbidden)
}

if exp, err := claims.GetExpirationTime(); err == nil && exp.Before(ctx.Begin()) {
return ctx.Problem(web.ProblemForbidden)
}

if baseToken := claims.BaseToken(); baseToken != "" { // token 为刷新令牌
// 如果关联的访问令牌已经被主动丢弃,比如客户端主动退出了当前的登录等操作,
// 那么由该访问令牌生成的刷新令牌也将失效果。
if j.blocker.TokenIsBlocked(baseToken) {
return ctx.Problem(web.ProblemForbidden)
}

if err := j.blocker.BlockToken(token, true); err != nil {
ctx.Logs().ERROR().Error(err)
}

if err := j.blocker.BlockToken(baseToken, false); err != nil {
ctx.Logs().ERROR().Error(err)
}

if claims, resp = j.parseClaims(ctx, token); resp != nil { // 拿到刷新令牌关联的 claims
return resp
}
}

mauth.Set(ctx, claims)
Expand Down
8 changes: 4 additions & 4 deletions plugins/health/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package health

import (
"math/rand"
"math/rand/v2"
"net/http"
"strconv"
"testing"
Expand Down Expand Up @@ -34,19 +34,19 @@ func TestHealth(t *testing.T) {
if err != nil {
panic(err)
}
time.Sleep(time.Microsecond * time.Duration(rand.Int63n(100))) // 防止过快,无法记录用时。
time.Sleep(time.Microsecond * time.Duration(rand.Int64N(100))) // 防止过快,无法记录用时。
return web.Status(status)
})
r.Post("/", func(*web.Context) web.Responser {
time.Sleep(time.Microsecond * time.Duration(rand.Int63n(100))) // 防止过快,无法记录用时。
time.Sleep(time.Microsecond * time.Duration(rand.Int64N(100))) // 防止过快,无法记录用时。
return nil
})
r.Delete("/users", func(ctx *web.Context) web.Responser {
status, err := strconv.Atoi(ctx.Request().FormValue("status"))
if err != nil {
panic(err)
}
time.Sleep(time.Microsecond * time.Duration(rand.Int63n(100))) // 防止过快,无法记录用时。
time.Sleep(time.Microsecond * time.Duration(rand.Int64N(100))) // 防止过快,无法记录用时。
return web.Status(status)
})

Expand Down

0 comments on commit 5edf5f4

Please sign in to comment.