@@ -99,9 +99,10 @@ class AuthViewModel(app: Application) : AndroidViewModel(app) {
9999 )
100100 .setScopes(AuthConfig .scopes)
101101 .build()
102+ val intent = authService.getAuthorizationRequestIntent(request)
102103 pendingAuthState = AuthState (config)
103- authService.getAuthorizationRequestIntent(request)
104- } catch (e: Throwable ) {
104+ intent
105+ } catch (e: Exception ) {
105106 _error .value = e.localizedMessage ? : " Authorization failed"
106107 null
107108 }
@@ -115,6 +116,7 @@ class AuthViewModel(app: Application) : AndroidViewModel(app) {
115116 val ex = AuthorizationException .fromIntent(intent)
116117 val pending = pendingAuthState
117118 if (response == null || pending == null ) {
119+ pendingAuthState = null
118120 _error .value = ex?.localizedMessage ? : " Authorization failed"
119121 return
120122 }
@@ -125,7 +127,7 @@ class AuthViewModel(app: Application) : AndroidViewModel(app) {
125127 pending.update(tokenResponse, null )
126128 _state .value = pending
127129 scheduleExpiryTimer()
128- } catch (e: Throwable ) {
130+ } catch (e: Exception ) {
129131 _error .value = e.localizedMessage ? : " Token exchange failed"
130132 } finally {
131133 pendingAuthState = null
@@ -137,7 +139,7 @@ class AuthViewModel(app: Application) : AndroidViewModel(app) {
137139 // @description Revoke the access token at the IdP and clear local auth state
138140 suspend fun signOut () {
139141 _state .value?.lastTokenResponse?.accessToken?.let { token ->
140- try { revokeToken(token) } catch (_: Throwable ) { /* best-effort */ }
142+ try { revokeToken(token) } catch (_: Exception ) { /* best-effort */ }
141143 }
142144 _state .value = null
143145 _expired .value = false
@@ -195,13 +197,16 @@ class AuthViewModel(app: Application) : AndroidViewModel(app) {
195197 val conn = (URL (revokeUrl).openConnection() as HttpURLConnection ).apply {
196198 requestMethod = " POST"
197199 doOutput = true
200+ connectTimeout = 5_000
201+ readTimeout = 5_000
198202 setRequestProperty(" Content-Type" , " application/x-www-form-urlencoded" )
199203 }
200204 try {
201205 val body = " token=${URLEncoder .encode(token, " UTF-8" )} " +
202206 " &client_id=${URLEncoder .encode(AuthConfig .clientId, " UTF-8" )} "
203207 conn.outputStream.use { it.write(body.toByteArray(Charsets .UTF_8 )) }
204- conn.inputStream.use { it.readBytes() }
208+ val stream = if (conn.responseCode in 200 .. 299 ) conn.inputStream else conn.errorStream
209+ stream?.use { it.readBytes() }
205210 } finally {
206211 conn.disconnect()
207212 }
0 commit comments