diff --git a/android/build.gradle b/android/build.gradle index d0b48f4..a779030 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -20,7 +20,6 @@ android { testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles "consumer-rules.pro" versionName "$version" - } buildTypes { diff --git a/android/src/main/java/com/frontegg/android/FronteggApp.kt b/android/src/main/java/com/frontegg/android/FronteggApp.kt index 7dafaba..4fec1b5 100644 --- a/android/src/main/java/com/frontegg/android/FronteggApp.kt +++ b/android/src/main/java/com/frontegg/android/FronteggApp.kt @@ -7,18 +7,22 @@ import android.content.pm.PackageManager.MATCH_ALL import com.frontegg.android.exceptions.FronteggException import com.frontegg.android.exceptions.FronteggException.Companion.FRONTEGG_APP_MUST_BE_INITIALIZED import com.frontegg.android.exceptions.FronteggException.Companion.FRONTEGG_DOMAIN_MUST_NOT_START_WITH_HTTPS +import com.frontegg.android.regions.RegionConfig import com.frontegg.android.services.* class FronteggApp private constructor( val context: Context, val baseUrl: String, val clientId: String, - val isEmbeddedMode: Boolean = true + val isEmbeddedMode: Boolean = true, + val regions: List = listOf(), + val selectedRegion: RegionConfig? = null ) { val credentialManager: CredentialManager = CredentialManager(context) val api: Api = Api(baseUrl, clientId, credentialManager) - val auth: FronteggAuth = FronteggAuth(baseUrl, clientId, api, credentialManager) + val auth: FronteggAuth = + FronteggAuth(baseUrl, clientId, api, credentialManager, regions, selectedRegion) val packageName: String = context.packageName companion object { @@ -47,6 +51,30 @@ class FronteggApp private constructor( instance = FronteggApp(context, baseUrl, clientId, isEmbeddedMode) } + public fun initWithRegions(regions: List, context: Context): FronteggApp { + + val isEmbeddedMode = isActivityEnabled(context, EmbeddedAuthActivity::class.java.name) + val selectedRegion = CredentialManager(context).getSelectedRegion() + if (selectedRegion != null) { + val regionConfig = regions.find { it.key == selectedRegion } + + if (regionConfig != null) { + val newInstance = FronteggApp( + context, + regionConfig.baseUrl, + regionConfig.clientId, + isEmbeddedMode, + regions + ) + instance = newInstance + return newInstance + } + } + val newInstance = FronteggApp(context, "", "", isEmbeddedMode, regions) + instance = newInstance + return newInstance + } + private fun isActivityEnabled(context: Context, activityClassName: String): Boolean { return try { diff --git a/android/src/main/java/com/frontegg/android/FronteggAuth.kt b/android/src/main/java/com/frontegg/android/FronteggAuth.kt index 0164fc0..9c3bc04 100644 --- a/android/src/main/java/com/frontegg/android/FronteggAuth.kt +++ b/android/src/main/java/com/frontegg/android/FronteggAuth.kt @@ -7,6 +7,7 @@ import android.os.Looper import android.util.Log import android.webkit.CookieManager import com.frontegg.android.models.User +import com.frontegg.android.regions.RegionConfig import com.frontegg.android.services.Api import com.frontegg.android.services.CredentialManager import com.frontegg.android.utils.Constants @@ -29,7 +30,9 @@ class FronteggAuth( val baseUrl: String, val clientId: String, val api: Api, - val credentialManager: CredentialManager + val credentialManager: CredentialManager, + val regions: List, + val selectedRegion: RegionConfig? ) { companion object { @@ -55,6 +58,7 @@ class FronteggAuth( init { + Observable.merge( isLoading.observable, isAuthenticated.observable, @@ -65,8 +69,8 @@ class FronteggAuth( GlobalScope.launch(Dispatchers.IO) { - val accessTokenSaved = credentialManager.getOrNull(CredentialKeys.ACCESS_TOKEN) - val refreshTokenSaved = credentialManager.getOrNull(CredentialKeys.REFRESH_TOKEN) + val accessTokenSaved = credentialManager.get(CredentialKeys.ACCESS_TOKEN) + val refreshTokenSaved = credentialManager.get(CredentialKeys.REFRESH_TOKEN) if (accessTokenSaved != null && refreshTokenSaved != null) { accessToken.value = accessTokenSaved @@ -144,6 +148,10 @@ class FronteggAuth( val codeVerifier = credentialManager.get(CredentialKeys.CODE_VERIFIER) val redirectUrl = Constants.oauthCallbackUrl(baseUrl) + if(codeVerifier == null){ + return false + } + GlobalScope.launch(Dispatchers.IO) { val data = api.exchangeToken(code, redirectUrl, codeVerifier) if (data != null) { diff --git a/android/src/main/java/com/frontegg/android/regions/RegionConfig.kt b/android/src/main/java/com/frontegg/android/regions/RegionConfig.kt new file mode 100644 index 0000000..afb4bb6 --- /dev/null +++ b/android/src/main/java/com/frontegg/android/regions/RegionConfig.kt @@ -0,0 +1,3 @@ +package com.frontegg.android.regions + +class RegionConfig(val key: String, val clientId: String, val baseUrl: String) {} \ No newline at end of file diff --git a/android/src/main/java/com/frontegg/android/services/Api.kt b/android/src/main/java/com/frontegg/android/services/Api.kt index 462ac77..dac61c4 100644 --- a/android/src/main/java/com/frontegg/android/services/Api.kt +++ b/android/src/main/java/com/frontegg/android/services/Api.kt @@ -43,7 +43,7 @@ open class Api( headers[it.key] = it.value } - val accessToken = this.credentialManager.getOrNull(CredentialKeys.ACCESS_TOKEN) + val accessToken = this.credentialManager.get(CredentialKeys.ACCESS_TOKEN) if (accessToken != null) { headers["Authorization"] = "Bearer $accessToken" } diff --git a/android/src/main/java/com/frontegg/android/services/CredentialManager.kt b/android/src/main/java/com/frontegg/android/services/CredentialManager.kt index 2b7955a..2ffa65c 100644 --- a/android/src/main/java/com/frontegg/android/services/CredentialManager.kt +++ b/android/src/main/java/com/frontegg/android/services/CredentialManager.kt @@ -28,20 +28,11 @@ open class CredentialManager(context: Context) { return sp.edit().putString(key.toString(), value).commit() } - /** - * Get value by key from the shared preference - */ - @Throws(KeyNotFoundException::class) - fun get(key: CredentialKeys): String { - Log.d(TAG, "get Frontegg $key in shared preference ") - return sp.getString(key.toString(), null) - ?: throw KeyNotFoundException(Throwable("key not found $key")) - } /** * Get value by key from the shared preference */ - fun getOrNull(key: CredentialKeys): String? { + fun get(key: CredentialKeys): String? { Log.d(TAG, "get Frontegg $key in shared preference ") return sp.getString(key.toString(), null) } @@ -55,4 +46,21 @@ open class CredentialManager(context: Context) { Log.d(TAG, "clear Frontegg shared preference ") sp.edit().clear().commit() } + + fun getCodeVerifier(): String? { + return this.get(CredentialKeys.CODE_VERIFIER) + } + + fun saveCodeVerifier(codeVerifier: String): Boolean { + return this.save(CredentialKeys.CODE_VERIFIER, codeVerifier) + } + + + fun getSelectedRegion(): String? { + return this.get(CredentialKeys.SELECTED_REGION) + } + + fun saveSelectedRegion(selectedRegion: String): Boolean { + return this.save(CredentialKeys.SELECTED_REGION, selectedRegion) + } } \ No newline at end of file diff --git a/android/src/main/java/com/frontegg/android/utils/AuthorizeUrlGenerator.kt b/android/src/main/java/com/frontegg/android/utils/AuthorizeUrlGenerator.kt index 9854515..7da25c4 100644 --- a/android/src/main/java/com/frontegg/android/utils/AuthorizeUrlGenerator.kt +++ b/android/src/main/java/com/frontegg/android/utils/AuthorizeUrlGenerator.kt @@ -41,7 +41,7 @@ class AuthorizeUrlGenerator { val codeChallenge = generateCodeChallenge(codeVerifier) val credentialManager = FronteggApp.getInstance().credentialManager - credentialManager.save(CredentialKeys.CODE_VERIFIER, codeVerifier) + credentialManager.saveCodeVerifier(codeVerifier) val redirectUrl = Constants.oauthCallbackUrl(baseUrl) val authorizeUrlBuilder = Uri.Builder() diff --git a/android/src/main/java/com/frontegg/android/utils/CredentialKeys.kt b/android/src/main/java/com/frontegg/android/utils/CredentialKeys.kt index 653a2d4..729e808 100644 --- a/android/src/main/java/com/frontegg/android/utils/CredentialKeys.kt +++ b/android/src/main/java/com/frontegg/android/utils/CredentialKeys.kt @@ -4,5 +4,6 @@ package com.frontegg.android.utils public enum class CredentialKeys(key: String) { ACCESS_TOKEN("access_token"), REFRESH_TOKEN("refresh_token"), - CODE_VERIFIER("code_verifier") + CODE_VERIFIER("code_verifier"), + SELECTED_REGION("selected_region") } \ No newline at end of file diff --git a/app/src/main/java/com/frontegg/demo/App.kt b/app/src/main/java/com/frontegg/demo/App.kt index 1a06112..8c7fbdd 100644 --- a/app/src/main/java/com/frontegg/demo/App.kt +++ b/app/src/main/java/com/frontegg/demo/App.kt @@ -2,6 +2,7 @@ package com.frontegg.demo import android.app.Application import com.frontegg.android.FronteggApp +import com.frontegg.demo.BuildConfig class App : Application() { diff --git a/multi-region/build.gradle b/multi-region/build.gradle index f3178a2..a8f6f3a 100644 --- a/multi-region/build.gradle +++ b/multi-region/build.gradle @@ -1,8 +1,8 @@ plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' -} +} def fronteggDomain = "auth.davidantoon.me" def fronteggClientId = "b6adfe4c-d695-4c04-b95f-3ec9fd0c6cca" @@ -26,9 +26,6 @@ android { "frontegg_client_id": fronteggClientId ] - buildConfigField "String", 'FRONTEGG_DOMAIN', "\"$fronteggDomain\"" - buildConfigField "String", 'FRONTEGG_CLIENT_ID', "\"$fronteggClientId\"" - } buildTypes { diff --git a/multi-region/src/main/AndroidManifest.xml b/multi-region/src/main/AndroidManifest.xml index 0d16b33..d575ccf 100644 --- a/multi-region/src/main/AndroidManifest.xml +++ b/multi-region/src/main/AndroidManifest.xml @@ -24,6 +24,41 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/multi-region/src/main/java/com/frontegg/demo/App.kt b/multi-region/src/main/java/com/frontegg/demo/App.kt index bc312ba..b751b17 100644 --- a/multi-region/src/main/java/com/frontegg/demo/App.kt +++ b/multi-region/src/main/java/com/frontegg/demo/App.kt @@ -2,19 +2,30 @@ package com.frontegg.demo import android.app.Application import com.frontegg.android.FronteggApp +import com.frontegg.android.regions.RegionConfig class App : Application() { companion object { - public lateinit var instance: App + lateinit var instance: App } override fun onCreate() { super.onCreate() instance = this - FronteggApp.init( - BuildConfig.FRONTEGG_DOMAIN, - BuildConfig.FRONTEGG_CLIENT_ID, + FronteggApp.initWithRegions( + listOf( + RegionConfig( + "eu", + "auth.davidantoon.me", + "b6adfe4c-d695-4c04-b95f-3ec9fd0c6cca" + ), + RegionConfig( + "us", + "davidprod.frontegg.com", + "d7d07347-2c57-4450-8418-0ec7ee6e096b" + ) + ), this ) } diff --git a/settings.gradle b/settings.gradle index 0ab64fb..9cc303d 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,6 +1,5 @@ pluginManagement { repositories { - gradlePluginPortal() google() mavenCentral() } @@ -19,4 +18,3 @@ include ':android' include ':app' include ':embedded' include ':multi-region' -