From 09442d2bdfbee3b8aa792a70504adb3f6fdef9e1 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Mon, 25 Sep 2023 17:27:42 +0900 Subject: [PATCH] =?UTF-8?q?style:=20=E3=82=B9=E3=82=BF=E3=82=A4=E3=83=AB?= =?UTF-8?q?=E3=82=92=E8=AA=BF=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- detekt.yml | 1 + .../dev/usbharu/hideout/JobQueueRunner.kt | 20 +++++----- .../dev/usbharu/hideout/SpringApplication.kt | 1 + .../hideout/config/HttpClientConfig.kt | 4 +- .../usbharu/hideout/config/SecurityConfig.kt | 10 ++--- .../hideout/config/SpringTransactionConfig.kt | 5 +-- .../hideout/controller/InboxController.kt | 9 +++-- .../hideout/controller/OutboxController.kt | 4 +- .../controller/OutboxControllerImpl.kt | 4 +- .../wellknown/HostMetaController.kt | 10 +---- .../wellknown/NodeinfoController.kt | 4 +- .../hideout/domain/model/UserDetailsImpl.kt | 20 +++++----- .../domain/model/wellknown/Nodeinfo.kt | 1 - .../domain/model/wellknown/Nodeinfo2_0.kt | 1 + .../RegisteredClientRepositoryImpl.kt | 33 ++++++++-------- .../hideout/service/ap/APReactionService.kt | 6 ++- .../service/ap/APReceiveFollowService.kt | 4 +- .../service/api/mastodon/AccountApiService.kt | 4 +- .../service/api/mastodon/AppApiService.kt | 4 +- .../api/mastodon/InstanceApiService.kt | 6 ++- .../api/mastodon/StatusesApiService.kt | 17 ++++---- .../auth/ExposedOAuth2AuthorizationService.kt | 13 +++++-- .../service/auth/UserDetailsServiceImpl.kt | 16 ++++---- .../service/job/JobQueueWorkerService.kt | 6 ++- .../service/job/KJobJobQueueWorkerService.kt | 8 ++-- .../service/mastodon/AccountService.kt | 18 ++++----- .../service/user/UserAuthServiceImpl.kt | 4 +- .../dev/usbharu/hideout/util/JsonUtil.kt | 16 -------- .../usbharu/hideout/util/JsonWebKeyUtil.kt | 39 ------------------- 29 files changed, 120 insertions(+), 168 deletions(-) delete mode 100644 src/main/kotlin/dev/usbharu/hideout/util/JsonUtil.kt delete mode 100644 src/main/kotlin/dev/usbharu/hideout/util/JsonWebKeyUtil.kt diff --git a/detekt.yml b/detekt.yml index bf483322..21fab9ff 100644 --- a/detekt.yml +++ b/detekt.yml @@ -2,6 +2,7 @@ build: maxIssues: 20 weights: Indentation: 0 + MagicNumber: 0 style: ClassOrdering: diff --git a/src/main/kotlin/dev/usbharu/hideout/JobQueueRunner.kt b/src/main/kotlin/dev/usbharu/hideout/JobQueueRunner.kt index c8d97610..d1c1b03f 100644 --- a/src/main/kotlin/dev/usbharu/hideout/JobQueueRunner.kt +++ b/src/main/kotlin/dev/usbharu/hideout/JobQueueRunner.kt @@ -30,17 +30,19 @@ class JobQueueWorkerRunner( ) : ApplicationRunner { override fun run(args: ApplicationArguments?) { LOGGER.info("Init job queue worker.") - jobQueueWorkerService.init(jobs.map { - it to { - execute { - LOGGER.debug("excute job ${it.name}") - apService.processActivity( - job = this, - hideoutJob = it - ) + jobQueueWorkerService.init( + jobs.map { + it to { + execute { + LOGGER.debug("excute job ${it.name}") + apService.processActivity( + job = this, + hideoutJob = it + ) + } } } - }) + ) } companion object { diff --git a/src/main/kotlin/dev/usbharu/hideout/SpringApplication.kt b/src/main/kotlin/dev/usbharu/hideout/SpringApplication.kt index 0cc32e48..d050b856 100644 --- a/src/main/kotlin/dev/usbharu/hideout/SpringApplication.kt +++ b/src/main/kotlin/dev/usbharu/hideout/SpringApplication.kt @@ -8,6 +8,7 @@ import org.springframework.boot.runApplication @ConfigurationPropertiesScan class SpringApplication +@Suppress("SpreadOperator") fun main(args: Array) { runApplication(*args) } diff --git a/src/main/kotlin/dev/usbharu/hideout/config/HttpClientConfig.kt b/src/main/kotlin/dev/usbharu/hideout/config/HttpClientConfig.kt index e5d5c56e..2110618d 100644 --- a/src/main/kotlin/dev/usbharu/hideout/config/HttpClientConfig.kt +++ b/src/main/kotlin/dev/usbharu/hideout/config/HttpClientConfig.kt @@ -32,7 +32,9 @@ class HttpClientConfig { applicationConfig: ApplicationConfig ): KtorKeyMap { return KtorKeyMap( - userQueryService, transaction, applicationConfig + userQueryService, + transaction, + applicationConfig ) } } diff --git a/src/main/kotlin/dev/usbharu/hideout/config/SecurityConfig.kt b/src/main/kotlin/dev/usbharu/hideout/config/SecurityConfig.kt index 62dba22a..d4a8d4d1 100644 --- a/src/main/kotlin/dev/usbharu/hideout/config/SecurityConfig.kt +++ b/src/main/kotlin/dev/usbharu/hideout/config/SecurityConfig.kt @@ -55,7 +55,6 @@ class SecurityConfig { return http.build() } - @Bean @Order(2) fun defaultSecurityFilterChain(http: HttpSecurity, introspector: HandlerMappingIntrospector): SecurityFilterChain { @@ -99,9 +98,7 @@ class SecurityConfig { } @Bean - fun passwordEncoder(): PasswordEncoder { - return BCryptPasswordEncoder() - } + fun passwordEncoder(): PasswordEncoder = BCryptPasswordEncoder() @Bean fun genJwkSource(): JWKSource { @@ -131,9 +128,8 @@ class SecurityConfig { } @Bean - fun jwtDecoder(jwkSource: JWKSource): JwtDecoder { - return OAuth2AuthorizationServerConfiguration.jwtDecoder(jwkSource) - } + fun jwtDecoder(jwkSource: JWKSource): JwtDecoder = + OAuth2AuthorizationServerConfiguration.jwtDecoder(jwkSource) @Bean fun authorizationServerSettings(): AuthorizationServerSettings { diff --git a/src/main/kotlin/dev/usbharu/hideout/config/SpringTransactionConfig.kt b/src/main/kotlin/dev/usbharu/hideout/config/SpringTransactionConfig.kt index 92caf4f1..3edcc581 100644 --- a/src/main/kotlin/dev/usbharu/hideout/config/SpringTransactionConfig.kt +++ b/src/main/kotlin/dev/usbharu/hideout/config/SpringTransactionConfig.kt @@ -12,7 +12,6 @@ import javax.sql.DataSource @EnableTransactionManagement class SpringTransactionConfig(val datasource: DataSource) : TransactionManagementConfigurer { @Bean - override fun annotationDrivenTransactionManager(): PlatformTransactionManager { - return SpringTransactionManager(datasource) - } + override fun annotationDrivenTransactionManager(): PlatformTransactionManager = + SpringTransactionManager(datasource) } diff --git a/src/main/kotlin/dev/usbharu/hideout/controller/InboxController.kt b/src/main/kotlin/dev/usbharu/hideout/controller/InboxController.kt index 15d4c77c..35e18206 100644 --- a/src/main/kotlin/dev/usbharu/hideout/controller/InboxController.kt +++ b/src/main/kotlin/dev/usbharu/hideout/controller/InboxController.kt @@ -12,10 +12,11 @@ interface InboxController { @RequestMapping( "/inbox", "/users/{username}/inbox", - produces = ["application/activity+json", "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""], + produces = [ + "application/activity+json", + "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"" + ], method = [RequestMethod.GET, RequestMethod.POST] ) - fun inbox(@RequestBody string: String): ResponseEntity { - return ResponseEntity(HttpStatus.ACCEPTED) - } + fun inbox(@RequestBody string: String): ResponseEntity = ResponseEntity(HttpStatus.ACCEPTED) } diff --git a/src/main/kotlin/dev/usbharu/hideout/controller/OutboxController.kt b/src/main/kotlin/dev/usbharu/hideout/controller/OutboxController.kt index ee003682..ad463860 100644 --- a/src/main/kotlin/dev/usbharu/hideout/controller/OutboxController.kt +++ b/src/main/kotlin/dev/usbharu/hideout/controller/OutboxController.kt @@ -10,7 +10,5 @@ import org.springframework.web.bind.annotation.RestController @RestController interface OutboxController { @RequestMapping("/outbox", "/users/{username}/outbox", method = [RequestMethod.POST, RequestMethod.GET]) - fun outbox(@RequestBody string: String): ResponseEntity { - return ResponseEntity(HttpStatus.ACCEPTED) - } + fun outbox(@RequestBody string: String): ResponseEntity = ResponseEntity(HttpStatus.ACCEPTED) } diff --git a/src/main/kotlin/dev/usbharu/hideout/controller/OutboxControllerImpl.kt b/src/main/kotlin/dev/usbharu/hideout/controller/OutboxControllerImpl.kt index 04dc227f..b6b114f6 100644 --- a/src/main/kotlin/dev/usbharu/hideout/controller/OutboxControllerImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/controller/OutboxControllerImpl.kt @@ -7,7 +7,5 @@ import org.springframework.web.bind.annotation.RestController @RestController class OutboxControllerImpl : OutboxController { - override fun outbox(@RequestBody string: String): ResponseEntity { - return ResponseEntity(HttpStatus.NOT_IMPLEMENTED) - } + override fun outbox(@RequestBody string: String): ResponseEntity = ResponseEntity(HttpStatus.NOT_IMPLEMENTED) } diff --git a/src/main/kotlin/dev/usbharu/hideout/controller/wellknown/HostMetaController.kt b/src/main/kotlin/dev/usbharu/hideout/controller/wellknown/HostMetaController.kt index 1c92dc73..c035eb84 100644 --- a/src/main/kotlin/dev/usbharu/hideout/controller/wellknown/HostMetaController.kt +++ b/src/main/kotlin/dev/usbharu/hideout/controller/wellknown/HostMetaController.kt @@ -29,14 +29,8 @@ class HostMetaController(private val applicationConfig: ApplicationConfig) { }""" @GetMapping("/.well-known/host-meta", produces = ["application/xml"]) - fun hostmeta(): ResponseEntity { - return ResponseEntity(xml, HttpStatus.OK) - } + fun hostmeta(): ResponseEntity = ResponseEntity(xml, HttpStatus.OK) @GetMapping("/.well-known/host-meta.json", produces = ["application/json"]) - fun hostmetJson(): ResponseEntity { - return ResponseEntity(json, HttpStatus.OK) - } - - + fun hostmetJson(): ResponseEntity = ResponseEntity(json, HttpStatus.OK) } diff --git a/src/main/kotlin/dev/usbharu/hideout/controller/wellknown/NodeinfoController.kt b/src/main/kotlin/dev/usbharu/hideout/controller/wellknown/NodeinfoController.kt index a6630fe7..5c645da3 100644 --- a/src/main/kotlin/dev/usbharu/hideout/controller/wellknown/NodeinfoController.kt +++ b/src/main/kotlin/dev/usbharu/hideout/controller/wellknown/NodeinfoController.kt @@ -20,11 +20,13 @@ class NodeinfoController(private val applicationConfig: ApplicationConfig) { "${applicationConfig.url}/nodeinfo/2.0" ) ) - ), HttpStatus.OK + ), + HttpStatus.OK ) } @GetMapping("/nodeinfo/2.0") + @Suppress("FunctionNaming") fun nodeinfo2_0(): ResponseEntity { return ResponseEntity( Nodeinfo2_0( diff --git a/src/main/kotlin/dev/usbharu/hideout/domain/model/UserDetailsImpl.kt b/src/main/kotlin/dev/usbharu/hideout/domain/model/UserDetailsImpl.kt index a94547ba..2a3c808c 100644 --- a/src/main/kotlin/dev/usbharu/hideout/domain/model/UserDetailsImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/domain/model/UserDetailsImpl.kt @@ -42,10 +42,12 @@ class UserDetailsImpl( ) @JsonIgnoreProperties(ignoreUnknown = true) @JsonSubTypes +@Suppress("UnnecessaryAbstractClass") abstract class UserDetailsMixin class UserDetailsDeserializer : JsonDeserializer() { - val SIMPLE_GRANTED_AUTHORITY_SET = object : TypeReference>() {} + + private val SIMPLE_GRANTED_AUTHORITY_SET = object : TypeReference>() {} override fun deserialize(p: JsonParser, ctxt: DeserializationContext): UserDetailsImpl { val mapper = p.codec as ObjectMapper val jsonNode: JsonNode = mapper.readTree(p) @@ -57,14 +59,14 @@ class UserDetailsDeserializer : JsonDeserializer() { val password = jsonNode.readText("password") return UserDetailsImpl( - jsonNode["id"].longValue(), - jsonNode.readText("username"), - password, - true, - true, - true, - true, - authorities.toMutableList(), + id = jsonNode["id"].longValue(), + username = jsonNode.readText("username"), + password = password, + enabled = true, + accountNonExpired = true, + credentialsNonExpired = true, + accountNonLocked = true, + authorities = authorities.toMutableList(), ) } diff --git a/src/main/kotlin/dev/usbharu/hideout/domain/model/wellknown/Nodeinfo.kt b/src/main/kotlin/dev/usbharu/hideout/domain/model/wellknown/Nodeinfo.kt index 46adc8b8..7335d12a 100644 --- a/src/main/kotlin/dev/usbharu/hideout/domain/model/wellknown/Nodeinfo.kt +++ b/src/main/kotlin/dev/usbharu/hideout/domain/model/wellknown/Nodeinfo.kt @@ -1,6 +1,5 @@ package dev.usbharu.hideout.domain.model.wellknown - data class Nodeinfo( val links: List ) { diff --git a/src/main/kotlin/dev/usbharu/hideout/domain/model/wellknown/Nodeinfo2_0.kt b/src/main/kotlin/dev/usbharu/hideout/domain/model/wellknown/Nodeinfo2_0.kt index fbce0298..99548641 100644 --- a/src/main/kotlin/dev/usbharu/hideout/domain/model/wellknown/Nodeinfo2_0.kt +++ b/src/main/kotlin/dev/usbharu/hideout/domain/model/wellknown/Nodeinfo2_0.kt @@ -1,5 +1,6 @@ package dev.usbharu.hideout.domain.model.wellknown +@Suppress("ClassNaming") data class Nodeinfo2_0( val version: String, val software: Software, diff --git a/src/main/kotlin/dev/usbharu/hideout/repository/RegisteredClientRepositoryImpl.kt b/src/main/kotlin/dev/usbharu/hideout/repository/RegisteredClientRepositoryImpl.kt index d03b302e..88aa66c2 100644 --- a/src/main/kotlin/dev/usbharu/hideout/repository/RegisteredClientRepositoryImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/repository/RegisteredClientRepositoryImpl.kt @@ -48,9 +48,9 @@ class RegisteredClientRepositoryImpl(private val database: Database) : Registere it[clientSecretExpiresAt] = registeredClient.clientSecretExpiresAt it[clientName] = registeredClient.clientName it[clientAuthenticationMethods] = - registeredClient.clientAuthenticationMethods.map { it.value }.joinToString(",") + registeredClient.clientAuthenticationMethods.map { method -> method.value }.joinToString(",") it[authorizationGrantTypes] = - registeredClient.authorizationGrantTypes.map { it.value }.joinToString(",") + registeredClient.authorizationGrantTypes.map { type -> type.value }.joinToString(",") it[redirectUris] = registeredClient.redirectUris.joinToString(",") it[postLogoutRedirectUris] = registeredClient.postLogoutRedirectUris.joinToString(",") it[scopes] = registeredClient.scopes.joinToString(",") @@ -100,20 +100,7 @@ class RegisteredClientRepositoryImpl(private val database: Database) : Registere private fun jsonToMap(json: String): Map = objectMapper.readValue(json) - companion object { - val objectMapper: ObjectMapper = ObjectMapper() - val LOGGER = LoggerFactory.getLogger(RegisteredClientRepositoryImpl::class.java) - - init { - - val classLoader = ExposedOAuth2AuthorizationService::class.java.classLoader - val modules = SecurityJackson2Modules.getModules(classLoader) - this.objectMapper.registerModules(JavaTimeModule()) - this.objectMapper.registerModules(modules) - this.objectMapper.registerModules(OAuth2AuthorizationServerJackson2Module()) - } - } - + @Suppress("CyclomaticComplexMethod") fun ResultRow.toRegisteredClient(): SpringRegisteredClient { fun resolveClientAuthenticationMethods(string: String): ClientAuthenticationMethod { return when (string) { @@ -175,6 +162,20 @@ class RegisteredClientRepositoryImpl(private val database: Database) : Registere return builder.build() } + + companion object { + val objectMapper: ObjectMapper = ObjectMapper() + val LOGGER = LoggerFactory.getLogger(RegisteredClientRepositoryImpl::class.java) + + init { + + val classLoader = ExposedOAuth2AuthorizationService::class.java.classLoader + val modules = SecurityJackson2Modules.getModules(classLoader) + this.objectMapper.registerModules(JavaTimeModule()) + this.objectMapper.registerModules(modules) + this.objectMapper.registerModules(OAuth2AuthorizationServerJackson2Module()) + } + } } // org/springframework/security/oauth2/server/authorization/client/oauth2-registered-client-schema.sql diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ap/APReactionService.kt b/src/main/kotlin/dev/usbharu/hideout/service/ap/APReactionService.kt index fc1aea04..c848cb62 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/APReactionService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/APReactionService.kt @@ -83,7 +83,8 @@ class APReactionServiceImpl( `object` = postUrl, id = "${Config.configData.url}/like/note/$id", content = content - ), objectMapper + ), + objectMapper ) } @@ -100,7 +101,8 @@ class APReactionServiceImpl( `object` = like, id = "${Config.configData.url}/undo/note/${like.id}", published = Instant.now() - ), objectMapper + ), + objectMapper ) } } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/ap/APReceiveFollowService.kt b/src/main/kotlin/dev/usbharu/hideout/service/ap/APReceiveFollowService.kt index 261c5fd1..13b95b7d 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/ap/APReceiveFollowService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/ap/APReceiveFollowService.kt @@ -18,7 +18,6 @@ import kjob.core.job.JobProps import org.springframework.beans.factory.annotation.Qualifier import org.springframework.stereotype.Service - interface APReceiveFollowService { suspend fun receiveFollow(follow: Follow): ActivityPubResponse suspend fun receiveFollowJob(props: JobProps) @@ -58,7 +57,8 @@ class APReceiveFollowServiceImpl( name = "Follow", `object` = follow, actor = targetActor - ), objectMapper + ), + objectMapper ) val targetEntity = userQueryService.findByUrl(targetActor) diff --git a/src/main/kotlin/dev/usbharu/hideout/service/api/mastodon/AccountApiService.kt b/src/main/kotlin/dev/usbharu/hideout/service/api/mastodon/AccountApiService.kt index 0a18609f..45df785d 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/api/mastodon/AccountApiService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/api/mastodon/AccountApiService.kt @@ -18,10 +18,10 @@ class AccountApiServiceImpl(private val accountService: AccountService, private AccountApiService { override suspend fun verifyCredentials(userid: Long): CredentialAccount = transaction.transaction { val account = accountService.findById(userid) - of(account) + from(account) } - private fun of(account: Account): CredentialAccount { + private fun from(account: Account): CredentialAccount { return CredentialAccount( id = account.id, username = account.username, diff --git a/src/main/kotlin/dev/usbharu/hideout/service/api/mastodon/AppApiService.kt b/src/main/kotlin/dev/usbharu/hideout/service/api/mastodon/AppApiService.kt index 85dd1be1..9d4f3e15 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/api/mastodon/AppApiService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/api/mastodon/AppApiService.kt @@ -55,7 +55,5 @@ class AppApiServiceImpl( } } - private fun parseScope(string: String): Set { - return string.split(" ").toSet() - } + private fun parseScope(string: String): Set = string.split(" ").toSet() } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/api/mastodon/InstanceApiService.kt b/src/main/kotlin/dev/usbharu/hideout/service/api/mastodon/InstanceApiService.kt index e83ae237..00537012 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/api/mastodon/InstanceApiService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/api/mastodon/InstanceApiService.kt @@ -11,13 +11,15 @@ interface InstanceApiService { @Service class InstanceApiServiceImpl(private val applicationConfig: ApplicationConfig) : InstanceApiService { + @Suppress("LongMethod") override suspend fun v1Instance(): V1Instance { val url = applicationConfig.url return V1Instance( uri = url.host, title = "Hideout Server", shortDescription = "Hideout test server", - description = "This server is operated for testing of Hideout. We are not responsible for any events that occur when associating with this server", + description = "This server is operated for testing of Hideout." + + " We are not responsible for any events that occur when associating with this server", email = "i@usbharu.dev", version = "0.0.1", urls = V1InstanceUrls("wss://${url.host}"), @@ -35,7 +37,7 @@ class InstanceApiServiceImpl(private val applicationConfig: ApplicationConfig) : 23 ), mediaAttachments = V1InstanceConfigurationMediaAttachments( - listOf(), + emptyList(), 0, 0, 0, diff --git a/src/main/kotlin/dev/usbharu/hideout/service/api/mastodon/StatusesApiService.kt b/src/main/kotlin/dev/usbharu/hideout/service/api/mastodon/StatusesApiService.kt index 4f0b8dc2..d058ed13 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/api/mastodon/StatusesApiService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/api/mastodon/StatusesApiService.kt @@ -26,6 +26,7 @@ class StatsesApiServiceImpl( private val userQueryService: UserQueryService ) : StatusesApiService { + @Suppress("LongMethod") override suspend fun postStatus(statusesRequest: StatusesRequest, user: UserDetailsImpl): Status { val visibility = when (statusesRequest.visibility) { StatusesRequest.Visibility.public -> Visibility.PUBLIC @@ -37,12 +38,12 @@ class StatsesApiServiceImpl( val post = postService.createLocal( PostCreateDto( - statusesRequest.status.orEmpty(), - statusesRequest.spoilerText, - visibility, - null, - statusesRequest.inReplyToId?.toLongOrNull(), - user.id + text = statusesRequest.status.orEmpty(), + overview = statusesRequest.spoilerText, + visibility = visibility, + repostId = null, + repolyId = statusesRequest.inReplyToId?.toLongOrNull(), + userId = user.id ) ) val account = accountService.findById(user.id) @@ -58,7 +59,7 @@ class StatsesApiServiceImpl( val replyUser = if (post.replyId != null) { try { userQueryService.findById(postQueryService.findById(post.replyId).userId).id - } catch (e: FailedToGetResourcesException) { + } catch (ignore: FailedToGetResourcesException) { null } } else { @@ -82,7 +83,7 @@ class StatsesApiServiceImpl( favouritesCount = 0, repliesCount = 0, url = post.url, - post.replyId?.toString(), + inReplyToId = post.replyId?.toString(), inReplyToAccountId = replyUser?.toString(), reblog = null, language = null, diff --git a/src/main/kotlin/dev/usbharu/hideout/service/auth/ExposedOAuth2AuthorizationService.kt b/src/main/kotlin/dev/usbharu/hideout/service/auth/ExposedOAuth2AuthorizationService.kt index 65d0b675..13ea2820 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/auth/ExposedOAuth2AuthorizationService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/auth/ExposedOAuth2AuthorizationService.kt @@ -40,6 +40,7 @@ class ExposedOAuth2AuthorizationService( } } + @Suppress("LongMethod", "CyclomaticComplexMethod") override fun save(authorization: OAuth2Authorization?): Unit = runBlocking { requireNotNull(authorization) transaction.transaction { @@ -56,7 +57,8 @@ class ExposedOAuth2AuthorizationService( it[registeredClientId] = authorization.registeredClientId it[principalName] = authorization.principalName it[authorizationGrantType] = authorization.authorizationGrantType.value - it[authorizedScopes] = authorization.authorizedScopes.joinToString(",").takeIf { it.isNotEmpty() } + it[authorizedScopes] = + authorization.authorizedScopes.joinToString(",").takeIf { s -> s.isNotEmpty() } it[attributes] = mapToJson(authorization.attributes) it[state] = authorization.getAttribute(OAuth2ParameterNames.STATE) it[authorizationCodeValue] = authorizationCodeToken?.token?.tokenValue @@ -99,7 +101,8 @@ class ExposedOAuth2AuthorizationService( it[registeredClientId] = authorization.registeredClientId it[principalName] = authorization.principalName it[authorizationGrantType] = authorization.authorizationGrantType.value - it[authorizedScopes] = authorization.authorizedScopes.joinToString(",").takeIf { it.isNotEmpty() } + it[authorizedScopes] = + authorization.authorizedScopes.joinToString(",").takeIf { s -> s.isNotEmpty() } it[attributes] = mapToJson(authorization.attributes) it[state] = authorization.getAttribute(OAuth2ParameterNames.STATE) it[authorizationCodeValue] = authorizationCodeToken?.token?.tokenValue @@ -111,8 +114,9 @@ class ExposedOAuth2AuthorizationService( it[accessTokenIssuedAt] = accessToken?.token?.issuedAt it[accessTokenExpiresAt] = accessToken?.token?.expiresAt it[accessTokenMetadata] = accessToken?.metadata?.let { it1 -> mapToJson(it1) } - it[accessTokenType] = accessToken?.token?.tokenType?.value - it[accessTokenScopes] = accessToken?.token?.scopes?.joinToString(",")?.takeIf { it.isNotEmpty() } + it[accessTokenType] = accessToken?.run { token.tokenType.value } + it[accessTokenScopes] = + accessToken?.run { token.scopes.joinToString(",").takeIf { s -> s.isNotEmpty() } } it[refreshTokenValue] = refreshToken?.token?.tokenValue it[refreshTokenIssuedAt] = refreshToken?.token?.issuedAt it[refreshTokenExpiresAt] = refreshToken?.token?.expiresAt @@ -203,6 +207,7 @@ class ExposedOAuth2AuthorizationService( } } + @Suppress("LongMethod", "CyclomaticComplexMethod") fun ResultRow.toAuthorization(): OAuth2Authorization { val registeredClientId = this[Authorization.registeredClientId] diff --git a/src/main/kotlin/dev/usbharu/hideout/service/auth/UserDetailsServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/auth/UserDetailsServiceImpl.kt index e6a8f6b3..f4573fae 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/auth/UserDetailsServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/auth/UserDetailsServiceImpl.kt @@ -24,14 +24,14 @@ class UserDetailsServiceImpl( transaction.transaction { val findById = userQueryService.findByNameAndDomain(username, applicationConfig.url.host) UserDetailsImpl( - findById.id, - findById.name, - findById.password, - true, - true, - true, - true, - mutableListOf() + id = findById.id, + username = findById.name, + password = findById.password, + enabled = true, + accountNonExpired = true, + credentialsNonExpired = true, + accountNonLocked = true, + authorities = mutableListOf() ) } } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/job/JobQueueWorkerService.kt b/src/main/kotlin/dev/usbharu/hideout/service/job/JobQueueWorkerService.kt index f8bfd523..c7cb2f13 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/job/JobQueueWorkerService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/job/JobQueueWorkerService.kt @@ -1,12 +1,14 @@ package dev.usbharu.hideout.service.job -import dev.usbharu.hideout.domain.model.job.HideoutJob import kjob.core.dsl.KJobFunctions import org.springframework.stereotype.Service +import dev.usbharu.hideout.domain.model.job.HideoutJob as HJ import kjob.core.dsl.JobContextWithProps as JCWP import kjob.core.dsl.JobRegisterContext as JRC @Service interface JobQueueWorkerService { - fun init(defines: List>.(HideoutJob) -> KJobFunctions>>>) + fun init( + defines: List>.(HJ) -> KJobFunctions>>> + ) } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/job/KJobJobQueueWorkerService.kt b/src/main/kotlin/dev/usbharu/hideout/service/job/KJobJobQueueWorkerService.kt index d4714df7..5b011424 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/job/KJobJobQueueWorkerService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/job/KJobJobQueueWorkerService.kt @@ -1,13 +1,13 @@ package dev.usbharu.hideout.service.job -import dev.usbharu.hideout.domain.model.job.HideoutJob import dev.usbharu.kjob.exposed.ExposedKJob -import kjob.core.dsl.JobContextWithProps import kjob.core.dsl.JobRegisterContext import kjob.core.dsl.KJobFunctions import kjob.core.kjob import org.jetbrains.exposed.sql.Database import org.springframework.stereotype.Service +import dev.usbharu.hideout.domain.model.job.HideoutJob as HJ +import kjob.core.dsl.JobContextWithProps as JCWP @Service class KJobJobQueueWorkerService(private val database: Database) : JobQueueWorkerService { @@ -21,7 +21,9 @@ class KJobJobQueueWorkerService(private val database: Database) : JobQueueWorker }.start() } - override fun init(defines: List>.(HideoutJob) -> KJobFunctions>>>) { + override fun init( + defines: List>.(HJ) -> KJobFunctions>>> + ) { defines.forEach { job -> kjob.register(job.first, job.second) } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/mastodon/AccountService.kt b/src/main/kotlin/dev/usbharu/hideout/service/mastodon/AccountService.kt index d3347aca..d4f7d97a 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/mastodon/AccountService.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/mastodon/AccountService.kt @@ -25,15 +25,15 @@ class AccountServiceImpl(private val userQueryService: UserQueryService) : Accou header = findById.url + "/header.jpg", headerStatic = findById.url + "/header.jpg", locked = false, - emptyList(), - emptyList(), - false, - false, - false, - findById.createdAt.toString(), - findById.createdAt.toString(), - 0, - 0, + fields = emptyList(), + emojis = emptyList(), + bot = false, + group = false, + discoverable = false, + createdAt = findById.createdAt.toString(), + lastStatusAt = findById.createdAt.toString(), + statusesCount = 0, + followersCount = 0, ) } } diff --git a/src/main/kotlin/dev/usbharu/hideout/service/user/UserAuthServiceImpl.kt b/src/main/kotlin/dev/usbharu/hideout/service/user/UserAuthServiceImpl.kt index 3c0a2ffe..a52852a1 100644 --- a/src/main/kotlin/dev/usbharu/hideout/service/user/UserAuthServiceImpl.kt +++ b/src/main/kotlin/dev/usbharu/hideout/service/user/UserAuthServiceImpl.kt @@ -12,9 +12,7 @@ class UserAuthServiceImpl( val userQueryService: UserQueryService ) : UserAuthService { - override fun hash(password: String): String { - return BCryptPasswordEncoder().encode(password) - } + override fun hash(password: String): String = BCryptPasswordEncoder().encode(password) override suspend fun usernameAlreadyUse(username: String): Boolean { userQueryService.findByName(username) diff --git a/src/main/kotlin/dev/usbharu/hideout/util/JsonUtil.kt b/src/main/kotlin/dev/usbharu/hideout/util/JsonUtil.kt deleted file mode 100644 index 958d0a90..00000000 --- a/src/main/kotlin/dev/usbharu/hideout/util/JsonUtil.kt +++ /dev/null @@ -1,16 +0,0 @@ -package dev.usbharu.hideout.util - -import com.fasterxml.jackson.databind.ObjectMapper -import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule -import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -import com.fasterxml.jackson.module.kotlin.readValue - -object JsonUtil { - val objectMapper = jacksonObjectMapper().registerModule(JavaTimeModule()) - - fun mapToJson(map: Map<*, *>, objectMapper: ObjectMapper = this.objectMapper): String = - objectMapper.writeValueAsString(map) - - fun jsonToMap(json: String, objectMapper: ObjectMapper = this.objectMapper): Map = - objectMapper.readValue(json) -} diff --git a/src/main/kotlin/dev/usbharu/hideout/util/JsonWebKeyUtil.kt b/src/main/kotlin/dev/usbharu/hideout/util/JsonWebKeyUtil.kt deleted file mode 100644 index c733388e..00000000 --- a/src/main/kotlin/dev/usbharu/hideout/util/JsonWebKeyUtil.kt +++ /dev/null @@ -1,39 +0,0 @@ -package dev.usbharu.hideout.util - -import java.math.BigInteger -import java.security.KeyFactory -import java.security.interfaces.RSAPublicKey -import java.security.spec.X509EncodedKeySpec -import java.util.* - -object JsonWebKeyUtil { - - 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) - } - - 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 { - 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 (minLength != -1) { - if (bytes.size < minLength) { - val array = ByteArray(minLength) - System.arraycopy(bytes, 0, array, minLength - bytes.size, bytes.size) - bytes = array - } - } - return Base64.getUrlEncoder().withoutPadding().encodeToString(bytes) - } -}