Repository navigation
Expand file tree
/
Copy pathFirebaseAuthTest.java
More file actions
365 lines (323 loc) · 14.4 KB
/
Copy pathFirebaseAuthTest.java
File metadata and controls
365 lines (323 loc) · 14.4 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.firebase.auth;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import com.google.api.client.googleapis.testing.auth.oauth2.MockTokenServerTransport;
import com.google.api.client.googleapis.util.Utils;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.core.ApiFuture;
import com.google.auth.http.HttpTransportFactory;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.auth.oauth2.UserCredentials;
import com.google.common.base.Defaults;
import com.google.common.base.Strings;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.ImplFirebaseTrampolines;
import com.google.firebase.TestOnlyImplFirebaseTrampolines;
import com.google.firebase.auth.internal.FirebaseCustomAuthToken;
import com.google.firebase.database.MapBuilder;
import com.google.firebase.testing.ServiceAccount;
import com.google.firebase.testing.TestUtils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.junit.After;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class FirebaseAuthTest {
private static final String ACCESS_TOKEN = "mockaccesstoken";
private static final String CLIENT_SECRET = "mockclientsecret";
private static final String CLIENT_ID = "mockclientid";
private static final String REFRESH_TOKEN = "mockrefreshtoken";
private static final JsonFactory JSON_FACTORY = Utils.getDefaultJsonFactory();
private final FirebaseOptions firebaseOptions;
private final boolean isCertCredential;
public FirebaseAuthTest(FirebaseOptions baseOptions, boolean isCertCredential) {
this.firebaseOptions = baseOptions;
this.isCertCredential = isCertCredential;
}
@Parameters
public static Collection<Object[]> data() throws Exception {
// Initialize this test suite with all available credential implementations.
return Arrays.asList(
new Object[][] {
{
new FirebaseOptions.Builder().setCredentials(createCertificateCredential()).build(),
/* isCertCredential */ true
},
{
new FirebaseOptions.Builder().setCredentials(createRefreshTokenCredential()).build(),
/* isCertCredential */ false
},
{
new FirebaseOptions.Builder()
.setCredentials(TestUtils.getApplicationDefaultCredentials())
.build(),
/* isCertCredential */ false
},
});
}
private static GoogleCredentials createRefreshTokenCredential() throws IOException {
final MockTokenServerTransport transport = new MockTokenServerTransport();
transport.addClient(CLIENT_ID, CLIENT_SECRET);
transport.addRefreshToken(REFRESH_TOKEN, ACCESS_TOKEN);
Map<String, Object> secretJson = new HashMap<>();
secretJson.put("client_id", CLIENT_ID);
secretJson.put("client_secret", CLIENT_SECRET);
secretJson.put("refresh_token", REFRESH_TOKEN);
secretJson.put("type", "authorized_user");
InputStream refreshTokenStream =
new ByteArrayInputStream(JSON_FACTORY.toByteArray(secretJson));
return UserCredentials.fromStream(refreshTokenStream, new HttpTransportFactory() {
@Override
public HttpTransport create() {
return transport;
}
});
}
private static GoogleCredentials createCertificateCredential() throws IOException {
final MockTokenServerTransport transport = new MockTokenServerTransport();
transport.addServiceAccount(ServiceAccount.EDITOR.getEmail(), ACCESS_TOKEN);
return ServiceAccountCredentials.fromStream(ServiceAccount.EDITOR.asStream(),
new HttpTransportFactory() {
@Override
public HttpTransport create() {
return transport;
}
});
}
@Before
public void setup() {
TestOnlyImplFirebaseTrampolines.clearInstancesForTest();
FirebaseApp.initializeApp(firebaseOptions);
}
@After
public void cleanup() {
TestOnlyImplFirebaseTrampolines.clearInstancesForTest();
}
@Test
public void testGetInstance() {
FirebaseAuth defaultAuth = FirebaseAuth.getInstance();
assertNotNull(defaultAuth);
assertSame(defaultAuth, FirebaseAuth.getInstance());
String token = TestOnlyImplFirebaseTrampolines.getToken(FirebaseApp.getInstance(), false);
Assert.assertTrue(!token.isEmpty());
}
@Test
public void testGetInstanceForApp() {
FirebaseApp app = FirebaseApp.initializeApp(firebaseOptions, "testGetInstanceForApp");
FirebaseAuth auth = FirebaseAuth.getInstance(app);
assertNotNull(auth);
assertSame(auth, FirebaseAuth.getInstance(app));
String token = TestOnlyImplFirebaseTrampolines.getToken(app, false);
Assert.assertTrue(!token.isEmpty());
}
@Test
public void testAppDelete() {
FirebaseApp app = FirebaseApp.initializeApp(firebaseOptions, "testAppDelete");
FirebaseAuth auth = FirebaseAuth.getInstance(app);
assertNotNull(auth);
app.delete();
try {
FirebaseAuth.getInstance(app);
fail("No error thrown when getting auth instance after deleting app");
} catch (IllegalStateException expected) {
// ignore
}
}
@Test
public void testInvokeAfterAppDelete() throws Exception {
FirebaseApp app = FirebaseApp.initializeApp(firebaseOptions, "testInvokeAfterAppDelete");
FirebaseAuth auth = FirebaseAuth.getInstance(app);
assertNotNull(auth);
app.delete();
for (Method method : auth.getClass().getDeclaredMethods()) {
int modifiers = method.getModifiers();
if (!Modifier.isPublic(modifiers) || Modifier.isStatic(modifiers)) {
continue;
}
List<Object> parameters = new ArrayList<>(method.getParameterTypes().length);
for (Class<?> parameterType : method.getParameterTypes()) {
parameters.add(Defaults.defaultValue(parameterType));
}
try {
method.invoke(auth, parameters.toArray());
fail("No error thrown when invoking auth after deleting app; method: " + method.getName());
} catch (InvocationTargetException expected) {
String message = "FirebaseAuth instance is no longer alive. This happens when "
+ "the parent FirebaseApp instance has been deleted.";
Throwable cause = expected.getCause();
assertTrue(cause instanceof IllegalStateException);
assertEquals(message, cause.getMessage());
}
}
}
@Test
public void testInitAfterAppDelete() throws ExecutionException, InterruptedException,
TimeoutException {
FirebaseApp app = FirebaseApp.initializeApp(firebaseOptions, "testInitAfterAppDelete");
FirebaseAuth auth1 = FirebaseAuth.getInstance(app);
assertNotNull(auth1);
app.delete();
app = FirebaseApp.initializeApp(firebaseOptions, "testInitAfterAppDelete");
FirebaseAuth auth2 = FirebaseAuth.getInstance(app);
assertNotNull(auth2);
assertNotSame(auth1, auth2);
if (isCertCredential) {
ApiFuture<String> future = auth2.createCustomTokenAsync("foo");
assertNotNull(future);
assertNotNull(future.get(TestUtils.TEST_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS));
}
}
@Test
public void testAppWithAuthVariableOverrides() {
Map<String, Object> authVariableOverrides = Collections.singletonMap("uid", (Object) "uid1");
FirebaseOptions options =
new FirebaseOptions.Builder(firebaseOptions)
.setDatabaseAuthVariableOverride(authVariableOverrides)
.build();
FirebaseApp app = FirebaseApp.initializeApp(options, "testGetAppWithUid");
assertEquals("uid1", app.getOptions().getDatabaseAuthVariableOverride().get("uid"));
String token = TestOnlyImplFirebaseTrampolines.getToken(app, false);
Assert.assertTrue(!token.isEmpty());
}
@Test
public void testCreateCustomToken() throws Exception {
GoogleCredentials credentials = TestOnlyImplFirebaseTrampolines.getCredentials(firebaseOptions);
Assume.assumeTrue("Skipping testCredentialCertificateRequired for cert credential",
credentials instanceof ServiceAccountCredentials);
FirebaseApp app = FirebaseApp.initializeApp(firebaseOptions, "testCreateCustomToken");
FirebaseAuth auth = FirebaseAuth.getInstance(app);
String token = auth.createCustomTokenAsync("user1").get();
FirebaseCustomAuthToken parsedToken = FirebaseCustomAuthToken.parse(new GsonFactory(), token);
assertEquals(parsedToken.getPayload().getUid(), "user1");
assertEquals(parsedToken.getPayload().getSubject(), ServiceAccount.EDITOR.getEmail());
assertEquals(parsedToken.getPayload().getIssuer(), ServiceAccount.EDITOR.getEmail());
assertNull(parsedToken.getPayload().getDeveloperClaims());
assertTrue(ServiceAccount.EDITOR.verifySignature(parsedToken));
}
@Test
public void testCreateCustomTokenWithDeveloperClaims() throws Exception {
GoogleCredentials credentials = TestOnlyImplFirebaseTrampolines.getCredentials(firebaseOptions);
Assume.assumeTrue("Skipping testCredentialCertificateRequired for cert credential",
credentials instanceof ServiceAccountCredentials);
FirebaseApp app =
FirebaseApp.initializeApp(firebaseOptions, "testCreateCustomTokenWithDeveloperClaims");
FirebaseAuth auth = FirebaseAuth.getInstance(app);
String token =
auth.createCustomTokenAsync("user1", MapBuilder.of("claim", "value")).get();
FirebaseCustomAuthToken parsedToken = FirebaseCustomAuthToken.parse(new GsonFactory(), token);
assertEquals(parsedToken.getPayload().getUid(), "user1");
assertEquals(parsedToken.getPayload().getSubject(), ServiceAccount.EDITOR.getEmail());
assertEquals(parsedToken.getPayload().getIssuer(), ServiceAccount.EDITOR.getEmail());
assertEquals(parsedToken.getPayload().getDeveloperClaims().keySet().size(), 1);
assertEquals(parsedToken.getPayload().getDeveloperClaims().get("claim"), "value");
assertTrue(ServiceAccount.EDITOR.verifySignature(parsedToken));
}
@Test
public void testServiceAccountRequired() throws Exception {
GoogleCredentials credentials = TestOnlyImplFirebaseTrampolines.getCredentials(firebaseOptions);
Assume.assumeFalse("Skipping testServiceAccountRequired for service account credentials",
credentials instanceof ServiceAccountCredentials);
FirebaseApp app = FirebaseApp.initializeApp(firebaseOptions, "testServiceAccountRequired");
try {
FirebaseAuth.getInstance(app).createCustomTokenAsync("foo").get();
fail("Expected exception.");
} catch (IllegalStateException expected) {
Assert.assertEquals(
"Failed to initialize FirebaseTokenFactory. Make sure to initialize the SDK with "
+ "service account credentials or specify a service account ID with "
+ "iam.serviceAccounts.signBlob permission. Please refer to "
+ "https://firebase.google.com/docs/auth/admin/create-custom-tokens for more details "
+ "on creating custom tokens.",
expected.getMessage());
}
}
@Test
public void testProjectIdRequired() throws Exception {
FirebaseApp app = FirebaseApp.initializeApp(firebaseOptions, "testProjectIdRequired");
String projectId = ImplFirebaseTrampolines.getProjectId(app);
Assume.assumeTrue("Skipping testProjectIdRequired for settings with project ID",
Strings.isNullOrEmpty(projectId));
try {
FirebaseAuth.getInstance(app).verifyIdTokenAsync("foo").get();
fail("Expected exception.");
} catch (IllegalArgumentException expected) {
Assert.assertEquals(
"Must initialize FirebaseApp with a project ID to call verifyIdToken()",
expected.getMessage());
}
}
@Test
public void testVerifyIdTokenWithExplicitProjectId() throws Exception {
GoogleCredentials credentials = TestOnlyImplFirebaseTrampolines.getCredentials(firebaseOptions);
Assume.assumeFalse(
"Skipping testVerifyIdTokenWithExplicitProjectId for service account credentials",
credentials instanceof ServiceAccountCredentials);
FirebaseOptions options =
new FirebaseOptions.Builder(firebaseOptions)
.setProjectId("mock-project-id")
.build();
FirebaseApp app = FirebaseApp.initializeApp(options, "testVerifyIdTokenWithExplicitProjectId");
try {
FirebaseAuth.getInstance(app).verifyIdTokenAsync("foo").get();
fail("Expected exception.");
} catch (ExecutionException expected) {
Assert.assertNotEquals(
"com.google.firebase.FirebaseException: Must initialize FirebaseApp with a project ID "
+ "to call verifyIdToken()",
expected.getMessage());
assertTrue(expected.getCause() instanceof IllegalArgumentException);
}
}
@Test(expected = IllegalArgumentException.class)
public void testAuthExceptionNullErrorCode() {
new FirebaseAuthException(null, "test");
}
@Test(expected = IllegalArgumentException.class)
public void testAuthExceptionEmptyErrorCode() {
new FirebaseAuthException("", "test");
}
}