Skip to content

Commit

Permalink
Auth: Add support for emulator
Browse files Browse the repository at this point in the history
  • Loading branch information
fmasa committed Oct 7, 2024
1 parent 94f0d1d commit 265c924
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/main/java/com/google/firebase/auth/FirebaseAuth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ import java.util.concurrent.TimeUnit

val jsonParser = Json { ignoreUnknownKeys = true }

class UrlFactory(
private val app: FirebaseApp,
private val emulatorUrl: String? = null,
) {
fun buildUrl(uri: String): String {
val baseUrl = "${emulatorUrl ?: "https://"}www.googleapis.com/identitytoolkit"

return "$baseUrl/$uri?key=${app.options.apiKey}"
}
}

@Serializable
class FirebaseUserImpl private constructor(
@Transient
Expand All @@ -52,17 +63,20 @@ class FirebaseUserImpl private constructor(
val idToken: String,
val refreshToken: String,
val expiresIn: Int,
val createdAt: Long
val createdAt: Long,
@Transient
private val urlFactory: UrlFactory = UrlFactory(app),
) : FirebaseUser() {

constructor(app: FirebaseApp, data: JsonObject, isAnonymous: Boolean = data["isAnonymous"]?.jsonPrimitive?.booleanOrNull ?: false) : this(
constructor(app: FirebaseApp, data: JsonObject, isAnonymous: Boolean = data["isAnonymous"]?.jsonPrimitive?.booleanOrNull ?: false, urlFactory: UrlFactory = UrlFactory(app)) : this(
app,
isAnonymous,
data["uid"]?.jsonPrimitive?.contentOrNull ?: data["user_id"]?.jsonPrimitive?.contentOrNull ?: data["localId"]?.jsonPrimitive?.contentOrNull ?: "",
data["idToken"]?.jsonPrimitive?.contentOrNull ?: data.getValue("id_token").jsonPrimitive.content,
data["refreshToken"]?.jsonPrimitive?.contentOrNull ?: data.getValue("refresh_token").jsonPrimitive.content,
data["expiresIn"]?.jsonPrimitive?.intOrNull ?: data.getValue("expires_in").jsonPrimitive.int,
data["createdAt"]?.jsonPrimitive?.longOrNull ?: System.currentTimeMillis()
data["createdAt"]?.jsonPrimitive?.longOrNull ?: System.currentTimeMillis(),
urlFactory
)

val claims: Map<String, Any?> by lazy {
Expand All @@ -85,7 +99,7 @@ class FirebaseUserImpl private constructor(
val source = TaskCompletionSource<Void>()
val body = RequestBody.create(FirebaseAuth.getInstance(app).json, JsonObject(mapOf("idToken" to JsonPrimitive(idToken))).toString())
val request = Request.Builder()
.url("https://www.googleapis.com/identitytoolkit/v3/relyingparty/deleteAccount?key=" + app.options.apiKey)
.url(urlFactory.buildUrl("v3/relyingparty/deleteAccount"))
.post(body)
.build()
FirebaseAuth.getInstance(app).client.newCall(request).enqueue(object : Callback {
Expand Down Expand Up @@ -184,11 +198,13 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
}
}

private var urlFactory = UrlFactory(app)

fun signInAnonymously(): Task<AuthResult> {
val source = TaskCompletionSource<AuthResult>()
val body = RequestBody.create(json, JsonObject(mapOf("returnSecureToken" to JsonPrimitive(true))).toString())
val request = Request.Builder()
.url("https://identitytoolkit.googleapis.com/v1/accounts:signUp?key=" + app.options.apiKey)
.url(urlFactory.buildUrl("v1/accounts:signUp"))
.post(body)
.build()
client.newCall(request).enqueue(object : Callback {
Expand Down Expand Up @@ -220,7 +236,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
JsonObject(mapOf("token" to JsonPrimitive(customToken), "returnSecureToken" to JsonPrimitive(true))).toString()
)
val request = Request.Builder()
.url("https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken?key=" + app.options.apiKey)
.url(urlFactory.buildUrl("v3/relyingparty/verifyCustomToken"))
.post(body)
.build()
client.newCall(request).enqueue(object : Callback {
Expand Down Expand Up @@ -252,7 +268,7 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
JsonObject(mapOf("email" to JsonPrimitive(email), "password" to JsonPrimitive(password), "returnSecureToken" to JsonPrimitive(true))).toString()
)
val request = Request.Builder()
.url("https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=" + app.options.apiKey)
.url("v3/relyingparty/verifyPassword?key=" + app.options.apiKey)
.post(body)
.build()
client.newCall(request).enqueue(object : Callback {
Expand Down Expand Up @@ -439,5 +455,8 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
fun signInWithEmailLink(email: String, link: String): Task<AuthResult> = TODO()

fun setLanguageCode(value: String): Nothing = TODO()
fun useEmulator(host: String, port: Int): Unit = TODO()

fun useEmulator(host: String, port: Int) {
urlFactory = UrlFactory(app, "http://$host:$port/")
}
}

0 comments on commit 265c924

Please sign in to comment.