-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_test.go
142 lines (117 loc) · 3.57 KB
/
common_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package auth0
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"time"
"gopkg.in/go-jose/go-jose.v2"
"gopkg.in/go-jose/go-jose.v2/jwt"
)
var (
emptyAudience = []string{}
emptyIssuer = ""
defaultAudience = []string{"audience"}
defaultIssuer = "issuer"
// The default generated token by Chrome jwt extension
defaultSecret = []byte("secret")
defaultSecretProvider = NewKeyProvider(defaultSecret)
defaultSecretRS256 = genRSASSAJWK(jose.RS256, "")
defaultSecretProviderRS256 = NewKeyProvider(defaultSecretRS256.Public())
defaultSecretES384 = genECDSAJWK(jose.ES384, "")
defaultSecretProviderES384 = NewKeyProvider(defaultSecretES384.Public())
)
func genRSASSAJWK(sigAlg jose.SignatureAlgorithm, kid string) jose.JSONWebKey {
var bits int
if sigAlg == jose.RS256 {
bits = 2048
}
if sigAlg == jose.RS512 {
bits = 4096
}
key, _ := rsa.GenerateKey(rand.Reader, bits)
jsonWebKey := jose.JSONWebKey{
Key: key,
KeyID: kid,
Use: "sig",
Algorithm: string(sigAlg),
}
return jsonWebKey
}
func genECDSAJWK(sigAlg jose.SignatureAlgorithm, kid string) jose.JSONWebKey {
var c elliptic.Curve
if sigAlg == jose.ES256 {
c = elliptic.P256()
}
if sigAlg == jose.ES384 {
c = elliptic.P384()
}
key, _ := ecdsa.GenerateKey(c, rand.Reader)
jsonWebKey := jose.JSONWebKey{
Key: key,
KeyID: kid,
Algorithm: string(sigAlg),
}
return jsonWebKey
}
func getTestToken(audience []string, issuer string, expTime time.Time, alg jose.SignatureAlgorithm, key interface{}) string {
signer, err := jose.NewSigner(jose.SigningKey{Algorithm: alg, Key: key}, (&jose.SignerOptions{}).WithType("JWT"))
if err != nil {
panic(err)
}
cl := jwt.Claims{
Issuer: issuer,
Audience: audience,
IssuedAt: jwt.NewNumericDate(time.Now().UTC()),
Expiry: jwt.NewNumericDate(expTime),
}
raw, err := jwt.Signed(signer).Claims(cl).CompactSerialize()
if err != nil {
panic(err)
}
return raw
}
func getTestTokenWithKid(audience []string, issuer string, expTime time.Time, alg jose.SignatureAlgorithm, key interface{}, kid string) string {
signer, err := jose.NewSigner(jose.SigningKey{Algorithm: alg, Key: key}, (&jose.SignerOptions{ExtraHeaders: map[jose.HeaderKey]interface{}{"kid": kid}}).WithType("JWT"))
if err != nil {
panic(err)
}
cl := jwt.Claims{
Issuer: issuer,
Audience: audience,
IssuedAt: jwt.NewNumericDate(time.Now().UTC()),
Expiry: jwt.NewNumericDate(expTime),
}
raw, err := jwt.Signed(signer).Claims(cl).CompactSerialize()
if err != nil {
panic(err)
}
return raw
}
func genNewTestServer(genJWKS bool) (JWKClientOptions, string, string, error) {
// Generate JWKs
jsonWebKeyRS256 := genRSASSAJWK(jose.RS256, "keyRS256")
jsonWebKeyES384 := genECDSAJWK(jose.ES384, "keyES384")
// Generate JWKS
jwks := JWKS{
Keys: []jose.JSONWebKey{},
}
if genJWKS {
jwks = JWKS{
Keys: []jose.JSONWebKey{jsonWebKeyRS256.Public(), jsonWebKeyES384.Public()},
}
}
value, err := json.Marshal(&jwks)
// Generate Tokens
tokenRS256 := getTestTokenWithKid(defaultAudience, defaultIssuer, time.Now().Add(24*time.Hour), jose.RS256, jsonWebKeyRS256, "keyRS256")
tokenES384 := getTestTokenWithKid(defaultAudience, defaultIssuer, time.Now().Add(24*time.Hour), jose.ES384, jsonWebKeyES384, "keyES384")
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintln(w, string(value))
}))
return JWKClientOptions{URI: ts.URL}, tokenRS256, tokenES384, err
}