Implement native web auth
This commit is contained in:
@@ -48,6 +48,10 @@ android {
|
||||
signingConfig signingConfigs.debug
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'androidx.browser:browser:1.0.0'
|
||||
}
|
||||
}
|
||||
|
||||
flutter {
|
||||
|
||||
@@ -45,8 +45,8 @@
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<activity android:name="com.linusu.flutter_web_auth.CallbackActivity" >
|
||||
<intent-filter android:label="flutter_web_auth">
|
||||
<activity android:name=".TbWebCallbackActivity" >
|
||||
<intent-filter android:label="tb_web_auth">
|
||||
<action android:name="android.intent.action.VIEW" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
<category android:name="android.intent.category.BROWSABLE" />
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.thingsboard.app
|
||||
|
||||
import android.app.Service
|
||||
import android.content.Intent
|
||||
import android.os.Binder
|
||||
import android.os.IBinder
|
||||
|
||||
class KeepAliveService: Service() {
|
||||
companion object {
|
||||
val binder = Binder()
|
||||
}
|
||||
|
||||
override fun onBind(intent: Intent): IBinder {
|
||||
return binder
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,20 @@
|
||||
package org.thingsboard.app
|
||||
|
||||
import androidx.annotation.NonNull
|
||||
import io.flutter.embedding.android.FlutterActivity
|
||||
import io.flutter.embedding.engine.FlutterEngine
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
|
||||
class MainActivity: FlutterActivity() {
|
||||
|
||||
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
|
||||
super.configureFlutterEngine(flutterEngine)
|
||||
registerTbWebAuth(flutterEngine)
|
||||
}
|
||||
|
||||
fun registerTbWebAuth(flutterEngine: FlutterEngine) {
|
||||
val channel = MethodChannel(flutterEngine.dartExecutor, "tb_web_auth")
|
||||
channel.setMethodCallHandler(TbWebAuthHandler(this))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package org.thingsboard.app
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
|
||||
import androidx.browser.customtabs.CustomTabsIntent
|
||||
|
||||
import io.flutter.plugin.common.MethodCall
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
|
||||
import io.flutter.plugin.common.MethodChannel.Result
|
||||
import io.flutter.plugin.common.PluginRegistry.Registrar
|
||||
|
||||
class TbWebAuthHandler(private val context: Context): MethodCallHandler {
|
||||
companion object {
|
||||
val callbacks = mutableMapOf<String, Result>()
|
||||
}
|
||||
|
||||
override fun onMethodCall(call: MethodCall, resultCallback: Result) {
|
||||
when (call.method) {
|
||||
"authenticate" -> {
|
||||
val url = Uri.parse(call.argument("url"))
|
||||
val callbackUrlScheme = call.argument<String>("callbackUrlScheme")!!
|
||||
val saveHistory = call.argument<Boolean>("saveHistory")
|
||||
|
||||
callbacks[callbackUrlScheme] = resultCallback
|
||||
|
||||
val intent = CustomTabsIntent.Builder().build()
|
||||
val keepAliveIntent = Intent(context, KeepAliveService::class.java)
|
||||
|
||||
intent.intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
if (saveHistory != null && !saveHistory) {
|
||||
intent.intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
|
||||
}
|
||||
intent.intent.putExtra("android.support.customtabs.extra.KEEP_ALIVE", keepAliveIntent)
|
||||
|
||||
intent.launchUrl(context, url)
|
||||
}
|
||||
"cleanUpDanglingCalls" -> {
|
||||
callbacks.forEach{ (_, danglingResultCallback) ->
|
||||
danglingResultCallback.error("CANCELED", "User canceled login", null)
|
||||
}
|
||||
callbacks.clear()
|
||||
resultCallback.success(null)
|
||||
}
|
||||
else -> resultCallback.notImplemented()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.thingsboard.app
|
||||
|
||||
import android.app.Activity
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
|
||||
class TbWebCallbackActivity: Activity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
val url = intent?.data
|
||||
val scheme = url?.scheme
|
||||
|
||||
if (scheme != null) {
|
||||
TbWebAuthHandler.callbacks.remove(scheme)?.success(url.toString())
|
||||
}
|
||||
|
||||
finish()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user