feat: reCAPTCHA二対応

This commit is contained in:
usbharu 2024-04-04 15:14:22 +09:00
parent a02c995b6a
commit c579efb110
4 changed files with 68 additions and 1 deletions

View File

@ -0,0 +1,8 @@
package dev.usbharu.hideout.application.config
import org.springframework.boot.context.properties.ConfigurationProperties
@ConfigurationProperties("hideout.security")
data class CaptchaConfig(
val reCaptchaSiteKey:String
)

View File

@ -4,5 +4,5 @@ import dev.usbharu.hideout.core.domain.model.actor.Actor
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetail
interface AuthApiService {
suspend fun registerAccount(registerAccountDto: RegisterAccountDto): Pair<Actor, UserDetail>
suspend fun registerAccount(registerAccountDto: RegisterAccountDto): Actor
}

View File

@ -0,0 +1,50 @@
package dev.usbharu.hideout.core.service.auth
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import dev.usbharu.hideout.application.config.CaptchaConfig
import dev.usbharu.hideout.core.domain.model.actor.Actor
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetail
import dev.usbharu.hideout.core.service.user.UserCreateDto
import dev.usbharu.hideout.core.service.user.UserService
import io.ktor.client.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
@Service
class AuthApiServiceImpl(
private val httpClient: HttpClient,
private val captchaConfig: CaptchaConfig,
private val objectMapper: ObjectMapper,
private val userService: UserService
) :
AuthApiService {
override suspend fun registerAccount(registerAccountDto: RegisterAccountDto): Actor {
val get =
httpClient.get("https://www.google.com/recaptcha/api/siteverify?secret=" + captchaConfig.reCaptchaSiteKey + "&response=" + registerAccountDto.recaptchaResponse)
val recaptchaResult = objectMapper.readValue<RecaptchaResult>(get.bodyAsText())
logger.debug("reCAPTCHA: {}",recaptchaResult)
require(recaptchaResult.success)
require(!(recaptchaResult.score < 0.5))
val createLocalUser = userService.createLocalUser(
UserCreateDto(
registerAccountDto.username,
registerAccountDto.username,
"",
registerAccountDto.password
)
)
return createLocalUser
}
companion object {
private val logger = LoggerFactory.getLogger(AuthApiServiceImpl::class.java)
}
}

View File

@ -0,0 +1,9 @@
package dev.usbharu.hideout.core.service.auth
data class RecaptchaResult(
val success: Boolean,
val challenge_ts: String,
val hostname: String,
val score: Float,
val action: String
)