Skip to content

Commit

Permalink
Merge pull request #1 from ThuySinhNhat/feat/create_option_webview
Browse files Browse the repository at this point in the history
Feature - Create option webview
  • Loading branch information
ThuySinhNhat committed May 24, 2024
2 parents b8d4df2 + 9eb2449 commit eb9988b
Show file tree
Hide file tree
Showing 18 changed files with 389 additions and 72 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ gen/
.gradle/
build/

sample
sample/

# Local configuration file (sdk path, etc)
local.properties

Expand Down
49 changes: 14 additions & 35 deletions auth0/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,13 @@

plugins {
id 'kotlin-android'
id "com.auth0.gradle.oss-library.android" version "0.18.0"
id 'com.android.library'
id "org.jetbrains.dokka" version "1.4.20"
id 'maven-publish'
}

logger.lifecycle("Using version ${version} for ${name}")

def signingKey = findProperty('signingKey')
def signingKeyPwd = findProperty('signingPassword')

oss {
name 'Auth0.Android'
repository 'Auth0.Android'
organization 'auth0'
description 'Android toolkit for Auth0 API'
skipAssertSigningConfiguration true

developers {
auth0 {
displayName = 'Auth0'
email = '[email protected]'
}
lbalmaceda {
displayName = 'Luciano Balmaceda'
email = '[email protected]'
}
}
}

signing {
useInMemoryPgpKeys(signingKey, signingKeyPwd)
}

