Skip to content

Commit

Permalink
Add region support in FronteggApp initialization function
Browse files Browse the repository at this point in the history
  • Loading branch information
frontegg-david committed Nov 24, 2023
1 parent 16e924a commit 9d03c3e
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 29 deletions.
1 change: 0 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ android {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
versionName "$version"

}

buildTypes {
Expand Down
32 changes: 30 additions & 2 deletions android/src/main/java/com/frontegg/android/FronteggApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<RegionConfig> = 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 {
Expand Down Expand Up @@ -47,6 +51,30 @@ class FronteggApp private constructor(
instance = FronteggApp(context, baseUrl, clientId, isEmbeddedMode)
}

public fun initWithRegions(regions: List<RegionConfig>, 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 {
Expand Down
14 changes: 11 additions & 3 deletions android/src/main/java/com/frontegg/android/FronteggAuth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,7 +30,9 @@ class FronteggAuth(
val baseUrl: String,
val clientId: String,
val api: Api,
val credentialManager: CredentialManager
val credentialManager: CredentialManager,
val regions: List<RegionConfig>,
val selectedRegion: RegionConfig?
) {

companion object {
Expand All @@ -55,6 +58,7 @@ class FronteggAuth(

init {


Observable.merge(
isLoading.observable,
isAuthenticated.observable,
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.frontegg.android.regions

class RegionConfig(val key: String, val clientId: String, val baseUrl: String) {}
2 changes: 1 addition & 1 deletion android/src/main/java/com/frontegg/android/services/Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
1 change: 1 addition & 0 deletions app/src/main/java/com/frontegg/demo/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down
5 changes: 1 addition & 4 deletions multi-region/build.gradle
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -26,9 +26,6 @@ android {
"frontegg_client_id": fronteggClientId
]

buildConfigField "String", 'FRONTEGG_DOMAIN', "\"$fronteggDomain\""
buildConfigField "String", 'FRONTEGG_CLIENT_ID', "\"$fronteggClientId\""

}

buildTypes {
Expand Down
35 changes: 35 additions & 0 deletions multi-region/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,41 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>


<activity
android:name="com.frontegg.android.EmbeddedAuthActivity"
android:exported="true"
tools:node="merge">
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data android:scheme="https" />
<data android:host="davidprod.frontegg.com" />
<data android:pathPrefix="/oauth/account/activate" />
<data android:pathPrefix="/oauth/account/invitation/accept" />
<data android:pathPrefix="/oauth/account/reset-password" />
<data android:pathPrefix="/oauth/account/social/success" />
<data android:pathPrefix="/oauth/account/login/magic-link" />
</intent-filter>
</activity>

<activity
android:name="com.frontegg.android.AuthenticationActivity"
tools:node="merge"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="davidprod.frontegg.com"
android:scheme="${package_name}" />
</intent-filter>
</activity>
</application>

</manifest>
19 changes: 15 additions & 4 deletions multi-region/src/main/java/com/frontegg/demo/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand Down
2 changes: 0 additions & 2 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
Expand All @@ -19,4 +18,3 @@ include ':android'
include ':app'
include ':embedded'
include ':multi-region'

0 comments on commit 9d03c3e

Please sign in to comment.