feat: 公開鍵を追加

This commit is contained in:
usbharu 2023-03-24 23:29:53 +09:00
parent 62eb6ddcce
commit 28fa2a4ecf
6 changed files with 66 additions and 21 deletions

View File

@ -48,7 +48,7 @@ fun Application.module() {
single<IUserAuthRepository> { UserAuthRepository(get()) } single<IUserAuthRepository> { UserAuthRepository(get()) }
single<IUserAuthService> { UserAuthService(get(), get()) } single<IUserAuthService> { UserAuthService(get(), get()) }
single<UserService> { UserService(get()) } single<UserService> { UserService(get()) }
single<ActivityPubUserService> { ActivityPubUserService(get()) } single<ActivityPubUserService> { ActivityPubUserService(get(),get()) }
} }
configureKoin(module) configureKoin(module)
val configData by inject<ConfigData>() val configData by inject<ConfigData>()

View File

@ -0,0 +1,21 @@
package dev.usbharu.hideout.ap
open class Key : Object{
private var id:String? = null
private var owner:String? = null
private var publicKeyPem:String? = null
protected constructor() : super()
constructor(
type: List<String>,
name: String,
id: String?,
owner: String?,
publicKeyPem: String?
) : super(add(type,"Key"), name) {
this.id = id
this.owner = owner
this.publicKeyPem = publicKeyPem
}
}

View File

@ -8,6 +8,7 @@ open class Person : Object {
private var outbox:String? = null private var outbox:String? = null
private var url:String? = null private var url:String? = null
private var icon:Image? = null private var icon:Image? = null
private var publicKey:Key? = null
protected constructor() : super() protected constructor() : super()
constructor( constructor(
type: List<String> = emptyList(), type: List<String> = emptyList(),
@ -18,7 +19,8 @@ open class Person : Object {
inbox: String?, inbox: String?,
outbox: String?, outbox: String?,
url: String?, url: String?,
icon: Image? icon: Image?,
publicKey: Key?
) : super(add(type,"Person"), name) { ) : super(add(type,"Person"), name) {
this.id = id this.id = id
this.preferredUsername = preferredUsername this.preferredUsername = preferredUsername
@ -27,6 +29,7 @@ open class Person : Object {
this.outbox = outbox this.outbox = outbox
this.url = url this.url = url
this.icon = icon this.icon = icon
this.publicKey = publicKey
} }
} }

View File

@ -1,27 +1,39 @@
package dev.usbharu.hideout.service package dev.usbharu.hideout.service
import dev.usbharu.hideout.ap.Image import dev.usbharu.hideout.ap.Image
import dev.usbharu.hideout.ap.Key
import dev.usbharu.hideout.ap.Person import dev.usbharu.hideout.ap.Person
import dev.usbharu.hideout.config.Config import dev.usbharu.hideout.config.Config
class ActivityPubUserService(private val userService: UserService) { class ActivityPubUserService(
suspend fun generateUserModel(name:String):Person{ private val userService: UserService,
private val userAuthService: IUserAuthService
) {
suspend fun generateUserModel(name: String): Person {
val userEntity = userService.findByName(name) val userEntity = userService.findByName(name)
val userAuthEntity = userAuthService.findByUserId(userEntity.id)
val userUrl = "${Config.configData.hostname}/users/$name" val userUrl = "${Config.configData.hostname}/users/$name"
return Person( return Person(
emptyList(), type = emptyList(),
userEntity.name, name = userEntity.name,
userUrl, id = userUrl,
name, preferredUsername = name,
userEntity.description, summary = userEntity.description,
"$userUrl/inbox", inbox = "$userUrl/inbox",
"$userUrl/outbox", outbox = "$userUrl/outbox",
userUrl, url = userUrl,
Image( icon = Image(
emptyList(), type = emptyList(),
"$userUrl/icon.png", name = "$userUrl/icon.png",
"image/png", mediaType = "image/png",
"$userUrl/icon.png" url = "$userUrl/icon.png"
),
publicKey = Key(
type = emptyList(),
name = "Public Key",
id = "$userUrl/pubkey",
owner = userUrl,
publicKeyPem = userAuthEntity.publicKey
) )
) )
} }

View File

@ -1,5 +1,7 @@
package dev.usbharu.hideout.service package dev.usbharu.hideout.service
import dev.usbharu.hideout.domain.model.UserAuthenticationEntity
interface IUserAuthService { interface IUserAuthService {
fun hash(password:String): String fun hash(password:String): String
@ -7,4 +9,6 @@ interface IUserAuthService {
suspend fun registerAccount(username: String, hash: String) suspend fun registerAccount(username: String, hash: String)
suspend fun verifyAccount(username: String,password: String): Boolean suspend fun verifyAccount(username: String,password: String): Boolean
suspend fun findByUserId(userId: Long):UserAuthenticationEntity
} }

View File

@ -2,6 +2,7 @@ package dev.usbharu.hideout.service
import dev.usbharu.hideout.domain.model.User import dev.usbharu.hideout.domain.model.User
import dev.usbharu.hideout.domain.model.UserAuthentication import dev.usbharu.hideout.domain.model.UserAuthentication
import dev.usbharu.hideout.domain.model.UserAuthenticationEntity
import dev.usbharu.hideout.exception.UserNotFoundException import dev.usbharu.hideout.exception.UserNotFoundException
import dev.usbharu.hideout.repository.IUserAuthRepository import dev.usbharu.hideout.repository.IUserAuthRepository
import dev.usbharu.hideout.repository.IUserRepository import dev.usbharu.hideout.repository.IUserRepository
@ -61,6 +62,10 @@ class UserAuthService(
return userAuthEntity.hash == hash(password) return userAuthEntity.hash == hash(password)
} }
override suspend fun findByUserId(userId: Long): UserAuthenticationEntity {
return userAuthRepository.findByUserId(userId) ?: throw UserNotFoundException("$userId was not found")
}
private fun generateKeyPair(): KeyPair { private fun generateKeyPair(): KeyPair {
val keyPairGenerator = KeyPairGenerator.getInstance("RSA") val keyPairGenerator = KeyPairGenerator.getInstance("RSA")
keyPairGenerator.initialize(1024) keyPairGenerator.initialize(1024)
@ -69,14 +74,14 @@ class UserAuthService(
private fun RSAPublicKey.toPem(): String { private fun RSAPublicKey.toPem(): String {
return "-----BEGIN RSA PUBLIC KEY-----\n" + return "-----BEGIN RSA PUBLIC KEY-----" +
Base64.getEncoder().encodeToString(encoded) + "\n" + Base64.getEncoder().encodeToString(encoded) +
"-----END RSA PUBLIC KEY-----" "-----END RSA PUBLIC KEY-----"
} }
private fun RSAPrivateKey.toPem(): String { private fun RSAPrivateKey.toPem(): String {
return "-----BEGIN RSA PRIVATE KEY-----\n" + return "-----BEGIN RSA PRIVATE KEY-----" +
Base64.getEncoder().encodeToString(encoded) + "\n" + Base64.getEncoder().encodeToString(encoded) +
"-----END RSA PRIVATE KEY-----" "-----END RSA PRIVATE KEY-----"
} }