Skip to content

Commit e17001a

Browse files
authored
Merge pull request #47 from xushiwei/test
yap/ytest/auth/jwt
2 parents 545b578 + 28a5e7d commit e17001a

File tree

6 files changed

+123
-17
lines changed

6 files changed

+123
-17
lines changed

go.mod

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/goplus/yap
22

33
go 1.18
44

5-
require github.com/qiniu/x v1.13.3-0.20240129201727-4013f184c5c7
5+
require (
6+
github.com/qiniu/x v1.13.3-0.20240129201727-4013f184c5c7
7+
github.com/golang-jwt/jwt/v5 v5.2.0
8+
)

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
github.com/golang-jwt/jwt/v5 v5.2.0 h1:d/ix8ftRUorsN+5eMIlF4T6J8CAt9rch3My2winC1Jw=
2+
github.com/golang-jwt/jwt/v5 v5.2.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
13
github.com/qiniu/x v1.13.3-0.20240129201727-4013f184c5c7 h1:rzpGloMVgWexXti9yRXMH8aAdjNy8rZrlAQdKvzZeMg=
24
github.com/qiniu/x v1.13.3-0.20240129201727-4013f184c5c7/go.mod h1:INZ2TSWSJVWO/RuELQROERcslBwVgFG7MkTfEdaQz9E=

gop.mod

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ project _yap.gox App github.com/goplus/yap
44

55
project _yapt.gox App github.com/goplus/yap/ytest
66
class _yapt.gox Case
7+
import github.com/goplus/yap/ytest/auth/jwt
78

89
project _ytest.gox App github.com/goplus/yap/ytest
910
class _ytest.gox Case
11+
import github.com/goplus/yap/ytest/auth/jwt

ytest/auth/auth.go

+21
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,24 @@ import (
2424
type RTComposer interface {
2525
Compose(base http.RoundTripper) http.RoundTripper
2626
}
27+
28+
// -----------------------------------------------------------------------------
29+
30+
type tokenRT struct {
31+
rt http.RoundTripper
32+
token string
33+
}
34+
35+
func (p *tokenRT) RoundTrip(req *http.Request) (*http.Response, error) {
36+
req.Header.Set("Authorization", p.token)
37+
return p.rt.RoundTrip(req)
38+
}
39+
40+
func WithToken(rt http.RoundTripper, token string) http.RoundTripper {
41+
return &tokenRT{
42+
rt: rt,
43+
token: token,
44+
}
45+
}
46+
47+
// -----------------------------------------------------------------------------

ytest/auth/bearer/token.go

+1-16
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,12 @@ import (
2424

2525
// -----------------------------------------------------------------------------
2626

27-
type tokenRounderTripper struct {
28-
rt http.RoundTripper
29-
token string
30-
}
31-
32-
func (p *tokenRounderTripper) RoundTrip(req *http.Request) (*http.Response, error) {
33-
req.Header.Set("Authorization", "Bearer "+p.token)
34-
return p.rt.RoundTrip(req)
35-
}
36-
37-
// -----------------------------------------------------------------------------
38-
3927
type tokenAuth struct {
4028
token string
4129
}
4230

4331
func (p *tokenAuth) Compose(rt http.RoundTripper) http.RoundTripper {
44-
return &tokenRounderTripper{
45-
rt: rt,
46-
token: p.token,
47-
}
32+
return auth.WithToken(rt, "Bearer "+p.token)
4833
}
4934

5035
// New creates an Authorization by specified token.

ytest/auth/jwt/jwt.go

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package jwt
18+
19+
import (
20+
"log"
21+
"net/http"
22+
"time"
23+
24+
"github.com/golang-jwt/jwt/v5"
25+
"github.com/goplus/yap/ytest/auth"
26+
)
27+
28+
// -----------------------------------------------------------------------------
29+
30+
type Signaturer struct {
31+
method jwt.SigningMethod
32+
claims jwt.MapClaims
33+
secret any
34+
}
35+
36+
func (p *Signaturer) Set(k string, v any) *Signaturer {
37+
p.claims[k] = v
38+
return p
39+
}
40+
41+
func (p *Signaturer) Audience(aud ...string) *Signaturer {
42+
p.claims["aud"] = jwt.ClaimStrings(aud)
43+
return p
44+
}
45+
46+
func (p *Signaturer) Expiration(exp time.Time) *Signaturer {
47+
p.claims["exp"] = jwt.NewNumericDate(exp)
48+
return p
49+
}
50+
51+
func (p *Signaturer) NotBefore(nbf time.Time) *Signaturer {
52+
p.claims["nbf"] = jwt.NewNumericDate(nbf)
53+
return p
54+
}
55+
56+
func (p *Signaturer) IssuedAt(iat time.Time) *Signaturer {
57+
p.claims["iat"] = jwt.NewNumericDate(iat)
58+
return p
59+
}
60+
61+
func (p *Signaturer) Compose(rt http.RoundTripper) http.RoundTripper {
62+
token := jwt.NewWithClaims(p.method, p.claims)
63+
raw, err := token.SignedString(p.secret)
64+
if err != nil {
65+
log.Panicln("jwt token.SignedString:", err)
66+
}
67+
return auth.WithToken(rt, "Bearer "+raw)
68+
}
69+
70+
func newSign(method jwt.SigningMethod, secret any) *Signaturer {
71+
return &Signaturer{
72+
method: method,
73+
claims: make(jwt.MapClaims),
74+
secret: secret,
75+
}
76+
}
77+
78+
// HS256 creates a signing methods by using the HMAC-SHA256.
79+
func HS256(key []byte) *Signaturer {
80+
return newSign(jwt.SigningMethodHS256, key)
81+
}
82+
83+
// HS384 creates a signing methods by using the HMAC-SHA384.
84+
func HS384(key []byte) *Signaturer {
85+
return newSign(jwt.SigningMethodHS384, key)
86+
}
87+
88+
// HS512 creates a signing methods by using the HMAC-SHA512.
89+
func HS512(key []byte) *Signaturer {
90+
return newSign(jwt.SigningMethodHS512, key)
91+
}
92+
93+
// -----------------------------------------------------------------------------

0 commit comments

Comments
 (0)