android / identity-samples

Multiple samples showing the best practices in identity on Android.
Apache License 2.0
322 stars 198 forks source link

FIDO2 NetworkOnMainThreadException #79

Open ckgal opened 2 months ago

ckgal commented 2 months ago

I have downloaded the source code and tried to configure the fido2 to work with a fido2 server. Managed to send registerRequest and got the response. While finish handling the response, the app crash with the below error. I would like to check if the code works and also do i have to modify to use async instead? I need to connect to physical key and hence yet to switch to Credential Manager.

2024-05-02 16:15:50.556 25176-25176 System.out com...oid.gms.identity.sample.fido2 I DEBUGGING: Skipped: extensions 2024-05-02 16:15:50.562 25176-25176 AuthRepository com...oid.gms.identity.sample.fido2 E Cannot call registerRequest android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1667) at com.android.org.conscrypt.Platform.blockGuardOnNetwork(Platform.java:436) at com.android.org.conscrypt.ConscryptEngineSocket$SSLInputStream.processDataFromSocket(ConscryptEngineSocket.java:874) at com.android.org.conscrypt.ConscryptEngineSocket$SSLInputStream.readUntilDataAvailable(ConscryptEngineSocket.java:862) at com.android.org.conscrypt.ConscryptEngineSocket$SSLInputStream.read(ConscryptEngineSocket.java:835) at okio.InputStreamSource.read(JvmOkio.kt:90) at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:129) at okio.RealBufferedSource.request(RealBufferedSource.kt:206) at okio.RealBufferedSource.require(RealBufferedSource.kt:199) at okio.RealBufferedSource.readHexadecimalUnsignedLong(RealBufferedSource.kt:381) at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.readChunkSize(Http1ExchangeCodec.kt:429) at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.read(Http1ExchangeCodec.kt:408) at okhttp3.internal.Util.skipAll(Util.kt:337) at okhttp3.internal.Util.discard(Util.kt:358) at okhttp3.internal.http1.Http1ExchangeCodec$ChunkedSource.close(Http1ExchangeCodec.kt:450) at okio.ForwardingSource.close(ForwardingSource.kt:34) at okhttp3.internal.connection.Exchange$ResponseBodySource.close(Exchange.kt:309) at okio.RealBufferedSource.close(RealBufferedSource.kt:477) at okio.RealBufferedSource$inputStream$1.close(RealBufferedSource.kt:170) at sun.nio.cs.StreamDecoder.implClose(StreamDecoder.java:423) at sun.nio.cs.StreamDecoder.close(StreamDecoder.java:196) at java.io.InputStreamReader.close(InputStreamReader.java:195) at java.io.BufferedReader.close(BufferedReader.java:539) i at android.util.JsonReader.close(JsonReader.java:540) at kotlin.io.CloseableKt.closeFinally(Closeable.kt:57) at com.google.android.gms.identity.sample.fido2.api.AuthApi.parsePublicKeyCredentialCreationOptions(AuthApi.kt:332) at com.google.android.gms.identity.sample.fido2.api.AuthApi.access$parsePublicKeyCredentialCreationOptions(AuthApi.kt:55) at com.google.android.gms.identity.sample.fido2.api.AuthApi$registerRequest$2.invoke(AuthApi.kt:165) at com.google.android.gms.identity.sample.fido2.api.AuthApi$registerRequest$2.invoke(AuthApi.kt:163) at com.google.android.gms.identity.sample.fido2.api.AuthApi.result(AuthApi.kt:620) at com.google.android.gms.identity.sample.fido2.api.AuthApi.registerRequest(AuthApi.kt:163) at com.google.android.gms.identity.sample.fido2.api.AuthApi$registerRequest$1.invokeSuspend(Unknown Source:15) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) at android.os.Handler.handleCallback(Handler.java:942) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:226) at android.os.Looper.loop(Looper.java:313) at android.app.ActivityThread.main(ActivityThread.java:8762) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:604) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)

ckgal commented 1 month ago

For demo purpose, managed to make it work by adding this line in MainActivity.kt // Disable StrictMode's network policy StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder().permitNetwork().build())