android {
compileSdkVersion 31

Expand Down Expand Up @@ -101,20 +76,14 @@ ext {
coroutinesVersion = '1.6.2'
}

// Configure javadoc jar to use dokka output
// TODO update oss-plugin to use dokka instead of doing it here
javadocJar {
dependsOn "dokkaJavadoc"
from "$buildDir/dokka/javadoc"
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'androidx.browser:browser:1.4.0'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion"
implementation "com.squareup.okhttp3:okhttp:$okhttpVersion"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.0"

implementation "com.squareup.okhttp3:logging-interceptor:$okhttpVersion"
implementation 'com.google.code.gson:gson:2.8.9'
Expand All @@ -134,4 +103,14 @@ dependencies {
testImplementation 'org.robolectric:robolectric:4.6.1'
testImplementation 'androidx.test.espresso:espresso-intents:3.5.1'
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesVersion"
}
}

project.afterEvaluate {
publishing {
publications {
release(MavenPublication) {
from components.release
}
}
}
}
5 changes: 4 additions & 1 deletion auth0/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
android:launchMode="singleTask"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />

<activity android:name="com.auth0.android.provider.WebAuthActivity" />

<activity
android:name="com.auth0.android.provider.RedirectActivity"
android:exported="true">
<intent-filter android:autoVerify="true"
<intent-filter
android:autoVerify="true"
tools:targetApi="m">
<action android:name="android.intent.action.VIEW" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ KeyStore.PrivateKeyEntry getRSAKeyEntry() throws CryptoException, IncompatibleDe
.setEndDate(end.getTime());

KeyguardManager kManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//The next call can return null when the LockScreen is not configured
Intent authIntent = kManager.createConfirmDeviceCredentialIntent(null, null);
boolean keyguardEnabled = kManager.isKeyguardSecure() && authIntent != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,30 @@ public open class AuthenticationActivity : Activity() {
private fun launchAuthenticationIntent() {
val extras = intent.extras
val authorizeUri = extras!!.getParcelable<Uri>(EXTRA_AUTHORIZE_URI)
if (extras.getBoolean(EXTRA_USE_WEB_VIEW, false)) {
val intent = Intent(this, WebAuthActivity::class.java)
intent.setData(authorizeUri)
intent.putExtra(
WebAuthActivity.FULLSCREEN_EXTRA,
extras.getBoolean(EXTRA_USE_FULL_SCREEN)
)
//The request code value can be ignored
startActivityForResult(intent, 33)
return
}
val customTabsOptions: CustomTabsOptions = extras.getParcelable(EXTRA_CT_OPTIONS)!!
val launchAsTwa: Boolean = extras.getBoolean(EXTRA_LAUNCH_AS_TWA, false)
customTabsController = createCustomTabsController(this, customTabsOptions)
customTabsController!!.bindService()
customTabsController!!.launchUri(authorizeUri!!, launchAsTwa, getInstance(), object : RunnableTask<AuthenticationException> {
override fun apply(error: AuthenticationException) {
deliverAuthenticationFailure(error)
}
})
customTabsController!!.launchUri(
authorizeUri!!,
launchAsTwa,
getInstance(),
object : RunnableTask<AuthenticationException> {
override fun apply(error: AuthenticationException) {
deliverAuthenticationFailure(error)
}
})
}

@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
Expand All @@ -103,6 +118,8 @@ public open class AuthenticationActivity : Activity() {
const val EXTRA_AUTHORIZE_URI = "com.auth0.android.EXTRA_AUTHORIZE_URI"
const val EXTRA_LAUNCH_AS_TWA = "com.auth0.android.EXTRA_LAUNCH_AS_TWA"
const val EXTRA_CT_OPTIONS = "com.auth0.android.EXTRA_CT_OPTIONS"
const val EXTRA_USE_WEB_VIEW = "com.auth0.android.EXTRA_USE_WEB_VIEW"
const val EXTRA_USE_FULL_SCREEN = "com.auth0.android.EXTRA_USE_FULL_SCREEN"
private const val EXTRA_INTENT_LAUNCHED = "com.auth0.android.EXTRA_INTENT_LAUNCHED"

@JvmStatic
Expand All @@ -116,8 +133,23 @@ public open class AuthenticationActivity : Activity() {
intent.putExtra(EXTRA_AUTHORIZE_URI, authorizeUri)
intent.putExtra(EXTRA_LAUNCH_AS_TWA, launchAsTwa)
intent.putExtra(EXTRA_CT_OPTIONS, options)
intent.putExtra(EXTRA_USE_WEB_VIEW, false)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
context.startActivity(intent)
}

fun authenticateUsingWebView(
activity: Activity,
authorizeUri: Uri?,
requestCode: Int,
useFullScreen: Boolean,
) {
val intent = Intent(activity, AuthenticationActivity::class.java)
intent.putExtra(EXTRA_AUTHORIZE_URI, authorizeUri)
intent.putExtra(EXTRA_USE_WEB_VIEW, true)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.putExtra(EXTRA_USE_FULL_SCREEN, useFullScreen)
activity.startActivityForResult(intent, requestCode)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package com.auth0.android.provider;

import static androidx.browser.customtabs.CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static androidx.browser.customtabs.CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION;

/**
* Class used to match any browser, preferring Custom Tabs compatible browsers
* and browsers that are selected as the default browser application in the device settings.
Expand Down Expand Up @@ -69,8 +70,8 @@ public BrowserPicker[] newArray(int size) {
* @return a new BrowserPicker.Builder ready to customize.
*/
@NonNull
public static BrowserPicker.Builder newBuilder() {
return new BrowserPicker.Builder();
public static Builder newBuilder() {
return new Builder();
}

public static class Builder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import com.auth0.android.authentication.AuthenticationException;
import com.auth0.android.callback.RunnableTask;
import com.auth0.android.request.internal.CommonThreadSwitcher;
import com.auth0.android.request.internal.ThreadSwitcher;
import com.google.androidbrowserhelper.trusted.TwaLauncher;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.browser.customtabs.CustomTabColorSchemeParams;
import androidx.browser.customtabs.CustomTabsIntent;
import androidx.browser.customtabs.CustomTabsSession;
import androidx.browser.trusted.TrustedWebActivityIntent;
import androidx.browser.trusted.TrustedWebActivityIntentBuilder;
import androidx.core.content.ContextCompat;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.auth0.android.provider

import com.auth0.android.provider.SignatureVerifier
import java.util.*
import java.util.Date

internal class IdTokenVerificationOptions(
val issuer: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.auth0.android.provider

import com.auth0.android.request.internal.Jwt
import android.text.TextUtils
import com.auth0.android.request.internal.Jwt
import java.util.*

internal class IdTokenVerifier {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.util.Log
import com.auth0.android.Auth0
import com.auth0.android.authentication.AuthenticationException
import com.auth0.android.callback.Callback
import java.util.*

internal class LogoutManager(
private val account: Auth0,
Expand Down
36 changes: 30 additions & 6 deletions auth0/src/main/java/com/auth0/android/provider/OAuthManager.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.auth0.android.provider

import android.content.Context
import android.app.Activity
import android.net.Uri
import android.text.TextUtils
import android.util.Base64
import android.util.Log
import androidx.annotation.ChecksSdkIntAtLeast
import androidx.annotation.VisibleForTesting
import com.auth0.android.Auth0
import com.auth0.android.Auth0Exception
Expand All @@ -31,6 +30,8 @@ internal class OAuthManager(
private val apiClient: AuthenticationAPIClient
private var requestCode = 0
private var pkce: PKCE? = null
private var useFullScreen = false
private var useWebView: Boolean = false

private var _currentTimeInMillis: Long? = null

Expand All @@ -57,14 +58,28 @@ internal class OAuthManager(
idTokenVerificationIssuer = if (TextUtils.isEmpty(issuer)) apiClient.baseURL else issuer
}

fun startAuthentication(context: Context, redirectUri: String, requestCode: Int) {
fun useFullScreen(useFullScreen: Boolean) {
this.useFullScreen = useFullScreen
}

fun useWebView(useWebView: Boolean) {
this.useWebView = useWebView
}

fun startAuthentication(activity: Activity, redirectUri: String, requestCode: Int) {
OidcUtils.includeDefaultScope(parameters)
addPKCEParameters(parameters, redirectUri, headers)
addClientParameters(parameters, redirectUri)
addValidationParameters(parameters)
val uri = buildAuthorizeUri()
this.requestCode = requestCode
AuthenticationActivity.authenticateUsingBrowser(context, uri, launchAsTwa, ctOptions)
if (useWebView) {
AuthenticationActivity.authenticateUsingWebView(
activity, uri, requestCode, useFullScreen
)
} else {
AuthenticationActivity.authenticateUsingBrowser(activity, uri, launchAsTwa, ctOptions)
}
}

fun setHeaders(headers: Map<String, String>) {
Expand Down Expand Up @@ -204,13 +219,22 @@ internal class OAuthManager(
errorDescription ?: "Permissions were not granted. Try again."
)
}

ERROR_VALUE_UNAUTHORIZED.equals(errorValue, ignoreCase = true) -> {
throw AuthenticationException(ERROR_VALUE_UNAUTHORIZED, errorDescription ?: unknownErrorDescription)
throw AuthenticationException(
ERROR_VALUE_UNAUTHORIZED,
errorDescription ?: unknownErrorDescription
)
}

ERROR_VALUE_LOGIN_REQUIRED == errorValue -> {
//Whitelist to allow SSO errors go through
throw AuthenticationException(errorValue, errorDescription ?: unknownErrorDescription)
throw AuthenticationException(
errorValue,
errorDescription ?: unknownErrorDescription
)
}

else -> {
throw AuthenticationException(
errorValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import androidx.annotation.NonNull;

import com.auth0.android.Auth0Exception;
import com.auth0.android.authentication.AuthenticationException;

/**
Expand Down
Loading

0 comments on commit eb9988b

Please sign in to comment.