-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathverify_test.go
89 lines (79 loc) · 2.22 KB
/
verify_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
package chainjwt
import (
"testing"
"time"
"golang.org/x/crypto/ed25519"
"github.com/ScaleFT/xjwt"
"github.com/stretchr/testify/require"
jose "gopkg.in/square/go-jose.v2"
"gopkg.in/square/go-jose.v2/jwt"
)
func TestVerifyBasic(t *testing.T) {
innerPub, innerKey, err := ed25519.GenerateKey(nil)
require.NoError(t, err)
outerPub, outerKey, err := ed25519.GenerateKey(nil)
require.NoError(t, err)
innerSigner, err := jose.NewSigner(jose.SigningKey{Algorithm: jose.EdDSA, Key: jose.JSONWebKey{
Key: innerKey,
KeyID: "E29A899C",
Algorithm: string(jose.EdDSA),
}}, &jose.SignerOptions{
NonceSource: &xjwt.RandomNonce{Size: 8},
})
require.NoError(t, err)
now := time.Now()
innerJWT, err := jwt.Signed(innerSigner).Claims(jwt.Claims{
ID: "03EC5EF4",
Subject: "Client X",
NotBefore: jwt.NewNumericDate(now.Add(time.Second * -30)),
IssuedAt: jwt.NewNumericDate(now),
Expiry: jwt.NewNumericDate(now.Add(time.Second * 30)),
Issuer: "api.example.com",
Audience: jwt.Audience{"api.example.com"},
}).Claims(
&TrustJWKClaim{
TrustJWK: jose.JSONWebKey{
Key: outerPub,
KeyID: "BE60DFC8-K1",
Algorithm: string(jose.EdDSA),
},
},
).CompactSerialize()
require.NoError(t, err)
require.NotEmpty(t, innerJWT)
claims := jwt.Claims{
Subject: "BE60DFC8",
NotBefore: jwt.NewNumericDate(now.Add(time.Second * -30)),
IssuedAt: jwt.NewNumericDate(now),
Expiry: jwt.NewNumericDate(now.Add(time.Second * 30)),
Issuer: "BE60DFC8",
Audience: jwt.Audience{"api.example.com"},
}
output, err := Create(CreateOptions{
Claims: claims,
Key: jose.SigningKey{Algorithm: jose.EdDSA, Key: jose.JSONWebKey{
Key: outerKey,
KeyID: "BE60DFC8-K1",
Algorithm: string(jose.EdDSA),
}},
JWSChain: innerJWT,
})
require.NoError(t, err)
require.NotEmpty(t, output)
// spew.Dump(output)
rv, err := Verify([]byte(output), &VerifyConfig{
ExpectedIssuer: "api.example.com",
ExpectedAudience: "api.example.com",
KeySet: &jose.JSONWebKeySet{
Keys: []jose.JSONWebKey{
jose.JSONWebKey{
Key: innerPub,
KeyID: "E29A899C",
Algorithm: string(jose.EdDSA),
},
},
},
})
require.NoError(t, err)
require.NotEmpty(t, rv)
}