15
15
package auth
16
16
17
17
import (
18
+ "encoding/json"
18
19
"errors"
20
+ "fmt"
19
21
"io/ioutil"
20
22
"log"
21
23
"os"
@@ -58,17 +60,16 @@ func TestMain(m *testing.M) {
58
60
log .Fatalln (err )
59
61
}
60
62
} else {
61
- opt := option . WithCredentialsFile ( "../testdata/service_account.json" )
62
- creds , err = transport .Creds (context . Background (), opt )
63
+ ctx = context . Background ( )
64
+ creds , err = transport .Creds (ctx , option . WithCredentialsFile ( "../testdata/service_account.json" ) )
63
65
if err != nil {
64
66
log .Fatalln (err )
65
67
}
66
68
67
69
ks = & fileKeySource {FilePath : "../testdata/public_certs.json" }
68
70
}
69
71
70
- client , err = NewClient (& internal.AuthConfig {
71
- Ctx : ctx ,
72
+ client , err = NewClient (ctx , & internal.AuthConfig {
72
73
Creds : creds ,
73
74
ProjectID : "mock-project-id" ,
74
75
})
@@ -81,6 +82,32 @@ func TestMain(m *testing.M) {
81
82
os .Exit (m .Run ())
82
83
}
83
84
85
+ func TestNewClientInvalidCredentials (t * testing.T ) {
86
+ creds := & google.DefaultCredentials {
87
+ JSON : []byte ("foo" ),
88
+ }
89
+ conf := & internal.AuthConfig {Creds : creds }
90
+ if c , err := NewClient (context .Background (), conf ); c != nil || err == nil {
91
+ t .Errorf ("NewCient() = (%v,%v); want = (nil, error)" , c , err )
92
+ }
93
+ }
94
+
95
+ func TestNewClientInvalidPrivateKey (t * testing.T ) {
96
+ sa := map [string ]interface {}{
97
+ "private_key" : "foo" ,
98
+ "client_email" :
"[email protected] " ,
99
+ }
100
+ b , err := json .Marshal (sa )
101
+ if err != nil {
102
+ t .Fatal (err )
103
+ }
104
+ creds := & google.DefaultCredentials {JSON : b }
105
+ conf := & internal.AuthConfig {Creds : creds }
106
+ if c , err := NewClient (context .Background (), conf ); c != nil || err == nil {
107
+ t .Errorf ("NewCient() = (%v,%v); want = (nil, error)" , c , err )
108
+ }
109
+ }
110
+
84
111
func TestCustomToken (t * testing.T ) {
85
112
token , err := client .CustomToken ("user1" )
86
113
if err != nil {
@@ -118,31 +145,32 @@ func TestCustomTokenError(t *testing.T) {
118
145
}{
119
146
{"EmptyName" , "" , nil },
120
147
{"LongUid" , strings .Repeat ("a" , 129 ), nil },
121
- {"ReservedClaims" , "uid" , map [string ]interface {}{"sub" : "1234" }},
148
+ {"ReservedClaim" , "uid" , map [string ]interface {}{"sub" : "1234" }},
149
+ {"ReservedClaims" , "uid" , map [string ]interface {}{"sub" : "1234" , "aud" : "foo" }},
122
150
}
123
151
124
152
for _ , tc := range cases {
125
153
token , err := client .CustomTokenWithClaims (tc .uid , tc .claims )
126
154
if token != "" || err == nil {
127
- t .Errorf ("CustomTokenWithClaims(%q) = (%q, %v); want: (\" \" , error)" , tc .name , token , err )
155
+ t .Errorf ("CustomTokenWithClaims(%q) = (%q, %v); want = (\" \" , error)" , tc .name , token , err )
128
156
}
129
157
}
130
158
}
131
159
132
160
func TestCustomTokenInvalidCredential (t * testing.T ) {
133
- s , err := NewClient (& internal. AuthConfig { Ctx : context .Background ()})
161
+ s , err := NewClient (context .Background (), & internal. AuthConfig { })
134
162
if err != nil {
135
163
t .Fatal (err )
136
164
}
137
165
138
166
token , err := s .CustomToken ("user1" )
139
167
if token != "" || err == nil {
140
- t .Errorf ("CustomTokenWithClaims() = (%q, %v); want: (\" \" , error)" , token , err )
168
+ t .Errorf ("CustomTokenWithClaims() = (%q, %v); want = (\" \" , error)" , token , err )
141
169
}
142
170
143
171
token , err = s .CustomTokenWithClaims ("user1" , map [string ]interface {}{"foo" : "bar" })
144
172
if token != "" || err == nil {
145
- t .Errorf ("CustomTokenWithClaims() = (%q, %v); want: (\" \" , error)" , token , err )
173
+ t .Errorf ("CustomTokenWithClaims() = (%q, %v); want = (\" \" , error)" , token , err )
146
174
}
147
175
}
148
176
@@ -152,15 +180,23 @@ func TestVerifyIDToken(t *testing.T) {
152
180
t .Fatal (err )
153
181
}
154
182
if ft .Claims ["admin" ] != true {
155
- t .Errorf ("Claims['admin'] = %v; want: true" , ft .Claims ["admin" ])
183
+ t .Errorf ("Claims['admin'] = %v; want = true" , ft .Claims ["admin" ])
156
184
}
157
185
if ft .UID != ft .Subject {
158
186
t .Errorf ("UID = %q; Sub = %q; want UID = Sub" , ft .UID , ft .Subject )
159
187
}
160
188
}
161
189
190
+ func TestVerifyIDTokenInvalidSignature (t * testing.T ) {
191
+ parts := strings .Split (testIDToken , "." )
192
+ token := fmt .Sprintf ("%s:%s:invalidsignature" , parts [0 ], parts [1 ])
193
+ if ft , err := client .VerifyIDToken (token ); ft != nil || err == nil {
194
+ t .Errorf ("VerifyiDToken('invalid-signature') = (%v, %v); want = (nil, error)" , ft , err )
195
+ }
196
+ }
197
+
162
198
func TestVerifyIDTokenError (t * testing.T ) {
163
- var now int64 = 1000
199
+ now := time . Now (). Unix ()
164
200
cases := []struct {
165
201
name string
166
202
token string
@@ -172,28 +208,24 @@ func TestVerifyIDTokenError(t *testing.T) {
172
208
{"EmptySubject" , getIDToken (mockIDTokenPayload {"sub" : "" })},
173
209
{"IntSubject" , getIDToken (mockIDTokenPayload {"sub" : 10 })},
174
210
{"LongSubject" , getIDToken (mockIDTokenPayload {"sub" : strings .Repeat ("a" , 129 )})},
175
- {"FutureToken" , getIDToken (mockIDTokenPayload {"iat" : time . Unix ( now + 1 , 0 ) })},
211
+ {"FutureToken" , getIDToken (mockIDTokenPayload {"iat" : now + 1000 })},
176
212
{"ExpiredToken" , getIDToken (mockIDTokenPayload {
177
- "iat" : time . Unix ( now - 10 , 0 ) ,
178
- "exp" : time . Unix ( now - 1 , 0 ) ,
213
+ "iat" : now - 1000 ,
214
+ "exp" : now - 100 ,
179
215
})},
180
216
{"EmptyToken" , "" },
181
217
{"BadFormatToken" , "foobar" },
182
218
}
183
219
184
- clk = & mockClock {now : time .Unix (now , 0 )}
185
- defer func () {
186
- clk = & systemClock {}
187
- }()
188
220
for _ , tc := range cases {
189
221
if _ , err := client .VerifyIDToken (tc .token ); err == nil {
190
- t .Errorf ("VerifyyIDToken (%q) = nil; want error" , tc .name )
222
+ t .Errorf ("VerifyIDToken (%q) = nil; want error" , tc .name )
191
223
}
192
224
}
193
225
}
194
226
195
227
func TestNoProjectID (t * testing.T ) {
196
- c , err := NewClient (& internal. AuthConfig { Ctx : context .Background ()})
228
+ c , err := NewClient (context .Background (), & internal. AuthConfig { })
197
229
if err != nil {
198
230
t .Fatal (err )
199
231
}
0 commit comments