|
| 1 | +package com.faforever.userservice.backend.account |
| 2 | + |
| 3 | +import com.faforever.userservice.backend.domain.User |
| 4 | +import com.faforever.userservice.backend.domain.UserRepository |
| 5 | +import com.faforever.userservice.backend.email.EmailService |
| 6 | +import com.faforever.userservice.backend.metrics.MetricHelper |
| 7 | +import com.faforever.userservice.backend.security.FafTokenService |
| 8 | +import com.faforever.userservice.backend.security.FafTokenType |
| 9 | +import com.faforever.userservice.backend.steam.SteamService |
| 10 | +import com.faforever.userservice.config.FafProperties |
| 11 | +import jakarta.enterprise.context.ApplicationScoped |
| 12 | +import org.slf4j.Logger |
| 13 | +import org.slf4j.LoggerFactory |
| 14 | +import java.time.Duration |
| 15 | + |
| 16 | +@ApplicationScoped |
| 17 | +class RecoveryService( |
| 18 | + private val fafProperties: FafProperties, |
| 19 | + private val metricHelper: MetricHelper, |
| 20 | + private val userRepository: UserRepository, |
| 21 | + private val fafTokenService: FafTokenService, |
| 22 | + private val steamService: SteamService, |
| 23 | + private val emailService: EmailService, |
| 24 | + private val loginService: LoginService, |
| 25 | +) { |
| 26 | + enum class Type { |
| 27 | + EMAIL, |
| 28 | + STEAM, |
| 29 | + } |
| 30 | + |
| 31 | + companion object { |
| 32 | + private val LOG: Logger = LoggerFactory.getLogger(RecoveryService::class.java) |
| 33 | + private const val KEY_USER_ID = "id" |
| 34 | + } |
| 35 | + |
| 36 | + fun requestPasswordResetViaEmail(usernameOrEmail: String) { |
| 37 | + metricHelper.incrementPasswordResetViaEmailRequestCounter() |
| 38 | + val user = userRepository.findByUsernameOrEmail(usernameOrEmail) |
| 39 | + |
| 40 | + if (user == null) { |
| 41 | + metricHelper.incrementPasswordResetViaEmailFailedCounter() |
| 42 | + LOG.info("No user found for recovery with username/email: {}", usernameOrEmail) |
| 43 | + } else { |
| 44 | + val token = fafTokenService.createToken( |
| 45 | + fafTokenType = FafTokenType.PASSWORD_RESET, |
| 46 | + lifetime = Duration.ofSeconds(fafProperties.account().passwordReset().linkExpirationSeconds()), |
| 47 | + attributes = mapOf(KEY_USER_ID to user.id.toString()), |
| 48 | + ) |
| 49 | + val passwordResetUrl = fafProperties.account().passwordReset().passwordResetUrlFormat().format(token) |
| 50 | + emailService.sendPasswordResetMail(user.username, user.email, passwordResetUrl) |
| 51 | + metricHelper.incrementPasswordResetViaEmailSentCounter() |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + fun buildSteamLoginUrl() = |
| 56 | + steamService.buildLoginUrl( |
| 57 | + redirectUrl = |
| 58 | + fafProperties.account().passwordReset().passwordResetUrlFormat().format("STEAM"), |
| 59 | + ) |
| 60 | + |
| 61 | + fun parseRecoveryHttpRequest(parameters: Map<String, List<String>>): Pair<Type, User?> { |
| 62 | + // At first glance it may seem strange that a service is parsing http request parameters, |
| 63 | + // but the parameters of the request are determined by this service itself in the request reset phase! |
| 64 | + val token = parameters["token"]?.first() |
| 65 | + LOG.debug("Extracted token: {}", token) |
| 66 | + |
| 67 | + val steamId = steamService.parseSteamIdFromRequestParameters(parameters) |
| 68 | + LOG.debug("Extracted Steam id: {}", steamId) |
| 69 | + |
| 70 | + return when { |
| 71 | + steamId != null -> Type.STEAM to steamService.findUserBySteamId(steamId).also { user -> |
| 72 | + if (user == null) metricHelper.incrementPasswordResetViaSteamFailedCounter() |
| 73 | + } |
| 74 | + |
| 75 | + token != null -> Type.EMAIL to extractUserFromEmailRecoveryToken(token) |
| 76 | + else -> { |
| 77 | + metricHelper.incrementPasswordResetViaEmailFailedCounter() |
| 78 | + throw InvalidRecoveryException("Could not extract recovery type or user from HTTP request") |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private fun extractUserFromEmailRecoveryToken(emailRecoveryToken: String): User { |
| 84 | + val claims = try { |
| 85 | + fafTokenService.getTokenClaims(FafTokenType.PASSWORD_RESET, emailRecoveryToken) |
| 86 | + } catch (exception: Exception) { |
| 87 | + metricHelper.incrementPasswordResetViaEmailFailedCounter() |
| 88 | + LOG.error("Unable to extract claims", exception) |
| 89 | + throw InvalidRecoveryException("Unable to extract claims from token") |
| 90 | + } |
| 91 | + |
| 92 | + val userId = claims[KEY_USER_ID] |
| 93 | + |
| 94 | + if (userId.isNullOrBlank()) { |
| 95 | + metricHelper.incrementPasswordResetViaEmailFailedCounter() |
| 96 | + throw InvalidRecoveryException("No user id found in token claims") |
| 97 | + } |
| 98 | + |
| 99 | + val user = userRepository.findById(userId.toInt()) |
| 100 | + |
| 101 | + if (user == null) { |
| 102 | + metricHelper.incrementPasswordResetViaEmailFailedCounter() |
| 103 | + throw InvalidRecoveryException("User with id $userId not found") |
| 104 | + } |
| 105 | + |
| 106 | + return user |
| 107 | + } |
| 108 | + |
| 109 | + fun resetPassword(type: Type, userId: Int, newPassword: String) { |
| 110 | + loginService.resetPassword(userId, newPassword) |
| 111 | + |
| 112 | + when (type) { |
| 113 | + Type.EMAIL -> metricHelper.incrementPasswordResetViaEmailDoneCounter() |
| 114 | + Type.STEAM -> metricHelper.incrementPasswordResetViaSteamDoneCounter() |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments