Skip to content

Commit

Permalink
add embedded mode authentication activity
Browse files Browse the repository at this point in the history
  • Loading branch information
frontegg-david committed Oct 7, 2023
1 parent 2289f86 commit b023c37
Show file tree
Hide file tree
Showing 63 changed files with 1,627 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
.externalNativeBuild
.cxx
local.properties
embedded/build
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ android {
Add `INTERNET` permission to the app's manifest file.

```xml

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
```
Expand Down
1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4"
implementation "androidx.browser:browser:1.6.0"
implementation 'com.google.androidbrowserhelper:androidbrowserhelper:2.5.0'
implementation 'androidx.core:core-ktx:+'
}

afterEvaluate {
Expand Down
6 changes: 6 additions & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<application>

<activity
android:name="com.frontegg.android.embedded.EmbeddedAuthActivity"
android:exported="true" />

<activity
android:name="com.google.androidbrowserhelper.trusted.LauncherActivity"
android:exported="true">
Expand Down Expand Up @@ -50,6 +55,7 @@
</intent-filter>
</activity>


</application>
<queries>
<intent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.net.Uri
import android.os.Bundle
import android.util.Log
import androidx.browser.customtabs.CustomTabsIntent
import com.frontegg.android.embedded.EmbeddedAuthActivity
import com.frontegg.android.utils.AuthorizeUrlGenerator

class AuthenticationActivity : Activity() {
Expand Down Expand Up @@ -49,7 +50,7 @@ class AuthenticationActivity : Activity() {
}
} else {
val intentUrl = intent.data
if(intentUrl == null){
if (intentUrl == null) {
setResult(RESULT_CANCELED)
finish()
return
Expand Down Expand Up @@ -116,12 +117,16 @@ class AuthenticationActivity : Activity() {
private val TAG = AuthenticationActivity::class.java.simpleName

fun authenticateUsingBrowser(activity: Activity) {
val intent = Intent(activity, AuthenticationActivity::class.java)
val authorizeUri = AuthorizeUrlGenerator().generate()
intent.putExtra(AUTH_LAUNCHED, true)
intent.putExtra(AUTHORIZE_URI, authorizeUri.first)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
activity.startActivityForResult(intent, OAUTH_LOGIN_REQUEST)
if (FronteggApp.getInstance().isEmbeddedMode) {
EmbeddedAuthActivity.authenticate(activity)
} else {
val intent = Intent(activity, AuthenticationActivity::class.java)
val authorizeUri = AuthorizeUrlGenerator().generate()
intent.putExtra(AUTH_LAUNCHED, true)
intent.putExtra(AUTHORIZE_URI, authorizeUri.first)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
activity.startActivityForResult(intent, OAUTH_LOGIN_REQUEST)
}
}
}
}
6 changes: 4 additions & 2 deletions android/src/main/java/com/frontegg/android/FronteggApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import com.frontegg.android.services.*
class FronteggApp private constructor(
val context: Context,
val baseUrl: String,
val clientId: String
val clientId: String,
val isEmbeddedMode:Boolean
) {

val credentialManager: CredentialManager = CredentialManager(context)
Expand All @@ -33,14 +34,15 @@ class FronteggApp private constructor(
fronteggDomain: String,
clientId: String,
context: Context,
isEmbeddedMode:Boolean = false,
) {
val baseUrl: String = if (fronteggDomain.startsWith("https")) {
throw FronteggException(FRONTEGG_DOMAIN_MUST_NOT_START_WITH_HTTPS)
} else {
"https://$fronteggDomain"
}

instance = FronteggApp(context, baseUrl, clientId)
instance = FronteggApp(context, baseUrl, clientId, isEmbeddedMode)
}
}

Expand Down
20 changes: 14 additions & 6 deletions android/src/main/java/com/frontegg/android/FronteggAuth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.os.Handler
import android.os.Looper
import android.util.Log
import android.webkit.CookieManager
import com.frontegg.android.embedded.EmbeddedAuthActivity
import com.frontegg.android.models.User
import com.frontegg.android.services.Api
import com.frontegg.android.services.CredentialManager
Expand All @@ -18,6 +19,7 @@ import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import java.lang.Exception
import java.time.Instant
import java.util.*
import kotlin.concurrent.schedule
Expand Down Expand Up @@ -88,12 +90,18 @@ class FronteggAuth(
private fun refreshTokenIfNeeded(): Boolean {
val refreshToken = this.refreshToken.value ?: return false
Log.d(TAG, "refreshTokenIfNeeded()")
val data = api.refreshToken(refreshToken)
return if (data != null) {
setCredentials(data.access_token, data.refresh_token)
true
} else {
false

return try {
val data = api.refreshToken(refreshToken)
if (data != null) {
setCredentials(data.access_token, data.refresh_token)
true
} else {
false
}
} catch (e: Exception) {
Log.e(TAG, "Failed to send refresh token request", e)
false;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.frontegg.android.embedded

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.LinearLayout
import com.frontegg.android.FronteggAuth
import com.frontegg.android.R
import com.frontegg.android.utils.AuthorizeUrlGenerator
import com.frontegg.android.utils.NullableObject
import io.reactivex.rxjava3.disposables.Disposable
import io.reactivex.rxjava3.functions.Consumer

class EmbeddedAuthActivity : Activity() {

lateinit var webView: FronteggWebView
var webViewUrl: String? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_embedded_auth)
overridePendingTransition(R.anim.fadein, R.anim.fadeout)

webView = findViewById(R.id.custom_webview)
loaderLayout = findViewById(R.id.loaderView)

val authorizeUrl = AuthorizeUrlGenerator()
val url = authorizeUrl.generate()

webViewUrl = url.first


if (!FronteggAuth.instance.initializing.value
&& !FronteggAuth.instance.isAuthenticated.value
) {
webView.loadUrl(webViewUrl!!)
webViewUrl = null
}

}

private val disposables: ArrayList<Disposable> = arrayListOf()
private var loaderLayout: LinearLayout? = null

private val showLoaderConsumer: Consumer<NullableObject<Boolean>> = Consumer {
Log.d(TAG, "showLoaderConsumer: ${it.value}")
runOnUiThread {
loaderLayout?.visibility = if (it.value) View.VISIBLE else View.GONE
}
}
private val isAuthenticatedConsumer: Consumer<NullableObject<Boolean>> = Consumer {
Log.d(TAG, "isAuthenticatedConsumer: ${it.value}")
if (it.value) {
runOnUiThread {
navigateToAuthenticated()
}
}
}

private fun navigateToAuthenticated() {
setResult(RESULT_OK)
finish()
}
override fun onResume() {
super.onResume()
disposables.add(FronteggAuth.instance.showLoader.subscribe(this.showLoaderConsumer))
disposables.add(FronteggAuth.instance.isAuthenticated.subscribe(this.isAuthenticatedConsumer))

}

override fun onPause() {
super.onPause()
disposables.forEach {
it.dispose()
}
}


companion object {
const val OAUTH_LOGIN_REQUEST = 100001
const val AUTHORIZE_URI = "com.frontegg.android.AUTHORIZE_URI"
private const val AUTH_LAUNCHED = "com.frontegg.android.AUTH_LAUNCHED"
private val TAG = EmbeddedAuthActivity::class.java.simpleName

fun authenticate(activity: Activity) {
val intent = Intent(activity, EmbeddedAuthActivity::class.java)

val authorizeUri = AuthorizeUrlGenerator().generate()
intent.putExtra(AUTH_LAUNCHED, true)
// intent.putExtra(AUTHORIZE_URI, authorizeUri.first)
// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
activity.startActivityForResult(intent, OAUTH_LOGIN_REQUEST)
}
}
}
Loading

0 comments on commit b023c37

Please sign in to comment.