@@ -13,33 +13,81 @@ import (
1313 "encoding/json"
1414 "encoding/pem"
1515 "strings"
16+ "sync"
1617 "testing"
1718 "time"
1819
1920 "golang.org/x/oauth2/jws"
2021)
2122
23+ var (
24+ privateKey * rsa.PrivateKey
25+ jsonKey []byte
26+ once sync.Once
27+ )
28+
2229func 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" )
2533 if err != nil {
26- t .Fatal ( err )
34+ t .Fatalf ( "JWTAccessTokenSourceFromJSON: %v \n JSON: %s" , err , string ( jsonKey ) )
2735 }
2836
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 ()
3538 if err != nil {
36- t .Fatalf ("json.Marshal : %v" , err )
39+ t .Fatalf ("Token : %v" , err )
3740 }
38- jsonKey := bytes .Replace (jwtJSONKey , []byte (`"super secret key"` ), enc , 1 )
3941
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 )
4150 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 ,
"[email protected] " ;
got != want {
60+ t .Errorf ("Iss = %q, want %q" , got , want )
61+ }
62+ if got ,
want := claim .
Sub ,
"[email protected] " ;
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 ))
4391 }
4492
4593 tok , err := ts .Token ()
@@ -70,7 +118,7 @@ func TestJWTAccessTokenSourceFromJSON(t *testing.T) {
70118 if got ,
want := claim .
Sub ,
"[email protected] " ;
got != want {
71119 t .Errorf ("Sub = %q, want %q" , got , want )
72120 }
73- if got , want := claim .Aud , "audience " ; got != want {
121+ if got , want := claim .Scope , "scope1 scope2 " ; got != want {
74122 t .Errorf ("Aud = %q, want %q" , got , want )
75123 }
76124
@@ -89,3 +137,24 @@ func TestJWTAccessTokenSourceFromJSON(t *testing.T) {
89137 t .Errorf ("Header KeyID = %q, want %q" , got , want )
90138 }
91139}
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