mirror of https://github.com/usbharu/Hideout.git
style: スタイルを修正
This commit is contained in:
parent
95e47a0e9b
commit
2f1df0bcfd
|
@ -38,7 +38,7 @@ val Application.property: Application.(propertyName: String) -> String
|
|||
}
|
||||
|
||||
// application.conf references the main function. This annotation prevents the IDE from marking it as unused.
|
||||
@Suppress("unused")
|
||||
@Suppress("unused", "LongMethod")
|
||||
fun Application.parent() {
|
||||
Config.configData = ConfigData(
|
||||
url = property("hideout.url"),
|
||||
|
@ -86,9 +86,9 @@ fun Application.parent() {
|
|||
single<IdGenerateService> { TwitterSnowflakeIdGenerateService }
|
||||
single<IMetaRepository> { MetaRepositoryImpl(get()) }
|
||||
single<IServerInitialiseService> { ServerInitialiseServiceImpl(get()) }
|
||||
single<IJwtRefreshTokenRepository> { JwtRefreshTokenRepositoryImpl(get(),get()) }
|
||||
single<IJwtRefreshTokenRepository> { JwtRefreshTokenRepositoryImpl(get(), get()) }
|
||||
single<IMetaService> { MetaServiceImpl(get()) }
|
||||
single<IJwtService> { JwtServiceImpl(get(),get(),get()) }
|
||||
single<IJwtService> { JwtServiceImpl(get(), get(), get()) }
|
||||
}
|
||||
configureKoin(module)
|
||||
runBlocking {
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
package dev.usbharu.hideout.domain.model.hideout.dto
|
||||
|
||||
data class JwtToken(val token:String,val refreshToken:String)
|
||||
data class JwtToken(val token: String, val refreshToken: String)
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
package dev.usbharu.hideout.domain.model.hideout.entity
|
||||
|
||||
data class Meta(val version:String,val jwt:Jwt)
|
||||
data class Meta(val version: String, val jwt: Jwt)
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
package dev.usbharu.hideout.domain.model.hideout.form
|
||||
|
||||
data class RefreshToken(val refreshToken:String)
|
||||
data class RefreshToken(val refreshToken: String)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package dev.usbharu.hideout.exception
|
||||
|
||||
class InvalidRefreshTokenException : IllegalArgumentException{
|
||||
class InvalidRefreshTokenException : IllegalArgumentException {
|
||||
constructor() : super()
|
||||
constructor(s: String?) : super(s)
|
||||
constructor(message: String?, cause: Throwable?) : super(message, cause)
|
||||
|
|
|
@ -24,6 +24,7 @@ import java.util.concurrent.TimeUnit
|
|||
|
||||
const val TOKEN_AUTH = "jwt-auth"
|
||||
|
||||
@Suppress("MagicNumber")
|
||||
fun Application.configureSecurity(
|
||||
userAuthService: IUserAuthService,
|
||||
metaService: IMetaService,
|
||||
|
@ -39,7 +40,6 @@ fun Application.configureSecurity(
|
|||
jwt(TOKEN_AUTH) {
|
||||
verifier(jwkProvider, issuer) {
|
||||
acceptLeeway(3)
|
||||
|
||||
}
|
||||
validate { jwtCredential ->
|
||||
if (jwtCredential.payload.getClaim("username").asString().isNotEmpty()) {
|
||||
|
|
|
@ -3,18 +3,18 @@ package dev.usbharu.hideout.repository
|
|||
import dev.usbharu.hideout.domain.model.hideout.entity.JwtRefreshToken
|
||||
|
||||
interface IJwtRefreshTokenRepository {
|
||||
suspend fun generateId():Long
|
||||
suspend fun generateId(): Long
|
||||
|
||||
suspend fun save(token: JwtRefreshToken)
|
||||
|
||||
suspend fun findById(id:Long):JwtRefreshToken?
|
||||
suspend fun findByToken(token:String):JwtRefreshToken?
|
||||
suspend fun findByUserId(userId:Long):JwtRefreshToken?
|
||||
suspend fun findById(id: Long): JwtRefreshToken?
|
||||
suspend fun findByToken(token: String): JwtRefreshToken?
|
||||
suspend fun findByUserId(userId: Long): JwtRefreshToken?
|
||||
|
||||
suspend fun delete(token:JwtRefreshToken)
|
||||
suspend fun deleteById(id:Long)
|
||||
suspend fun deleteByToken(token:String)
|
||||
suspend fun deleteByUserId(userId:Long)
|
||||
suspend fun delete(token: JwtRefreshToken)
|
||||
suspend fun deleteById(id: Long)
|
||||
suspend fun deleteByToken(token: String)
|
||||
suspend fun deleteByUserId(userId: Long)
|
||||
|
||||
suspend fun deleteAll()
|
||||
}
|
||||
|
|
|
@ -6,5 +6,5 @@ interface IMetaRepository {
|
|||
|
||||
suspend fun save(meta: Meta)
|
||||
|
||||
suspend fun get():Meta?
|
||||
suspend fun get(): Meta?
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ class JwtRefreshTokenRepositoryImpl(
|
|||
}
|
||||
}
|
||||
|
||||
@Suppress("InjectDispatcher")
|
||||
suspend fun <T> query(block: suspend () -> T): T =
|
||||
newSuspendedTransaction(Dispatchers.IO) { block() }
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@ class MetaRepositoryImpl(private val database: Database) : IMetaRepository {
|
|||
}
|
||||
}
|
||||
|
||||
@Suppress("InjectDispatcher")
|
||||
suspend fun <T> query(block: suspend () -> T): T =
|
||||
newSuspendedTransaction(Dispatchers.IO) { block() }
|
||||
|
||||
|
@ -29,7 +30,7 @@ class MetaRepositoryImpl(private val database: Database) : IMetaRepository {
|
|||
it[this.jwtPrivateKey] = meta.jwt.privateKey
|
||||
it[this.jwtPublicKey] = meta.jwt.publicKey
|
||||
}
|
||||
}else {
|
||||
} else {
|
||||
Meta.update({ Meta.id eq 1 }) {
|
||||
it[this.version] = meta.version
|
||||
it[kid] = UUID.randomUUID().toString()
|
||||
|
|
|
@ -20,6 +20,7 @@ class PostRepositoryImpl(database: Database, private val idGenerateService: IdGe
|
|||
}
|
||||
}
|
||||
|
||||
@Suppress("InjectDispatcher")
|
||||
suspend fun <T> query(block: suspend () -> T): T =
|
||||
newSuspendedTransaction(Dispatchers.IO) { block() }
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@ class UserRepository(private val database: Database, private val idGenerateServi
|
|||
}
|
||||
}
|
||||
|
||||
@Suppress("InjectDispatcher")
|
||||
suspend fun <T> query(block: suspend () -> T): T =
|
||||
newSuspendedTransaction(Dispatchers.IO) { block() }
|
||||
|
||||
|
|
|
@ -7,10 +7,9 @@ import io.ktor.server.auth.jwt.*
|
|||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
|
||||
|
||||
fun Routing.authTestRouting(){
|
||||
authenticate(TOKEN_AUTH){
|
||||
get("/auth-check"){
|
||||
fun Routing.authTestRouting() {
|
||||
authenticate(TOKEN_AUTH) {
|
||||
get("/auth-check") {
|
||||
val principal = call.principal<JWTPrincipal>()
|
||||
val username = principal!!.payload.getClaim("username")
|
||||
call.respondText("Hello $username")
|
||||
|
|
|
@ -3,6 +3,5 @@ package dev.usbharu.hideout.routing
|
|||
import dev.usbharu.hideout.service.IUserAuthService
|
||||
import io.ktor.server.routing.*
|
||||
|
||||
fun Routing.login(userAuthService: IUserAuthService){
|
||||
|
||||
fun Routing.login(userAuthService: IUserAuthService) {
|
||||
}
|
||||
|
|
|
@ -5,10 +5,10 @@ import dev.usbharu.hideout.domain.model.hideout.entity.User
|
|||
import dev.usbharu.hideout.domain.model.hideout.form.RefreshToken
|
||||
|
||||
interface IJwtService {
|
||||
suspend fun createToken(user:User):JwtToken
|
||||
suspend fun refreshToken(refreshToken: RefreshToken):JwtToken
|
||||
suspend fun createToken(user: User): JwtToken
|
||||
suspend fun refreshToken(refreshToken: RefreshToken): JwtToken
|
||||
|
||||
suspend fun revokeToken(refreshToken: RefreshToken)
|
||||
suspend fun revokeToken(user:User)
|
||||
suspend fun revokeToken(user: User)
|
||||
suspend fun revokeAll()
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ import java.time.Instant
|
|||
import java.time.temporal.ChronoUnit
|
||||
import java.util.*
|
||||
|
||||
@Suppress("InjectDispatcher")
|
||||
class JwtServiceImpl(
|
||||
private val metaService: IMetaService,
|
||||
private val refreshTokenRepository: IJwtRefreshTokenRepository,
|
||||
|
@ -42,6 +43,7 @@ class JwtServiceImpl(
|
|||
}
|
||||
}
|
||||
|
||||
@Suppress("MagicNumber")
|
||||
override suspend fun createToken(user: User): JwtToken {
|
||||
val now = Instant.now()
|
||||
val token = JWT.create()
|
||||
|
|
|
@ -4,16 +4,16 @@ import dev.usbharu.hideout.domain.model.hideout.entity.Jwt
|
|||
import dev.usbharu.hideout.domain.model.hideout.entity.Meta
|
||||
import dev.usbharu.hideout.repository.IMetaRepository
|
||||
import dev.usbharu.hideout.util.ServerUtil
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.security.KeyPairGenerator
|
||||
import java.util.*
|
||||
|
||||
class ServerInitialiseServiceImpl(private val metaRepository: IMetaRepository) : IServerInitialiseService {
|
||||
|
||||
val logger = LoggerFactory.getLogger(ServerInitialiseServiceImpl::class.java)
|
||||
val logger: Logger = LoggerFactory.getLogger(ServerInitialiseServiceImpl::class.java)
|
||||
|
||||
override suspend fun init() {
|
||||
|
||||
val savedMeta = metaRepository.get()
|
||||
val implementationVersion = ServerUtil.getImplementationVersion()
|
||||
if (wasInitialised(savedMeta).not()) {
|
||||
|
@ -27,7 +27,6 @@ class ServerInitialiseServiceImpl(private val metaRepository: IMetaRepository) :
|
|||
logger.info("Version changed!! (${savedMeta.version} -> $implementationVersion)")
|
||||
updateVersion(savedMeta, implementationVersion)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private fun wasInitialised(meta: Meta?): Boolean {
|
||||
|
|
|
@ -77,7 +77,7 @@ class ActivityPubUserServiceImpl(
|
|||
publicKeyPem = userEntity.publicKey
|
||||
)
|
||||
)
|
||||
} catch (e: UserNotFoundException) {
|
||||
} catch (ignore: UserNotFoundException) {
|
||||
val httpResponse = if (targetActor != null) {
|
||||
httpClient.getAp(url, "$targetActor#pubkey")
|
||||
} else {
|
||||
|
|
|
@ -6,5 +6,4 @@ object Base64Util {
|
|||
fun decode(str: String): ByteArray = Base64.getDecoder().decode(str)
|
||||
|
||||
fun encode(bytes: ByteArray): String = Base64.getEncoder().encodeToString(bytes)
|
||||
|
||||
}
|
||||
|
|
|
@ -8,29 +8,27 @@ import java.util.*
|
|||
|
||||
object JsonWebKeyUtil {
|
||||
|
||||
fun publicKeyToJwk(publicKey: String,kid:String): String {
|
||||
fun publicKeyToJwk(publicKey: String, kid: String): String {
|
||||
val x509EncodedKeySpec = X509EncodedKeySpec(Base64.getDecoder().decode(publicKey))
|
||||
val generatePublic = KeyFactory.getInstance("RSA").generatePublic(x509EncodedKeySpec)
|
||||
return publicKeyToJwk(generatePublic as RSAPublicKey,kid)
|
||||
return publicKeyToJwk(generatePublic as RSAPublicKey, kid)
|
||||
}
|
||||
|
||||
fun publicKeyToJwk(publicKey: RSAPublicKey,kid:String): String {
|
||||
fun publicKeyToJwk(publicKey: RSAPublicKey, kid: String): String {
|
||||
val e = encodeBase64UInt(publicKey.publicExponent)
|
||||
val n = encodeBase64UInt(publicKey.modulus)
|
||||
return """{"keys":[{"e":"$e","n":"$n","use":"sig","kid":"$kid","kty":"RSA"}]}"""
|
||||
}
|
||||
|
||||
private fun encodeBase64UInt(bigInteger: BigInteger, minLength: Int = -1): String {
|
||||
if(bigInteger.signum() < 0){
|
||||
throw IllegalArgumentException("Cannot encode negative numbers")
|
||||
}
|
||||
require(bigInteger.signum() >= 0) { "Cannot encode negative numbers" }
|
||||
|
||||
var bytes = bigInteger.toByteArray()
|
||||
if (bigInteger.bitLength() % 8 == 0 && (bytes[0] == 0.toByte()) && bytes.size > 1){
|
||||
bytes = Arrays.copyOfRange(bytes, 1, bytes.size)
|
||||
if (bigInteger.bitLength() % 8 == 0 && (bytes[0] == 0.toByte()) && bytes.size > 1) {
|
||||
bytes = Arrays.copyOfRange(bytes, 1, bytes.size)
|
||||
}
|
||||
if (minLength != -1){
|
||||
if (bytes.size < minLength){
|
||||
if (minLength != -1) {
|
||||
if (bytes.size < minLength) {
|
||||
val array = ByteArray(minLength)
|
||||
System.arraycopy(bytes, 0, array, minLength - bytes.size, bytes.size)
|
||||
bytes = array
|
||||
|
|
|
@ -7,17 +7,17 @@ import java.security.spec.PKCS8EncodedKeySpec
|
|||
import java.security.spec.X509EncodedKeySpec
|
||||
|
||||
object RsaUtil {
|
||||
fun decodeRsaPublicKey(byteArray: ByteArray):RSAPublicKey{
|
||||
fun decodeRsaPublicKey(byteArray: ByteArray): RSAPublicKey {
|
||||
val x509EncodedKeySpec = X509EncodedKeySpec(byteArray)
|
||||
return KeyFactory.getInstance("RSA").generatePublic(x509EncodedKeySpec) as RSAPublicKey
|
||||
}
|
||||
|
||||
fun decodeRsaPublicKey(encoded: String): RSAPublicKey = decodeRsaPublicKey(Base64Util.decode(encoded))
|
||||
|
||||
fun decodeRsaPrivateKey(byteArray: ByteArray):RSAPrivateKey{
|
||||
fun decodeRsaPrivateKey(byteArray: ByteArray): RSAPrivateKey {
|
||||
val pkcS8EncodedKeySpec = PKCS8EncodedKeySpec(byteArray)
|
||||
return KeyFactory.getInstance("RSA").generatePrivate(pkcS8EncodedKeySpec) as RSAPrivateKey
|
||||
}
|
||||
|
||||
fun decodeRsaPrivateKey(encoded: String):RSAPrivateKey = decodeRsaPrivateKey(Base64Util.decode(encoded))
|
||||
fun decodeRsaPrivateKey(encoded: String): RSAPrivateKey = decodeRsaPrivateKey(Base64Util.decode(encoded))
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package dev.usbharu.hideout.util
|
||||
|
||||
object ServerUtil {
|
||||
fun getImplementationVersion():String = ServerUtil.javaClass.`package`.implementationVersion ?: "DEVELOPMENT-VERSION"
|
||||
fun getImplementationVersion(): String =
|
||||
ServerUtil.javaClass.`package`.implementationVersion ?: "DEVELOPMENT-VERSION"
|
||||
}
|
||||
|
|
|
@ -54,6 +54,7 @@ class ExposedJobRepository(
|
|||
}
|
||||
}
|
||||
|
||||
@Suppress("InjectDispatcher")
|
||||
suspend fun <T> query(block: suspend () -> T): T = newSuspendedTransaction(Dispatchers.IO) { block() }
|
||||
|
||||
override suspend fun completeProgress(id: String): Boolean {
|
||||
|
@ -204,7 +205,7 @@ class ExposedJobRepository(
|
|||
this ?: return emptyMap()
|
||||
return json.parseToJsonElement(this).jsonObject.mapValues { (_, el) ->
|
||||
if (el is JsonObject) {
|
||||
val t = el["t"]?.jsonPrimitive?.content ?: error("Cannot get jsonPrimitive")
|
||||
val t = el["t"]?.run { jsonPrimitive.content } ?: error("Cannot get jsonPrimitive")
|
||||
val value = el["v"]?.jsonArray ?: error("Cannot get jsonArray")
|
||||
when (t) {
|
||||
"s" -> value.map { it.jsonPrimitive.content }
|
||||
|
|
|
@ -33,6 +33,7 @@ class ExposedLockRepository(
|
|||
}
|
||||
}
|
||||
|
||||
@Suppress("InjectDispatcher")
|
||||
suspend fun <T> query(block: suspend () -> T): T = newSuspendedTransaction(Dispatchers.IO) { block() }
|
||||
|
||||
override suspend fun exists(id: UUID): Boolean {
|
||||
|
|
Loading…
Reference in New Issue