diff --git a/src/main/kotlin/dev/usbharu/hideout/Application.kt b/src/main/kotlin/dev/usbharu/hideout/Application.kt index 10f8596b..b6e8d924 100644 --- a/src/main/kotlin/dev/usbharu/hideout/Application.kt +++ b/src/main/kotlin/dev/usbharu/hideout/Application.kt @@ -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 { TwitterSnowflakeIdGenerateService } single { MetaRepositoryImpl(get()) } single { ServerInitialiseServiceImpl(get()) } - single { JwtRefreshTokenRepositoryImpl(get(),get()) } + single { JwtRefreshTokenRepositoryImpl(get(), get()) } single { MetaServiceImpl(get()) } - single { JwtServiceImpl(get(),get(),get()) } + single { JwtServiceImpl(get(), get(), get()) } } configureKoin(module) runBlocking { diff --git a/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/dto/JwtToken.kt b/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/dto/JwtToken.kt index fe25b3b0..9c232f82 100644 --- a/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/dto/JwtToken.kt +++ b/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/dto/JwtToken.kt @@ -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) diff --git a/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/entity/Meta.kt b/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/entity/Meta.kt index f1fa6d5f..770b04d1 100644 --- a/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/entity/Meta.kt +++ b/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/entity/Meta.kt @@ -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) diff --git a/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/form/RefreshToken.kt b/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/form/RefreshToken.kt index 3d35cf21..5d7898a1 100644 --- a/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/form/RefreshToken.kt +++ b/src/main/kotlin/dev/usbharu/hideout/domain/model/hideout/form/RefreshToken.kt @@ -1,3 +1,3 @@ package dev.usbharu.hideout.domain.model.hideout.form -data class RefreshToken(val refreshToken:String) +data class RefreshToken(val refreshToken: String) diff --git a/src/main/kotlin/dev/usbharu/hideout/exception/InvalidRefreshTokenException.kt b/src/main/kotlin/dev/usbharu/hideout/exception/InvalidRefreshTokenException.kt index e9b780d5..08c8ab7d 100644 --- a/src/main/kotlin/dev/usbharu/hideout/exception/InvalidRefreshTokenException.kt +++ b/src/main/kotlin/dev/usbharu/hideout/exception/InvalidRefreshTokenException.kt @@ -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) diff --git a/src/main/kotlin/dev/usbharu/hideout/plugins/Security.kt b/src/main/kotlin/dev/usbharu/hideout/plugins/Security.kt index 468419c2..42210def 100644 --- a/src/main/kotlin/dev/usbharu/hideout/plugins/Security.kt +++ b/src/main/kotlin/dev/usbharu/hideout/plugins/Security.kt @@ -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()) { diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/IJwtRefreshTokenRepository.kt b/src/main/kotlin/dev/usbharu/hideout/repository/IJwtRefreshTokenRepository.kt index 851fa5bc..9e6a3c96 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/IJwtRefreshTokenRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/IJwtRefreshTokenRepository.kt @@ -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() } diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/IMetaRepository.kt b/src/main/kotlin/dev/usbharu/hideout/repository/IMetaRepository.kt index b3ed940f..b90be212 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/IMetaRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/IMetaRepository.kt @@ -6,5 +6,5 @@ interface IMetaRepository { suspend fun save(meta: Meta) - suspend fun get():Meta? + suspend fun get(): Meta? } diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/JwtRefreshTokenRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/JwtRefreshTokenRepositoryImpl.kt index 8796c9fc..74bf019c 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/JwtRefreshTokenRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/JwtRefreshTokenRepositoryImpl.kt @@ -22,6 +22,7 @@ class JwtRefreshTokenRepositoryImpl( } } + @Suppress("InjectDispatcher") suspend fun query(block: suspend () -> T): T = newSuspendedTransaction(Dispatchers.IO) { block() } diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/MetaRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/MetaRepositoryImpl.kt index 5a537ae7..f58e555e 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/MetaRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/MetaRepositoryImpl.kt @@ -16,6 +16,7 @@ class MetaRepositoryImpl(private val database: Database) : IMetaRepository { } } + @Suppress("InjectDispatcher") suspend fun 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() diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt index 237acb6b..669a0df7 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/PostRepositoryImpl.kt @@ -20,6 +20,7 @@ class PostRepositoryImpl(database: Database, private val idGenerateService: IdGe } } + @Suppress("InjectDispatcher") suspend fun query(block: suspend () -> T): T = newSuspendedTransaction(Dispatchers.IO) { block() } diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/UserRepository.kt b/src/main/kotlin/dev/usbharu/hideout/repository/UserRepository.kt index 2f2905d8..8dc92ba0 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/UserRepository.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/UserRepository.kt @@ -21,6 +21,7 @@ class UserRepository(private val database: Database, private val idGenerateServi } } + @Suppress("InjectDispatcher") suspend fun query(block: suspend () -> T): T = newSuspendedTransaction(Dispatchers.IO) { block() } diff --git a/src/main/kotlin/dev/usbharu/hideout/routing/AuthTestRouting.kt b/src/main/kotlin/dev/usbharu/hideout/routing/AuthTestRouting.kt index 79946c46..0667a7a7 100644 --- a/src/main/kotlin/dev/usbharu/hideout/routing/AuthTestRouting.kt +++ b/src/main/kotlin/dev/usbharu/hideout/routing/AuthTestRouting.kt @@ -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() val username = principal!!.payload.getClaim("username") call.respondText("Hello $username") diff --git a/src/main/kotlin/dev/usbharu/hideout/routing/LoginRouting.kt b/src/main/kotlin/dev/usbharu/hideout/routing/LoginRouting.kt index e0db266b..0a8b9a26 100644 --- a/src/main/kotlin/dev/usbharu/hideout/routing/LoginRouting.kt +++ b/src/main/kotlin/dev/usbharu/hideout/routing/LoginRouting.kt @@ -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) { } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/IJwtService.kt b/src/main/kotlin/dev/usbharu/hideout/service/IJwtService.kt index 0eece5a7..f722517f 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/IJwtService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/IJwtService.kt @@ -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() } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/JwtServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/JwtServiceImpl.kt index 5b036522..116b6088 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/JwtServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/JwtServiceImpl.kt @@ -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() diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ServerInitialiseServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/ServerInitialiseServiceImpl.kt index f18a090a..35f53843 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ServerInitialiseServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ServerInitialiseServiceImpl.kt @@ -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 { 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 18811258..767a6bdf 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubUserServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/activitypub/ActivityPubUserServiceImpl.kt @@ -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 { diff --git a/src/main/kotlin/dev/usbharu/hideout/util/Base64Util.kt b/src/main/kotlin/dev/usbharu/hideout/util/Base64Util.kt index 07ae9284..451d4ade 100644 --- a/src/main/kotlin/dev/usbharu/hideout/util/Base64Util.kt +++ b/src/main/kotlin/dev/usbharu/hideout/util/Base64Util.kt @@ -6,5 +6,4 @@ object Base64Util { fun decode(str: String): ByteArray = Base64.getDecoder().decode(str) fun encode(bytes: ByteArray): String = Base64.getEncoder().encodeToString(bytes) - } diff --git a/src/main/kotlin/dev/usbharu/hideout/util/JsonWebKeyUtil.kt b/src/main/kotlin/dev/usbharu/hideout/util/JsonWebKeyUtil.kt index 4b73a8f5..c733388e 100644 --- a/src/main/kotlin/dev/usbharu/hideout/util/JsonWebKeyUtil.kt +++ b/src/main/kotlin/dev/usbharu/hideout/util/JsonWebKeyUtil.kt @@ -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 diff --git a/src/main/kotlin/dev/usbharu/hideout/util/RsaUtil.kt b/src/main/kotlin/dev/usbharu/hideout/util/RsaUtil.kt index db912596..e0ebbfc8 100644 --- a/src/main/kotlin/dev/usbharu/hideout/util/RsaUtil.kt +++ b/src/main/kotlin/dev/usbharu/hideout/util/RsaUtil.kt @@ -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)) } diff --git a/src/main/kotlin/dev/usbharu/hideout/util/ServerUtil.kt b/src/main/kotlin/dev/usbharu/hideout/util/ServerUtil.kt index 438f7e33..e8264487 100644 --- a/src/main/kotlin/dev/usbharu/hideout/util/ServerUtil.kt +++ b/src/main/kotlin/dev/usbharu/hideout/util/ServerUtil.kt @@ -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" } diff --git a/src/main/kotlin/dev/usbharu/kjob/exposed/ExposedJobRepository.kt b/src/main/kotlin/dev/usbharu/kjob/exposed/ExposedJobRepository.kt index a9f5e5bb..c2ba778e 100644 --- a/src/main/kotlin/dev/usbharu/kjob/exposed/ExposedJobRepository.kt +++ b/src/main/kotlin/dev/usbharu/kjob/exposed/ExposedJobRepository.kt @@ -54,6 +54,7 @@ class ExposedJobRepository( } } + @Suppress("InjectDispatcher") suspend fun 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 } diff --git a/src/main/kotlin/dev/usbharu/kjob/exposed/ExposedLockRepository.kt b/src/main/kotlin/dev/usbharu/kjob/exposed/ExposedLockRepository.kt index 9ed2146f..e29341ba 100644 --- a/src/main/kotlin/dev/usbharu/kjob/exposed/ExposedLockRepository.kt +++ b/src/main/kotlin/dev/usbharu/kjob/exposed/ExposedLockRepository.kt @@ -33,6 +33,7 @@ class ExposedLockRepository( } } + @Suppress("InjectDispatcher") suspend fun query(block: suspend () -> T): T = newSuspendedTransaction(Dispatchers.IO) { block() } override suspend fun exists(id: UUID): Boolean {