Skip to content

Commit 6a73131

Browse files
committed
upgrade to okhttp 3
1 parent cce0c8c commit 6a73131

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

src/main/java/com/google/firebase/auth/FirebaseAuth.kt

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import com.google.firebase.FirebasePlatform
1010
import com.google.firebase.auth.internal.InternalAuthProvider
1111
import com.google.firebase.internal.InternalTokenResult
1212
import com.google.firebase.internal.api.FirebaseNoSignedInUserException
13-
import com.squareup.okhttp.*
1413
import kotlinx.coroutines.Dispatchers
1514
import kotlinx.coroutines.GlobalScope
1615
import kotlinx.coroutines.launch
1716
import kotlinx.serialization.Serializable
1817
import kotlinx.serialization.Transient
1918
import kotlinx.serialization.json.*
19+
import okhttp3.*
2020
import java.io.IOException
2121
import java.util.*
2222
import java.util.concurrent.CopyOnWriteArrayList
@@ -71,12 +71,12 @@ class FirebaseUserImpl private constructor(
7171
.build()
7272
FirebaseAuth.getInstance(app).client.newCall(request).enqueue(object : Callback {
7373

74-
override fun onFailure(request: Request, e: IOException) {
74+
override fun onFailure(call: Call, e: IOException) {
7575
source.setException(FirebaseException(e.toString(), e))
7676
}
7777

7878
@Throws(IOException::class)
79-
override fun onResponse(response: Response) {
79+
override fun onResponse(call: Call, response: Response) {
8080
if (!response.isSuccessful) {
8181
FirebaseAuth.getInstance(app).signOut()
8282
source.setException(FirebaseAuthInvalidUserException(
@@ -104,7 +104,11 @@ class FirebaseUserImpl private constructor(
104104
class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
105105

106106
val json = MediaType.parse("application/json; charset=utf-8")
107-
val client = OkHttpClient()
107+
val client: OkHttpClient = OkHttpClient.Builder()
108+
.connectTimeout(60, TimeUnit.SECONDS)
109+
.readTimeout(60, TimeUnit.SECONDS)
110+
.writeTimeout(60, TimeUnit.SECONDS)
111+
.build()
108112

109113
companion object {
110114

@@ -159,12 +163,6 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
159163
}
160164
}
161165

162-
init {
163-
client.setConnectTimeout(60, TimeUnit.SECONDS)
164-
client.setReadTimeout(60, TimeUnit.SECONDS)
165-
client.setWriteTimeout(60, TimeUnit.SECONDS)
166-
}
167-
168166
fun signInAnonymously(): Task<AuthResult> {
169167
val source = TaskCompletionSource<AuthResult>()
170168
val body = RequestBody.create(json, JsonObject(mapOf("returnSecureToken" to JsonPrimitive(true))).toString())
@@ -174,19 +172,19 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
174172
.build()
175173
client.newCall(request).enqueue(object : Callback {
176174

177-
override fun onFailure(request: Request, e: IOException) {
175+
override fun onFailure(call: Call, e: IOException) {
178176
source.setException(FirebaseException(e.toString(), e))
179177
}
180178

181179
@Throws(IOException::class)
182-
override fun onResponse(response: Response) {
180+
override fun onResponse(call: Call, response: Response) {
183181
if (!response.isSuccessful) {
184182
source.setException(FirebaseAuthInvalidUserException(
185183
response.message(),
186184
formatErrorMessage("accounts:signUp", request, response)
187185
))
188186
} else {
189-
val body = response.body().use { it.string() }
187+
val body = response.body()!!.use { it.string() }
190188
user = FirebaseUserImpl(app, jsonParser.parseToJsonElement(body).jsonObject, true)
191189
source.setResult(AuthResult { user })
192190
}
@@ -207,19 +205,19 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
207205
.build()
208206
client.newCall(request).enqueue(object : Callback {
209207

210-
override fun onFailure(request: Request, e: IOException) {
208+
override fun onFailure(call: Call, e: IOException) {
211209
source.setException(FirebaseException(e.toString(), e))
212210
}
213211

214212
@Throws(IOException::class)
215-
override fun onResponse(response: Response) {
213+
override fun onResponse(call: Call, response: Response) {
216214
if (!response.isSuccessful) {
217215
source.setException(FirebaseAuthInvalidUserException(
218216
response.message(),
219217
formatErrorMessage("verifyCustomToken", request, response)
220218
))
221219
} else {
222-
val body = response.body().use { it.string() }
220+
val body = response.body()!!.use { it.string() }
223221
val user = FirebaseUserImpl(app, jsonParser.parseToJsonElement(body).jsonObject)
224222
refreshToken(user, source) { AuthResult { it } }
225223
}
@@ -240,19 +238,19 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
240238
.build()
241239
client.newCall(request).enqueue(object : Callback {
242240

243-
override fun onFailure(request: Request, e: IOException) {
241+
override fun onFailure(call: Call, e: IOException) {
244242
source.setException(FirebaseException(e.toString(), e))
245243
}
246244

247245
@Throws(IOException::class)
248-
override fun onResponse(response: Response) {
246+
override fun onResponse(call: Call, response: Response) {
249247
if (!response.isSuccessful) {
250248
source.setException(FirebaseAuthInvalidUserException(
251249
response.message(),
252250
formatErrorMessage("verifyPassword", request, response)
253251
))
254252
} else {
255-
val body = response.body().use { it.string() }
253+
val body = response.body()!!.use { it.string() }
256254
val user = FirebaseUserImpl(app, jsonParser.parseToJsonElement(body).jsonObject)
257255
refreshToken(user, source) { AuthResult { it } }
258256
}
@@ -263,8 +261,8 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
263261

264262
internal fun formatErrorMessage(title: String, request: Request, response: Response): String {
265263
return "$title API returned an error, " +
266-
"with url [${request.method()}] ${request.urlString()} ${request.body()} -- " +
267-
"response [${response.code()}] ${response.message()} ${response.body().use { it.string() }}"
264+
"with url [${request.method()}] ${request.url()} ${request.body()} -- " +
265+
"response [${response.code()}] ${response.message()} ${response.body().use { it?.string() }}"
268266
}
269267

270268
fun signOut() {
@@ -313,17 +311,17 @@ class FirebaseAuth constructor(val app: FirebaseApp) : InternalAuthProvider {
313311

314312
client.newCall(request).enqueue(object : Callback {
315313

316-
override fun onFailure(request: Request, e: IOException) {
314+
override fun onFailure(call: Call, e: IOException) {
317315
source.setException(FirebaseException(e.toString(), e))
318316
}
319317

320318
@Throws(IOException::class)
321-
override fun onResponse(response: Response) {
319+
override fun onResponse(call: Call, response: Response) {
322320
response.body().use { body ->
323321
if (!response.isSuccessful) {
324-
body.string().let { signOutAndThrowInvalidUserException(it, "token API returned an error: $it") }
322+
signOutAndThrowInvalidUserException(body?.string().orEmpty(), "token API returned an error: ${body?.string()}")
325323
} else {
326-
jsonParser.parseToJsonElement(body.string()).jsonObject.apply {
324+
jsonParser.parseToJsonElement(body!!.string()).jsonObject.apply {
327325
val user = FirebaseUserImpl(app, this, user.isAnonymous)
328326
if (user.claims["aud"] != app.options.projectId) {
329327
signOutAndThrowInvalidUserException(

0 commit comments

Comments
 (0)