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

View File

@ -1,27 +1,39 @@
package dev.usbharu.hideout.service
import dev.usbharu.hideout.ap.Image
import dev.usbharu.hideout.ap.Key
import dev.usbharu.hideout.ap.Person
import dev.usbharu.hideout.config.Config
class ActivityPubUserService(private val userService: UserService) {
suspend fun generateUserModel(name:String):Person{
class ActivityPubUserService(
private val userService: UserService,
private val userAuthService: IUserAuthService
) {
suspend fun generateUserModel(name: String): Person {
val userEntity = userService.findByName(name)
val userAuthEntity = userAuthService.findByUserId(userEntity.id)
val userUrl = "${Config.configData.hostname}/users/$name"
return Person(
emptyList(),
userEntity.name,
userUrl,
name,
userEntity.description,
"$userUrl/inbox",
"$userUrl/outbox",
userUrl,
Image(
emptyList(),
"$userUrl/icon.png",
"image/png",
"$userUrl/icon.png"
type = emptyList(),
name = userEntity.name,
id = userUrl,
preferredUsername = name,
summary = userEntity.description,
inbox = "$userUrl/inbox",
outbox = "$userUrl/outbox",
url = userUrl,
icon = Image(
type = emptyList(),
name = "$userUrl/icon.png",
mediaType = "image/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
import dev.usbharu.hideout.domain.model.UserAuthenticationEntity
interface IUserAuthService {
fun hash(password:String): String
@ -7,4 +9,6 @@ interface IUserAuthService {
suspend fun registerAccount(username: String, hash: String)
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.UserAuthentication
import dev.usbharu.hideout.domain.model.UserAuthenticationEntity
import dev.usbharu.hideout.exception.UserNotFoundException
import dev.usbharu.hideout.repository.IUserAuthRepository
import dev.usbharu.hideout.repository.IUserRepository
@ -61,6 +62,10 @@ class UserAuthService(
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 {
val keyPairGenerator = KeyPairGenerator.getInstance("RSA")
keyPairGenerator.initialize(1024)
@ -69,14 +74,14 @@ class UserAuthService(
private fun RSAPublicKey.toPem(): String {
return "-----BEGIN RSA PUBLIC KEY-----\n" +
Base64.getEncoder().encodeToString(encoded) + "\n" +
return "-----BEGIN RSA PUBLIC KEY-----" +
Base64.getEncoder().encodeToString(encoded) +
"-----END RSA PUBLIC KEY-----"
}
private fun RSAPrivateKey.toPem(): String {
return "-----BEGIN RSA PRIVATE KEY-----\n" +
Base64.getEncoder().encodeToString(encoded) + "\n" +
return "-----BEGIN RSA PRIVATE KEY-----" +
Base64.getEncoder().encodeToString(encoded) +
"-----END RSA PRIVATE KEY-----"
}