mirror of https://github.com/usbharu/Hideout.git
feat: reCAPTCHA二対応
This commit is contained in:
parent
a02c995b6a
commit
c579efb110
|
@ -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
|
||||
)
|
|
@ -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
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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
|
||||
)
|
Loading…
Reference in New Issue