Something.

This commit is contained in:
LamGC 2024-01-06 16:27:15 +00:00
parent e4908dfd1e
commit aa84499d62
5 changed files with 65 additions and 7 deletions

View File

@ -17,8 +17,9 @@ repositories {
}
dependencies {
implementation("org.slf4j:slf4j-api:2.0.10")
implementation("net.lamgc:scalabot-extension:0.6.1")
compileOnly("org.slf4j:slf4j-api:2.0.10")
compileOnly("io.github.microutils:kotlin-logging:3.0.5")
compileOnly("net.lamgc:scalabot-extension:0.6.1")
val exposedVersion = "0.45.0"
implementation("org.jetbrains.exposed:exposed-core:$exposedVersion")

View File

@ -3,4 +3,5 @@ package net.lamgc.scext.onedrive_transfer
data class ExtensionConfig(
val clientId: String,
val clientSecret: String,
val useCommandPrefix: Boolean
)

View File

@ -7,6 +7,7 @@ import com.microsoft.aad.msal4j.*
import java.sql.Connection
import java.net.URL
import java.net.URI
import java.util.concurrent.ExecutionException
class MicrosoftAccountManager(private val authClient: ConfidentialClientApplication, private val db: Database) {
@ -51,6 +52,23 @@ class MicrosoftAccountManager(private val authClient: ConfidentialClientApplicat
}
}
fun getAccountByTgUserId(userId: Long): MicrosoftAccount? {
return transaction(db) {
MicrosoftAccount.find { MicrosoftAccounts.telegramUserId eq userId }.firstOrNull()
}
}
fun getAccessToken(account: MicrosoftAccount): IAuthenticationResult? {
val iAccount = authClient.accounts.get().find { it.homeAccountId() == account.accountId }
val silentParameters = SilentParameters.builder(OAUTH2_SCOPE, iAccount).build()
val future = authClient.acquireTokenSilently(silentParameters)
try {
return future.get()
} catch (e: ExecutionException) {
throw e.cause ?: e
}
}
companion object {
const val AUTHORITY = "https://login.microsoftonline.com/common"
val OAUTH2_SCOPE = setOf(

View File

@ -2,7 +2,7 @@ package net.lamgc.scext.onedrive_transfer
import com.google.gson.Gson
import com.microsoft.aad.msal4j.*
import com.microsoft.graph.requests.GraphServiceClient
import mu.KotlinLogging
import org.jetbrains.exposed.sql.Database
import org.telegram.abilitybots.api.bot.BaseAbilityBot
import org.telegram.abilitybots.api.objects.Ability
@ -15,9 +15,12 @@ import java.net.URL
class OneDriveTransferExtension(val bot: BaseAbilityBot, val dataFolder: File) : AbilityExtension {
private val logger = KotlinLogging.logger { }
private val config: ExtensionConfig
private val authClient: ConfidentialClientApplication
private val accountManager: MicrosoftAccountManager
private val onedriveService: OneDriveTransferService
init {
val configFile = File(dataFolder, "config.json")
@ -35,6 +38,7 @@ class OneDriveTransferExtension(val bot: BaseAbilityBot, val dataFolder: File) :
.setTokenCacheAccessAspect(DatabaseTokenCache(db))
.build()
accountManager = MicrosoftAccountManager(authClient, db)
onedriveService = OneDriveTransferService(config, accountManager, authClient)
}
fun loginOneDrive(): Ability = Ability
@ -56,13 +60,41 @@ class OneDriveTransferExtension(val bot: BaseAbilityBot, val dataFolder: File) :
.reply(Reply.of(
{bot, upd ->
val token = MicrosoftAccountManager.getTokenFromUrl(URL(upd.message.text.trim()))
val account = accountManager.updateAccount(upd.message.chat.id, token)
try {
val account = accountManager.updateAccount(upd.message.chat.id, token)
bot.silent().send("""
登录成功
Microsoft 账号${account.userName}
只需要向我发送文件即可中转至 OneDrive
""".trimIndent(), upd.message.chatId)
} catch (e: Exception) {
logger.error(e) { "处理 Oauth2 令牌时发生错误." }
bot.silent().send("处理 Oauth2 令牌时发生错误,请稍后重试。", upd.message.chatId)
}
},
{ upd ->
upd.hasMessage()
if (!upd.hasMessage() || !upd.message.hasText().not()) {
return@of false
}
try {
URL(upd.message.text)
return@of true
} catch (e: Exception) {
return@of false
}
}
))
.build()
fun status(): Ability = Ability.builder()
.name("")
.build()
private fun setCommandName(name: String): String {
if (config.useCommandPrefix) {
return "odt_$name"
}
return name
}
}

View File

@ -1,6 +1,12 @@
package net.lamgc.scext.onedrive_transfer
class OneDriveTransferService() {
import com.microsoft.aad.msal4j.ConfidentialClientApplication
class OneDriveTransferService(
private val config: ExtensionConfig,
private val accountManager: MicrosoftAccountManager,
private val authClient: ConfidentialClientApplication
) {