Skip to content

Commit e8a6f09

Browse files
committed
added safety checks around the scenario when intent bundle are null for authentication activity
1 parent d8ac49e commit e8a6f09

File tree

3 files changed

+54
-20
lines changed

3 files changed

+54
-20
lines changed

auth0/src/main/java/com/auth0/android/Auth0.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public open class Auth0 private constructor(
7272
* @return Url to call to perform the web flow of OAuth
7373
*/
7474
public open val authorizeUrl: String
75-
get() = domainUrl!!.newBuilder()
75+
get() = domainUrl.newBuilder()
7676
.addEncodedPathSegment("authorize")
7777
.build()
7878
.toString()
@@ -83,7 +83,7 @@ public open class Auth0 private constructor(
8383
* @return Url to call to perform the web logout
8484
*/
8585
public open val logoutUrl: String
86-
get() = domainUrl!!.newBuilder()
86+
get() = domainUrl.newBuilder()
8787
.addEncodedPathSegment("v2")
8888
.addEncodedPathSegment("logout")
8989
.build()
@@ -134,7 +134,7 @@ public open class Auth0 private constructor(
134134
): Auth0 {
135135
val domainUrl = ensureValidUrl(domain)
136136
requireNotNull(domainUrl) { String.format("Invalid domain url: '%s'", domain) }
137-
if (instance == null || instance!!.clientId != clientId || instance!!.domainUrl.host != domainUrl.host || instance!!.configurationDomain != configurationDomain) {
137+
if (instance == null || instance?.clientId != clientId || instance?.domainUrl?.host != domainUrl.host || instance?.configurationDomain != configurationDomain) {
138138
instance = Auth0(clientId, domainUrl, configurationDomain)
139139
}
140140
return instance!!

auth0/src/main/java/com/auth0/android/authentication/AuthenticationException.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ public class AuthenticationException : Auth0Exception {
2626

2727
public constructor(message: String, cause: Exception? = null) : super(message, cause)
2828

29-
public constructor(code: String, description: String, cause: Exception) : this(DEFAULT_MESSAGE, cause) {
29+
public constructor(code: String, description: String, cause: Exception) : this(
30+
DEFAULT_MESSAGE,
31+
cause
32+
) {
3033
this.code = code
3134
this.description = description
3235
}
@@ -126,7 +129,10 @@ public class AuthenticationException : Auth0Exception {
126129
get() = "a0.invalid_configuration" == code
127130

128131
// When a user closes the browser app and in turn, cancels the authentication
129-
@Deprecated("This property can refer to both log in and log out actions.", replaceWith = ReplaceWith("isCanceled"))
132+
@Deprecated(
133+
"This property can refer to both log in and log out actions.",
134+
replaceWith = ReplaceWith("isCanceled")
135+
)
130136
public val isAuthenticationCanceled: Boolean
131137
get() = isCanceled
132138

@@ -183,7 +189,7 @@ public class AuthenticationException : Auth0Exception {
183189
/// When authenticating with web-based authentication using prompt=none and the auth0 session had expired
184190
public val isLoginRequired: Boolean
185191
get() = "login_required" == code
186-
192+
187193
/// User is deleted
188194
public val isRefreshTokenDeleted: Boolean
189195
get() = "invalid_grant" == code
@@ -205,6 +211,12 @@ public class AuthenticationException : Auth0Exception {
205211

206212
internal companion object {
207213
internal const val ERROR_VALUE_AUTHENTICATION_CANCELED = "a0.authentication_canceled"
214+
internal const val ERROR_KEY_URI_NULL = "a0.auth.authorize_uri"
215+
internal const val ERROR_VALUE_AUTHORIZE_URI_INVALID =
216+
"Authorization URI is received as null from the intent"
217+
internal const val ERROR_KEY_CT_OPTIONS_NULL = "a0.auth.ct_options"
218+
internal const val ERROR_VALUE_CT_OPTIONS_INVALID =
219+
"Custom tab options are received as null from the intent"
208220
private const val ERROR_KEY = "error"
209221
private const val CODE_KEY = "code"
210222
private const val DESCRIPTION_KEY = "description"

auth0/src/main/java/com/auth0/android/provider/AuthenticationActivity.kt

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import android.net.Uri
77
import android.os.Bundle
88
import androidx.annotation.VisibleForTesting
99
import com.auth0.android.authentication.AuthenticationException
10+
import com.auth0.android.authentication.AuthenticationException.Companion.ERROR_KEY_CT_OPTIONS_NULL
11+
import com.auth0.android.authentication.AuthenticationException.Companion.ERROR_KEY_URI_NULL
12+
import com.auth0.android.authentication.AuthenticationException.Companion.ERROR_VALUE_AUTHORIZE_URI_INVALID
13+
import com.auth0.android.authentication.AuthenticationException.Companion.ERROR_VALUE_CT_OPTIONS_INVALID
1014
import com.auth0.android.callback.RunnableTask
1115
import com.auth0.android.provider.WebAuthProvider.failure
1216
import com.auth0.android.provider.WebAuthProvider.resume
@@ -68,23 +72,44 @@ public open class AuthenticationActivity : Activity() {
6872
}
6973

7074
private fun launchAuthenticationIntent() {
71-
val extras = intent.extras
72-
val authorizeUri = extras!!.getParcelable<Uri>(EXTRA_AUTHORIZE_URI)
73-
val customTabsOptions: CustomTabsOptions = extras.getParcelable(EXTRA_CT_OPTIONS)!!
75+
val extras: Bundle? = intent.extras
76+
77+
val authorizeUri = extras?.getParcelable<Uri>(EXTRA_AUTHORIZE_URI)
78+
authorizeUri ?: run {
79+
deliverAuthenticationFailure(
80+
AuthenticationException(
81+
ERROR_KEY_URI_NULL, ERROR_VALUE_AUTHORIZE_URI_INVALID
82+
)
83+
)
84+
return
85+
}
86+
87+
val customTabsOptions: CustomTabsOptions? = extras.getParcelable(EXTRA_CT_OPTIONS)
88+
customTabsOptions ?: run {
89+
deliverAuthenticationFailure(
90+
AuthenticationException(
91+
ERROR_KEY_CT_OPTIONS_NULL, ERROR_VALUE_CT_OPTIONS_INVALID
92+
)
93+
)
94+
return
95+
}
96+
7497
val launchAsTwa: Boolean = extras.getBoolean(EXTRA_LAUNCH_AS_TWA, false)
7598
customTabsController = createCustomTabsController(this, customTabsOptions)
7699
customTabsController!!.bindService()
77-
customTabsController!!.launchUri(authorizeUri!!, launchAsTwa, getInstance(), object : RunnableTask<AuthenticationException> {
78-
override fun apply(error: AuthenticationException) {
79-
deliverAuthenticationFailure(error)
80-
}
81-
})
100+
customTabsController!!.launchUri(authorizeUri,
101+
launchAsTwa,
102+
getInstance(),
103+
object : RunnableTask<AuthenticationException> {
104+
override fun apply(error: AuthenticationException) {
105+
deliverAuthenticationFailure(error)
106+
}
107+
})
82108
}
83109

84110
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
85111
internal open fun createCustomTabsController(
86-
context: Context,
87-
options: CustomTabsOptions
112+
context: Context, options: CustomTabsOptions
88113
): CustomTabsController {
89114
return CustomTabsController(context, options, TwaLauncher(context))
90115
}
@@ -107,10 +132,7 @@ public open class AuthenticationActivity : Activity() {
107132

108133
@JvmStatic
109134
internal fun authenticateUsingBrowser(
110-
context: Context,
111-
authorizeUri: Uri,
112-
launchAsTwa: Boolean,
113-
options: CustomTabsOptions
135+
context: Context, authorizeUri: Uri, launchAsTwa: Boolean, options: CustomTabsOptions
114136
) {
115137
val intent = Intent(context, AuthenticationActivity::class.java)
116138
intent.putExtra(EXTRA_AUTHORIZE_URI, authorizeUri)

0 commit comments

Comments
 (0)