Archived
Menyelesaikan plugin, siap ucob
This commit is contained in:
1 parent
6f5478e9ab
commit
caf005b82d
14 files changed
+312
-15
No files matched your search
@@ -0,0 +1,2 @@
|
||||
/build
|
||||
/.tauri
|
||||
@@ -0,0 +1,48 @@
|
||||
plugins {
|
||||
id("com.android.library")
|
||||
id("org.jetbrains.kotlin.android")
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "com.plugin.auth"
|
||||
compileSdk = 36
|
||||
|
||||
defaultConfig {
|
||||
minSdk = 21
|
||||
|
||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||
consumerProguardFiles("consumer-rules.pro")
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
isMinifyEnabled = false
|
||||
proguardFiles(
|
||||
getDefaultProguardFile("proguard-android-optimize.txt"),
|
||||
"proguard-rules.pro"
|
||||
)
|
||||
}
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility = JavaVersion.VERSION_1_8
|
||||
targetCompatibility = JavaVersion.VERSION_1_8
|
||||
}
|
||||
kotlinOptions {
|
||||
jvmTarget = "1.8"
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
||||
implementation("androidx.core:core-ktx:1.9.0")
|
||||
implementation("androidx.appcompat:appcompat:1.6.0")
|
||||
implementation("com.google.android.material:material:1.7.0")
|
||||
testImplementation("junit:junit:4.13.2")
|
||||
androidTestImplementation("androidx.test.ext:junit:1.1.5")
|
||||
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
|
||||
implementation(project(":tauri-android"))
|
||||
|
||||
implementation("androidx.credentials:credentials:1.7.0-alpha03")
|
||||
implementation("androidx.credentials:credentials-play-services-auth:1.7.0-alpha03")
|
||||
implementation("com.google.android.libraries.identity.googleid:googleid:<latest version>")
|
||||
}
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
# Add project specific ProGuard rules here.
|
||||
# You can control the set of applied configuration files using the
|
||||
# proguardFiles setting in build.gradle.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
|
||||
# Uncomment this to preserve the line number information for
|
||||
# debugging stack traces.
|
||||
#-keepattributes SourceFile,LineNumberTable
|
||||
|
||||
# If you keep the line number information, uncomment this to
|
||||
# hide the original source file name.
|
||||
#-renamesourcefileattribute SourceFile
|
||||
@@ -0,0 +1,31 @@
|
||||
pluginManagement {
|
||||
repositories {
|
||||
mavenCentral()
|
||||
gradlePluginPortal()
|
||||
google()
|
||||
}
|
||||
resolutionStrategy {
|
||||
eachPlugin {
|
||||
switch (requested.id.id) {
|
||||
case "com.android.library":
|
||||
useVersion("8.0.2")
|
||||
break
|
||||
case "org.jetbrains.kotlin.android":
|
||||
useVersion("1.8.20")
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dependencyResolutionManagement {
|
||||
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
|
||||
repositories {
|
||||
mavenCentral()
|
||||
google()
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
include ':tauri-android'
|
||||
project(':tauri-android').projectDir = new File('./.tauri/tauri-api')
|
||||
@@ -0,0 +1,3 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
</manifest>
|
||||
@@ -0,0 +1,157 @@
|
||||
package com.plugin.auth
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.util.Base64
|
||||
import androidx.credentials.ClearCredentialStateRequest
|
||||
import androidx.credentials.CredentialManager
|
||||
import androidx.credentials.CustomCredential
|
||||
import androidx.credentials.GetCredentialRequest
|
||||
import androidx.credentials.GetCredentialResponse
|
||||
import androidx.credentials.exceptions.ClearCredentialException
|
||||
import androidx.credentials.exceptions.GetCredentialException
|
||||
import androidx.credentials.exceptions.NoCredentialException
|
||||
import com.google.android.libraries.identity.googleid.GetGoogleIdOption
|
||||
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential
|
||||
import com.google.android.libraries.identity.googleid.GoogleIdTokenParsingException
|
||||
import app.tauri.annotation.Command
|
||||
import app.tauri.annotation.InvokeArg
|
||||
import app.tauri.annotation.TauriPlugin
|
||||
import app.tauri.plugin.Invoke
|
||||
import app.tauri.plugin.JSObject
|
||||
import app.tauri.plugin.Plugin
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import java.security.SecureRandom
|
||||
|
||||
|
||||
@InvokeArg
|
||||
class KirimArgs {
|
||||
var idKlien: String? = null
|
||||
}
|
||||
|
||||
@TauriPlugin
|
||||
class BluclasPlugin(private val activity: Activity) : Plugin(activity) {
|
||||
private val credentialManager by lazy { CredentialManager.create(activity) }
|
||||
private val scope = CoroutineScope(Dispatchers.Main)
|
||||
private val idKlien: String? = null
|
||||
|
||||
@Command
|
||||
fun masuk(invoke: Invoke) {
|
||||
val args = invoke.parseArgs(KirimArgs::class.java)
|
||||
idKlien = args.idKlien
|
||||
if (idKlien.isNullOrEmpty()) {
|
||||
invoke.reject("ID Klien belum disediakan")
|
||||
return
|
||||
}
|
||||
|
||||
val googleIdOption: GetGoogleIdOption = GetGoogleIdOption.Builder()
|
||||
.setFilterByAuthorizedAccounts(true)
|
||||
.setServerClientId(idKlien)
|
||||
.setNonce(generateSecureRandomNonce())
|
||||
.build()
|
||||
|
||||
val request = GetCredentialRequest.Builder()
|
||||
.addCredentialOption(googleIdOption)
|
||||
.build()
|
||||
|
||||
scope.launch {
|
||||
try {
|
||||
val result = credentialManager.getCredential(
|
||||
request = request,
|
||||
context = activity
|
||||
)
|
||||
handleSignInResult(result, invoke)
|
||||
} catch (e: NoCredentialException) {
|
||||
if (args.filterByAuthorizedAccounts) {
|
||||
signInWithAllAccounts(idKlien, invoke)
|
||||
} else {
|
||||
invoke.reject("Tidak ada kredensial Google yang tersedia: ${e.message}")
|
||||
}
|
||||
} catch (e: GetCredentialException) {
|
||||
invoke.reject("Gagal masuk: ${e.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Command
|
||||
fun keluar(invoke: Invoke) {
|
||||
scope.launch {
|
||||
try {
|
||||
credentialManager.clearCredentialState(ClearCredentialStateRequest())
|
||||
invoke.resolve()
|
||||
} catch (e: ClearCredentialException) {
|
||||
invoke.reject("Sign-out failed: ${e.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun signInWithAllAccounts(serverClientId: String, invoke: Invoke) {
|
||||
val googleIdOption = GetGoogleIdOption.Builder()
|
||||
.setFilterByAuthorizedAccounts(false)
|
||||
.setServerClientId(serverClientId)
|
||||
.setNonce(generateSecureRandomNonce())
|
||||
.build()
|
||||
|
||||
val request = GetCredentialRequest.Builder()
|
||||
.addCredentialOption(googleIdOption)
|
||||
.build()
|
||||
|
||||
try {
|
||||
val result = credentialManager.getCredential(
|
||||
request = request,
|
||||
context = activity
|
||||
)
|
||||
handleSignInResult(result, invoke)
|
||||
} catch (e: GetCredentialException) {
|
||||
invoke.reject("Sign-in failed: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
private fun generateSecureRandomNonce(byteLength: Int = 32): String {
|
||||
val randomBytes = ByteArray(byteLength)
|
||||
SecureRandom().nextBytes(randomBytes)
|
||||
return Base64.encodeToString(randomBytes, Base64.NO_WRAP or Base64.URL_SAFE or Base64.NO_PADDING)
|
||||
}
|
||||
|
||||
private fun handleSignInResult(result: GetCredentialResponse, invoke: Invoke) {
|
||||
val credential = result.credential
|
||||
|
||||
when (credential) {
|
||||
is CustomCredential -> {
|
||||
if (credential.type == GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL) {
|
||||
try {
|
||||
val googleIdTokenCredential =
|
||||
GoogleIdTokenCredential.createFrom(credential.data)
|
||||
|
||||
// NOTE: do not treat the user as signed in on the client
|
||||
// alone — send idToken to your backend and verify it there
|
||||
// before trusting it.
|
||||
val ret = JSObject()
|
||||
ret.put("idToken", googleIdTokenCredential.idToken)
|
||||
ret.put("id", googleIdTokenCredential.id)
|
||||
ret.put("displayName", googleIdTokenCredential.displayName)
|
||||
ret.put("givenName", googleIdTokenCredential.givenName)
|
||||
ret.put("familyName", googleIdTokenCredential.familyName)
|
||||
ret.put(
|
||||
"profilePictureUri",
|
||||
googleIdTokenCredential.profilePictureUri?.toString()
|
||||
)
|
||||
ret.put("phoneNumber", googleIdTokenCredential.phoneNumber)
|
||||
|
||||
invoke.resolve(ret)
|
||||
} catch (e: GoogleIdTokenParsingException) {
|
||||
invoke.reject("Invalid Google ID token response: ${e.message}")
|
||||
}
|
||||
} else {
|
||||
invoke.reject("Unexpected credential type: ${credential.type}")
|
||||
}
|
||||
}
|
||||
|
||||
else -> {
|
||||
invoke.reject("Unexpected credential type")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user