@@ -13,33 +13,81 @@ import (
13
13
"encoding/json"
14
14
"encoding/pem"
15
15
"strings"
16
+ "sync"
16
17
"testing"
17
18
"time"
18
19
19
20
"golang.org/x/oauth2/jws"
20
21
)
21
22
23
+ var (
24
+ privateKey * rsa.PrivateKey
25
+ jsonKey []byte
26
+ once sync.Once
27
+ )
28
+
22
29
func TestJWTAccessTokenSourceFromJSON (t * testing.T ) {
23
- // Generate a key we can use in the test data.
24
- privateKey , err := rsa .GenerateKey (rand .Reader , 2048 )
30
+ setupDummyKey (t )
31
+
32
+ ts , err := JWTAccessTokenSourceFromJSON (jsonKey , "audience" )
25
33
if err != nil {
26
- t .Fatal ( err )
34
+ t .Fatalf ( "JWTAccessTokenSourceFromJSON: %v \n JSON: %s" , err , string ( jsonKey ) )
27
35
}
28
36
29
- // Encode the key and substitute into our example JSON.
30
- enc := pem .EncodeToMemory (& pem.Block {
31
- Type : "PRIVATE KEY" ,
32
- Bytes : x509 .MarshalPKCS1PrivateKey (privateKey ),
33
- })
34
- enc , err = json .Marshal (string (enc ))
37
+ tok , err := ts .Token ()
35
38
if err != nil {
36
- t .Fatalf ("json.Marshal : %v" , err )
39
+ t .Fatalf ("Token : %v" , err )
37
40
}
38
- jsonKey := bytes .Replace (jwtJSONKey , []byte (`"super secret key"` ), enc , 1 )
39
41
40
- ts , err := JWTAccessTokenSourceFromJSON (jsonKey , "audience" )
42
+ if got , want := tok .TokenType , "Bearer" ; got != want {
43
+ t .Errorf ("TokenType = %q, want %q" , got , want )
44
+ }
45
+ if got := tok .Expiry ; tok .Expiry .Before (time .Now ()) {
46
+ t .Errorf ("Expiry = %v, should not be expired" , got )
47
+ }
48
+
49
+ err = jws .Verify (tok .AccessToken , & privateKey .PublicKey )
41
50
if err != nil {
42
- t .Fatalf ("JWTAccessTokenSourceFromJSON: %v\n JSON: %s" , err , string (jsonKey ))
51
+ t .Errorf ("jws.Verify on AccessToken: %v" , err )
52
+ }
53
+
54
+ claim , err := jws .Decode (tok .AccessToken )
55
+ if err != nil {
56
+ t .Fatalf ("jws.Decode on AccessToken: %v" , err )
57
+ }
58
+
59
+ if got , want := claim .Iss , "gopher@developer.gserviceaccount.com" ; got != want {
60
+ t .Errorf ("Iss = %q, want %q" , got , want )
61
+ }
62
+ if got , want := claim .Sub , "gopher@developer.gserviceaccount.com" ; got != want {
63
+ t .Errorf ("Sub = %q, want %q" , got , want )
64
+ }
65
+ if got , want := claim .Aud , "audience" ; got != want {
66
+ t .Errorf ("Aud = %q, want %q" , got , want )
67
+ }
68
+
69
+ // Finally, check the header private key.
70
+ parts := strings .Split (tok .AccessToken , "." )
71
+ hdrJSON , err := base64 .RawURLEncoding .DecodeString (parts [0 ])
72
+ if err != nil {
73
+ t .Fatalf ("base64 DecodeString: %v\n String: %q" , err , parts [0 ])
74
+ }
75
+ var hdr jws.Header
76
+ if err := json .Unmarshal ([]byte (hdrJSON ), & hdr ); err != nil {
77
+ t .Fatalf ("json.Unmarshal: %v (%q)" , err , hdrJSON )
78
+ }
79
+
80
+ if got , want := hdr .KeyID , "268f54e43a1af97cfc71731688434f45aca15c8b" ; got != want {
81
+ t .Errorf ("Header KeyID = %q, want %q" , got , want )
82
+ }
83
+ }
84
+
85
+ func TestJWTAccessTokenSourceWithScope (t * testing.T ) {
86
+ setupDummyKey (t )
87
+
88
+ ts , err := JWTAccessTokenSourceWithScope (jsonKey , "scope1" , "scope2" )
89
+ if err != nil {
90
+ t .Fatalf ("JWTAccessTokenSourceWithScope: %v\n JSON: %s" , err , string (jsonKey ))
43
91
}
44
92
45
93
tok , err := ts .Token ()
@@ -70,7 +118,7 @@ func TestJWTAccessTokenSourceFromJSON(t *testing.T) {
70
118
if got , want := claim .Sub , "gopher@developer.gserviceaccount.com" ; got != want {
71
119
t .Errorf ("Sub = %q, want %q" , got , want )
72
120
}
73
- if got , want := claim .Aud , "audience " ; got != want {
121
+ if got , want := claim .Scope , "scope1 scope2 " ; got != want {
74
122
t .Errorf ("Aud = %q, want %q" , got , want )
75
123
}
76
124
@@ -89,3 +137,24 @@ func TestJWTAccessTokenSourceFromJSON(t *testing.T) {
89
137
t .Errorf ("Header KeyID = %q, want %q" , got , want )
90
138
}
91
139
}
140
+
141
+ func setupDummyKey (t * testing.T ) {
142
+ once .Do (func () {
143
+ // Generate a key we can use in the test data.
144
+ pk , err := rsa .GenerateKey (rand .Reader , 2048 )
145
+ if err != nil {
146
+ t .Fatal (err )
147
+ }
148
+ privateKey = pk
149
+ // Encode the key and substitute into our example JSON.
150
+ enc := pem .EncodeToMemory (& pem.Block {
151
+ Type : "PRIVATE KEY" ,
152
+ Bytes : x509 .MarshalPKCS1PrivateKey (privateKey ),
153
+ })
154
+ enc , err = json .Marshal (string (enc ))
155
+ if err != nil {
156
+ t .Fatalf ("json.Marshal: %v" , err )
157
+ }
158
+ jsonKey = bytes .Replace (jwtJSONKey , []byte (`"super secret key"` ), enc , 1 )
159
+ })
160
+ }
0 commit comments