From 98178ef5ab19d2cadd864edf793145c67cc08b35 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Wed, 26 Apr 2023 16:52:05 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20UserAuthentication=E3=82=92?= =?UTF-8?q?=E9=9D=9E=E6=8E=A8=E5=A5=A8=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dev/usbharu/hideout/domain/model/User.kt | 27 ++++++++++++++++--- .../domain/model/UserAuthentication.kt | 5 ++-- .../hideout/repository/UserRepository.kt | 23 +++------------- .../activitypub/ActivityPubUserServiceImpl.kt | 6 ++++- .../hideout/service/impl/UserAuthService.kt | 6 ++++- 5 files changed, 41 insertions(+), 26 deletions(-) diff --git a/src/main/kotlin/dev/usbharu/hideout/domain/model/User.kt b/src/main/kotlin/dev/usbharu/hideout/domain/model/User.kt index 7239465e..62c8fa47 100644 --- a/src/main/kotlin/dev/usbharu/hideout/domain/model/User.kt +++ b/src/main/kotlin/dev/usbharu/hideout/domain/model/User.kt @@ -1,16 +1,26 @@ package dev.usbharu.hideout.domain.model +import org.h2.mvstore.type.LongDataType import org.jetbrains.exposed.dao.id.LongIdTable import org.jetbrains.exposed.sql.ResultRow +import org.jetbrains.exposed.sql.Table +import java.time.Instant +import java.time.LocalDateTime +import java.time.ZoneId data class User( + val id:Long, val name: String, val domain: String, val screenName: String, val description: String, + val password:String? = null, val inbox: String, val outbox: String, - val url: String + val url: String, + val publicKey:String, + val privateKey:String? = null, + val createdAt:LocalDateTime ) data class UserEntity( @@ -35,15 +45,21 @@ data class UserEntity( ) } -object Users : LongIdTable("users") { +object Users : Table("users") { + val id = long("id").uniqueIndex() val name = varchar("name", length = 64) val domain = varchar("domain", length = 255) val screenName = varchar("screen_name", length = 64) val description = varchar("description", length = 600) + val password = varchar("password", length = 255).nullable() val inbox = varchar("inbox", length = 255).uniqueIndex() val outbox = varchar("outbox", length = 255).uniqueIndex() val url = varchar("url", length = 255).uniqueIndex() + val publicKey = varchar("public_key", length = 10000) + val privateKey = varchar("private_key", length = 10000) + val createdAt = long("created_at") + override val primaryKey: PrimaryKey = PrimaryKey(id) init { uniqueIndex(name, domain) } @@ -52,12 +68,17 @@ object Users : LongIdTable("users") { fun ResultRow.toUser(): User { return User( + this[Users.id], this[Users.name], this[Users.domain], this[Users.screenName], this[Users.description], + this[Users.password], this[Users.inbox], this[Users.outbox], - this[Users.url] + this[Users.url], + this[Users.publicKey], + this[Users.privateKey], + LocalDateTime.ofInstant(Instant.ofEpochMilli((this[Users.createdAt])), ZoneId.systemDefault()) ) } diff --git a/src/main/kotlin/dev/usbharu/hideout/domain/model/UserAuthentication.kt b/src/main/kotlin/dev/usbharu/hideout/domain/model/UserAuthentication.kt index d1cd3d82..c1f02961 100644 --- a/src/main/kotlin/dev/usbharu/hideout/domain/model/UserAuthentication.kt +++ b/src/main/kotlin/dev/usbharu/hideout/domain/model/UserAuthentication.kt @@ -3,13 +3,14 @@ package dev.usbharu.hideout.domain.model import org.jetbrains.exposed.dao.id.LongIdTable import org.jetbrains.exposed.sql.ReferenceOption +@Deprecated("") data class UserAuthentication( val userId: Long, val hash: String?, val publicKey: String, val privateKey: String? ) - +@Deprecated("") data class UserAuthenticationEntity( val id: Long, val userId: Long, @@ -25,7 +26,7 @@ data class UserAuthenticationEntity( userAuthentication.privateKey ) } - +@Deprecated("") object UsersAuthentication : LongIdTable("users_auth") { val userId = long("user_id").references(Users.id, onUpdate = ReferenceOption.CASCADE) val hash = varchar("hash", length = 64).nullable() diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/UserRepository.kt b/src/main/kotlin/dev/usbharu/hideout/repository/UserRepository.kt index dfaf32aa..c0382755 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/UserRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/UserRepository.kt @@ -1,9 +1,6 @@ package dev.usbharu.hideout.repository -import dev.usbharu.hideout.domain.model.User -import dev.usbharu.hideout.domain.model.UserEntity -import dev.usbharu.hideout.domain.model.Users -import dev.usbharu.hideout.domain.model.UsersFollowers +import dev.usbharu.hideout.domain.model.* import kotlinx.coroutines.Dispatchers import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq @@ -20,21 +17,9 @@ class UserRepository(private val database: Database) : IUserRepository { } } - private fun ResultRow.toUser(): User { - return User( - this[Users.name], - this[Users.domain], - this[Users.screenName], - this[Users.description], - this[Users.inbox], - this[Users.outbox], - this[Users.url] - ) - } - private fun ResultRow.toUserEntity(): UserEntity { return UserEntity( - this[Users.id].value, + this[Users.id], this[Users.name], this[Users.domain], this[Users.screenName], @@ -58,7 +43,7 @@ class UserRepository(private val database: Database) : IUserRepository { it[inbox] = user.inbox it[outbox] = user.outbox it[url] = user.url - }[Users.id].value, user) + }[Users.id], user) } } @@ -143,7 +128,7 @@ class UserRepository(private val database: Database) : IUserRepository { .select { Users.id eq id } .map { UserEntity( - id = it[followers[Users.id]].value, + id = it[followers[Users.id]], name = it[followers[Users.name]], domain = it[followers[Users.domain]], screenName = it[followers[Users.screenName]], diff --git a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubUserServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubUserServiceImpl.kt index 88272bdf..f4dd9fec 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubUserServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubUserServiceImpl.kt @@ -17,6 +17,7 @@ import io.ktor.client.request.* import io.ktor.client.statement.* import io.ktor.http.* import org.slf4j.LoggerFactory +import java.time.LocalDateTime class ActivityPubUserServiceImpl( private val userService: UserService, @@ -91,6 +92,7 @@ class ActivityPubUserServiceImpl( val person = Config.configData.objectMapper.readValue(httpResponse.bodyAsText()) val userEntity = userService.create( User( + id = 0L, name = person.preferredUsername ?: throw IllegalActivityPubObjectException("preferredUsername is null"), domain = url.substringAfter(":").substringBeforeLast("/"), @@ -98,7 +100,9 @@ class ActivityPubUserServiceImpl( description = person.summary ?: "", inbox = person.inbox ?: throw IllegalActivityPubObjectException("inbox is null"), outbox = person.outbox ?: throw IllegalActivityPubObjectException("outbox is null"), - url = url + url = url, + publicKey = "", + createdAt = LocalDateTime.now() ) ) userAuthService.createAccount( diff --git a/src/main/kotlin/dev/usbharu/hideout/service/impl/UserAuthService.kt b/src/main/kotlin/dev/usbharu/hideout/service/impl/UserAuthService.kt index 5542d1ee..5166082a 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/impl/UserAuthService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/impl/UserAuthService.kt @@ -13,6 +13,7 @@ import io.ktor.util.* import java.security.* import java.security.interfaces.RSAPrivateKey import java.security.interfaces.RSAPublicKey +import java.time.LocalDateTime import java.util.* class UserAuthService( @@ -34,13 +35,16 @@ class UserAuthService( override suspend fun registerAccount(username: String, hash: String) { val url = "${Config.configData.url}/users/$username" val registerUser = User( + id = 0L, name = username, domain = Config.configData.domain, screenName = username, description = "", inbox = "$url/inbox", outbox = "$url/outbox", - url = url + url = url, + publicKey = "", + createdAt = LocalDateTime.now(), ) val createdUser = userRepository.create(registerUser)