Skip to content

Commit 43fd115

Browse files
Add JVM OAuth credential authentication
1 parent 9f3a1b8 commit 43fd115

15 files changed

Lines changed: 891 additions & 90 deletions

‎build.gradle.kts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ dependencies {
138138
testImplementation(libs.kotlinx.coroutines.play.services)
139139
testImplementation(libs.kotlinx.coroutines.swing)
140140
testImplementation(libs.kotlinx.coroutines.test)
141+
testImplementation(libs.okhttp.mockwebserver)
141142
// firebase aars
142143
aar(platform(libs.google.firebase.bom))
143144
aar(libs.google.firebase.firestore)

‎gradle/libs.versions.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-t
3333
kotlinx-serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinx-serialization" }
3434
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" }
3535
okhttp = { module = "com.squareup.okhttp3:okhttp", version = "3.12.13" }
36+
okhttp-mockwebserver = { module = "com.squareup.okhttp3:mockwebserver", version = "3.12.13" }
3637
robolectric-android-all = { module = "org.robolectric:android-all", version = "14-robolectric-10818077" }
3738
xerial-sqlite-jdbc = { module = "org.xerial:sqlite-jdbc", version = "3.46.1.0" }
3839

‎src/main/java/android/os/AsyncTask.kt‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package android.os
22

3-
import kotlinx.coroutines.Dispatchers
3+
import com.google.firebase.FirebasePlatform
44
import kotlinx.coroutines.GlobalScope
55
import kotlinx.coroutines.launch
66
import kotlinx.coroutines.withContext
@@ -19,9 +19,9 @@ abstract class AsyncTask {
1919

2020
fun execute(vararg params: Any): AsyncTask {
2121
GlobalScope.launch {
22-
withContext(Dispatchers.Main) { onPreExecute() }
22+
withContext(FirebasePlatform.firebasePlatform.mainDispatcher) { onPreExecute() }
2323
val result = doInBackground(*params)
24-
withContext(Dispatchers.Main) { onPostExecute(result) }
24+
withContext(FirebasePlatform.firebasePlatform.mainDispatcher) { onPostExecute(result) }
2525
}
2626
return this
2727
}

‎src/main/java/android/os/Handler.kt‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package android.os
22

3-
import kotlinx.coroutines.Dispatchers
4-
import kotlinx.coroutines.GlobalScope
3+
import com.google.firebase.FirebasePlatform
54
import kotlinx.coroutines.delay
65
import kotlinx.coroutines.launch
76

@@ -10,14 +9,14 @@ open class Handler(looper: Looper?, callback: Handler.Callback?) {
109
constructor(looper: Looper) : this(looper, null)
1110

1211
fun post(runnable: Runnable): Boolean {
13-
GlobalScope.launch(Dispatchers.Main) {
12+
FirebasePlatform.firebasePlatform.mainScope.launch {
1413
runnable.run()
1514
}
1615
return true
1716
}
1817

1918
fun postDelayed(runnable: Runnable, time: Long): Boolean {
20-
GlobalScope.launch(Dispatchers.Main) {
19+
FirebasePlatform.firebasePlatform.mainScope.launch {
2120
delay(time)
2221
runnable.run()
2322
}

‎src/main/java/com/google/firebase/FirebasePlatform.kt‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
package com.google.firebase
22

3+
import kotlinx.coroutines.CoroutineDispatcher
4+
import kotlinx.coroutines.CoroutineScope
5+
import kotlinx.coroutines.Dispatchers
6+
import kotlinx.coroutines.SupervisorJob
37
import java.io.File
48

59
abstract class FirebasePlatform {
610

11+
open val mainDispatcher: CoroutineDispatcher
12+
get() = Dispatchers.Default
13+
14+
internal val mainScope: CoroutineScope by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
15+
CoroutineScope(SupervisorJob() + mainDispatcher)
16+
}
17+
718
companion object {
819

920
internal lateinit var firebasePlatform: FirebasePlatform
Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,75 @@
11
package com.google.firebase.auth;
22

3-
import kotlin.NotImplementedError;
3+
import java.net.URLEncoder;
4+
import java.nio.charset.StandardCharsets;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public abstract class AuthCredential {
9+
private final String provider;
10+
private final String idToken;
11+
private final String accessToken;
12+
private final String rawNonce;
13+
14+
protected AuthCredential(String provider) {
15+
this(provider, null, null, null);
16+
}
17+
18+
protected AuthCredential(String provider, String idToken, String accessToken, String rawNonce) {
19+
this.provider = requireValue(provider, "provider");
20+
this.idToken = optionalValue(idToken, "idToken");
21+
this.accessToken = optionalValue(accessToken, "accessToken");
22+
this.rawNonce = optionalValue(rawNonce, "rawNonce");
23+
24+
if (this.rawNonce != null && this.idToken == null) {
25+
throw new IllegalArgumentException("An ID token is required when a raw nonce is supplied.");
26+
}
27+
}
428

5-
public class AuthCredential {
629
public String getProvider() {
7-
throw new NotImplementedError();
30+
return provider;
31+
}
32+
33+
public String getSignInMethod() {
34+
return provider;
35+
}
36+
37+
String toIdpPostBody() {
38+
List<String> values = new ArrayList<>();
39+
values.add(formValue("providerId", provider));
40+
if (idToken != null) {
41+
values.add(formValue("id_token", idToken));
42+
}
43+
if (accessToken != null) {
44+
values.add(formValue("access_token", accessToken));
45+
}
46+
if (rawNonce != null) {
47+
values.add(formValue("nonce", rawNonce));
48+
}
49+
return String.join("&", values);
50+
}
51+
52+
@Override
53+
public String toString() {
54+
return getClass().getSimpleName() + "{provider='" + provider + "'}";
55+
}
56+
57+
static String requireValue(String value, String name) {
58+
if (value == null || value.trim().isEmpty()) {
59+
throw new IllegalArgumentException(name + " must not be null or blank.");
60+
}
61+
return value;
62+
}
63+
64+
static String optionalValue(String value, String name) {
65+
if (value != null && value.trim().isEmpty()) {
66+
throw new IllegalArgumentException(name + " must not be blank.");
67+
}
68+
return value;
69+
}
70+
71+
private static String formValue(String name, String value) {
72+
return URLEncoder.encode(name, StandardCharsets.UTF_8) + "=" +
73+
URLEncoder.encode(value, StandardCharsets.UTF_8);
874
}
975
}

0 commit comments

Comments
 (0)