Something.
This commit is contained in:
parent
e4908dfd1e
commit
aa84499d62
@ -17,8 +17,9 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation("org.slf4j:slf4j-api:2.0.10")
|
compileOnly("org.slf4j:slf4j-api:2.0.10")
|
||||||
implementation("net.lamgc:scalabot-extension:0.6.1")
|
compileOnly("io.github.microutils:kotlin-logging:3.0.5")
|
||||||
|
compileOnly("net.lamgc:scalabot-extension:0.6.1")
|
||||||
|
|
||||||
val exposedVersion = "0.45.0"
|
val exposedVersion = "0.45.0"
|
||||||
implementation("org.jetbrains.exposed:exposed-core:$exposedVersion")
|
implementation("org.jetbrains.exposed:exposed-core:$exposedVersion")
|
||||||
|
@ -3,4 +3,5 @@ package net.lamgc.scext.onedrive_transfer
|
|||||||
data class ExtensionConfig(
|
data class ExtensionConfig(
|
||||||
val clientId: String,
|
val clientId: String,
|
||||||
val clientSecret: String,
|
val clientSecret: String,
|
||||||
|
val useCommandPrefix: Boolean
|
||||||
)
|
)
|
||||||
|
@ -7,6 +7,7 @@ import com.microsoft.aad.msal4j.*
|
|||||||
import java.sql.Connection
|
import java.sql.Connection
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
import java.net.URI
|
import java.net.URI
|
||||||
|
import java.util.concurrent.ExecutionException
|
||||||
|
|
||||||
class MicrosoftAccountManager(private val authClient: ConfidentialClientApplication, private val db: Database) {
|
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 {
|
companion object {
|
||||||
const val AUTHORITY = "https://login.microsoftonline.com/common"
|
const val AUTHORITY = "https://login.microsoftonline.com/common"
|
||||||
val OAUTH2_SCOPE = setOf(
|
val OAUTH2_SCOPE = setOf(
|
||||||
|
@ -2,7 +2,7 @@ package net.lamgc.scext.onedrive_transfer
|
|||||||
|
|
||||||
import com.google.gson.Gson
|
import com.google.gson.Gson
|
||||||
import com.microsoft.aad.msal4j.*
|
import com.microsoft.aad.msal4j.*
|
||||||
import com.microsoft.graph.requests.GraphServiceClient
|
import mu.KotlinLogging
|
||||||
import org.jetbrains.exposed.sql.Database
|
import org.jetbrains.exposed.sql.Database
|
||||||
import org.telegram.abilitybots.api.bot.BaseAbilityBot
|
import org.telegram.abilitybots.api.bot.BaseAbilityBot
|
||||||
import org.telegram.abilitybots.api.objects.Ability
|
import org.telegram.abilitybots.api.objects.Ability
|
||||||
@ -15,9 +15,12 @@ import java.net.URL
|
|||||||
|
|
||||||
class OneDriveTransferExtension(val bot: BaseAbilityBot, val dataFolder: File) : AbilityExtension {
|
class OneDriveTransferExtension(val bot: BaseAbilityBot, val dataFolder: File) : AbilityExtension {
|
||||||
|
|
||||||
|
private val logger = KotlinLogging.logger { }
|
||||||
|
|
||||||
private val config: ExtensionConfig
|
private val config: ExtensionConfig
|
||||||
private val authClient: ConfidentialClientApplication
|
private val authClient: ConfidentialClientApplication
|
||||||
private val accountManager: MicrosoftAccountManager
|
private val accountManager: MicrosoftAccountManager
|
||||||
|
private val onedriveService: OneDriveTransferService
|
||||||
|
|
||||||
init {
|
init {
|
||||||
val configFile = File(dataFolder, "config.json")
|
val configFile = File(dataFolder, "config.json")
|
||||||
@ -35,6 +38,7 @@ class OneDriveTransferExtension(val bot: BaseAbilityBot, val dataFolder: File) :
|
|||||||
.setTokenCacheAccessAspect(DatabaseTokenCache(db))
|
.setTokenCacheAccessAspect(DatabaseTokenCache(db))
|
||||||
.build()
|
.build()
|
||||||
accountManager = MicrosoftAccountManager(authClient, db)
|
accountManager = MicrosoftAccountManager(authClient, db)
|
||||||
|
onedriveService = OneDriveTransferService(config, accountManager, authClient)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun loginOneDrive(): Ability = Ability
|
fun loginOneDrive(): Ability = Ability
|
||||||
@ -56,13 +60,41 @@ class OneDriveTransferExtension(val bot: BaseAbilityBot, val dataFolder: File) :
|
|||||||
.reply(Reply.of(
|
.reply(Reply.of(
|
||||||
{bot, upd ->
|
{bot, upd ->
|
||||||
val token = MicrosoftAccountManager.getTokenFromUrl(URL(upd.message.text.trim()))
|
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 ->
|
||||||
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()
|
.build()
|
||||||
|
|
||||||
|
fun status(): Ability = Ability.builder()
|
||||||
|
.name("")
|
||||||
|
.build()
|
||||||
|
|
||||||
|
private fun setCommandName(name: String): String {
|
||||||
|
if (config.useCommandPrefix) {
|
||||||
|
return "odt_$name"
|
||||||
|
}
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,12 @@
|
|||||||
package net.lamgc.scext.onedrive_transfer
|
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
|
||||||
|
) {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user