diff --git a/src/main/kotlin/es/wokis/data/bo/user/UserBO.kt b/src/main/kotlin/es/wokis/data/bo/user/UserBO.kt index fddb50d..c685c9b 100644 --- a/src/main/kotlin/es/wokis/data/bo/user/UserBO.kt +++ b/src/main/kotlin/es/wokis/data/bo/user/UserBO.kt @@ -16,6 +16,7 @@ data class UserBO( val totpEncodedSecret: ByteArray? = null, val currentSession: String? = null, val emailVerified: Boolean = false, + val loginWithGoogle: Boolean = false, val sessions: List = emptyList(), val badges: List = emptyList(), val devices: List = emptyList(), diff --git a/src/main/kotlin/es/wokis/data/dbo/user/UserDBO.kt b/src/main/kotlin/es/wokis/data/dbo/user/UserDBO.kt index 3d267e9..767396e 100644 --- a/src/main/kotlin/es/wokis/data/dbo/user/UserDBO.kt +++ b/src/main/kotlin/es/wokis/data/dbo/user/UserDBO.kt @@ -16,6 +16,7 @@ data class UserDBO( val image: String = ServerConstants.EMPTY_TEXT, val createdOn: Long = Date().time, val emailVerified: Boolean = false, + val loginWithGoogle: Boolean = false, val totpEncodedSecret: ByteArray? = null, val sessions: List = emptyList(), val badges: List = emptyList(), diff --git a/src/main/kotlin/es/wokis/data/dto/user/UserDTO.kt b/src/main/kotlin/es/wokis/data/dto/user/UserDTO.kt index a72e64c..5b42984 100644 --- a/src/main/kotlin/es/wokis/data/dto/user/UserDTO.kt +++ b/src/main/kotlin/es/wokis/data/dto/user/UserDTO.kt @@ -15,6 +15,8 @@ data class UserDTO( val image: String = EMPTY_TEXT, @SerializedName("lang") val lang: String, + @SerializedName("loginWithGoogle") + val loginWithGoogle: Boolean = false, @SerializedName("createdOn") val createdOn: Long, @SerializedName("totpEnabled") diff --git a/src/main/kotlin/es/wokis/data/mapper/user/UserMapper.kt b/src/main/kotlin/es/wokis/data/mapper/user/UserMapper.kt index 92d9365..f82c3f9 100644 --- a/src/main/kotlin/es/wokis/data/mapper/user/UserMapper.kt +++ b/src/main/kotlin/es/wokis/data/mapper/user/UserMapper.kt @@ -35,6 +35,7 @@ fun UserDTO.toBO() = UserBO( image = image, lang = lang, devices = devices, + loginWithGoogle = loginWithGoogle, badges = badges.toBO(), createdOn = createdOn, emailVerified = emailVerified @@ -53,6 +54,7 @@ fun UserBO.toDBO() = UserDBO( sessions = sessions, createdOn = createdOn, emailVerified = emailVerified, + loginWithGoogle = loginWithGoogle, recoverWords = recoverWords ) @@ -66,6 +68,7 @@ fun UserDBO.toBO() = UserBO( totpEncodedSecret = totpEncodedSecret, devices = devices, sessions = sessions, + loginWithGoogle = loginWithGoogle, badges = badges.toBO(), createdOn = createdOn, emailVerified = emailVerified, @@ -82,6 +85,7 @@ fun UserBO.toDTO() = UserDTO( lang = lang, totpEnabled = totpEncodedSecret != null, devices = devices, + loginWithGoogle = loginWithGoogle, badges = badges.toDTO(), createdOn = createdOn, emailVerified = emailVerified diff --git a/src/main/kotlin/es/wokis/data/repository/recover/RecoverRepository.kt b/src/main/kotlin/es/wokis/data/repository/recover/RecoverRepository.kt index 37c5a5c..ae1e2ef 100644 --- a/src/main/kotlin/es/wokis/data/repository/recover/RecoverRepository.kt +++ b/src/main/kotlin/es/wokis/data/repository/recover/RecoverRepository.kt @@ -8,6 +8,7 @@ import es.wokis.data.exception.RecoverCodeNotFoundException import es.wokis.data.exception.UserNotFoundException import es.wokis.data.repository.user.UserRepository import es.wokis.services.EmailService +import org.mindrot.jbcrypt.BCrypt interface RecoverRepository { suspend fun changeUserPassword(changePassRequest: ChangePassRequestDTO): AcknowledgeBO @@ -25,10 +26,21 @@ class RecoverRepositoryImpl( throw RecoverCodeNotFoundException } val recover = localDataSource.getRecoverByToken(changePassRequest.recoverCode) - recover?.let { - val user = userRepository.getUserByEmail(it.email) + recover?.let { recover -> + val user = userRepository.getUserByEmail(recover.email) return user?.let { - userRepository.updateUser(user.copy(password = changePassRequest.newPass, sessions = listOf())) + recover.id?.let { recoverId -> + localDataSource.removeRecover(recoverId) + } + userRepository.updateUser( + user.copy( + password = BCrypt.hashpw( + changePassRequest.newPass, + BCrypt.gensalt() + ), + sessions = listOf() + ) + ) } ?: throw UserNotFoundException } throw RecoverCodeNotFoundException @@ -37,6 +49,9 @@ class RecoverRepositoryImpl( override suspend fun requestChangePass(email: String): AcknowledgeBO { val user = userRepository.getUserByEmail(email) user?.let { + if (user.emailVerified) { + throw IllegalStateException() + } emailService.sendRecoverPass(user)?.also { return saveRequestChangePass(it) } ?: throw IllegalStateException() diff --git a/src/main/kotlin/es/wokis/data/repository/user/UserRepository.kt b/src/main/kotlin/es/wokis/data/repository/user/UserRepository.kt index d79101d..9fee831 100644 --- a/src/main/kotlin/es/wokis/data/repository/user/UserRepository.kt +++ b/src/main/kotlin/es/wokis/data/repository/user/UserRepository.kt @@ -106,7 +106,6 @@ class UserRepositoryImpl( val email: String = payload.email val imageUrl: String = (payload["picture"] as? String).orEmpty() val locale: String = payload["locale"] as? String ?: DEFAULT_LANG - val username = email.split("@").firstOrNull() ?: HashGenerator.generateHash() val user = getUserByEmail(email) val token = if (user == null) { val token = register( @@ -132,7 +131,7 @@ class UserRepositoryImpl( } else { login( - LoginDTO(username = username, password = EMPTY_TEXT, isGoogleAuth = true), + LoginDTO(username = email, password = EMPTY_TEXT, isGoogleAuth = true), code, timeStamp ) @@ -214,7 +213,8 @@ class UserRepositoryImpl( if (BCrypt.checkpw(changePass.oldPass, user.password)) { return updateUser( user.copy( - password = BCrypt.hashpw(changePass.newPass, BCrypt.gensalt()) + password = BCrypt.hashpw(changePass.newPass, BCrypt.gensalt()), + sessions = emptyList() ) ) } diff --git a/src/main/kotlin/es/wokis/plugins/HTTP.kt b/src/main/kotlin/es/wokis/plugins/HTTP.kt index 8864771..b25e1e8 100644 --- a/src/main/kotlin/es/wokis/plugins/HTTP.kt +++ b/src/main/kotlin/es/wokis/plugins/HTTP.kt @@ -28,6 +28,6 @@ fun Application.configureHTTP() { allowSameOrigin = true maxAgeInSeconds = Duration.ofDays(1).toMinutes() * 60L - anyHost() // TODO: No dejarlo como anyhost, limitarlo al host final + anyHost() } } diff --git a/src/main/resources/emails/es/recover-pass.html b/src/main/resources/emails/es/recover-pass.html index 5f03653..c776d70 100644 --- a/src/main/resources/emails/es/recover-pass.html +++ b/src/main/resources/emails/es/recover-pass.html @@ -1 +1 @@ -

Recover your password

Use the following code on the app to recover your password: %%TOKEN

If you have not requested this action, ignore this message.

Thank you!

\ No newline at end of file +

Recuperar tu contraseña

Usa el siguiente código en la app para recuperar tu contraseña: %%TOKEN

Si no has solicitado este código, ignora este mensaje.

¡Gracias!

\ No newline at end of file