|
1 | 1 | package osahner.service
|
2 | 2 |
|
| 3 | +import org.jboss.aerogear.security.otp.Totp |
3 | 4 | import org.springframework.security.authentication.AuthenticationManager
|
4 | 5 | import org.springframework.security.authentication.BadCredentialsException
|
5 | 6 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
|
6 | 7 | import org.springframework.security.core.Authentication
|
7 | 8 | import org.springframework.security.core.AuthenticationException
|
| 9 | +import org.springframework.security.core.GrantedAuthority |
| 10 | +import org.springframework.security.core.authority.SimpleGrantedAuthority |
| 11 | +import org.springframework.security.core.userdetails.UsernameNotFoundException |
8 | 12 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
|
9 | 13 | import org.springframework.stereotype.Component
|
| 14 | +import osahner.domain.User |
10 | 15 |
|
11 | 16 |
|
12 | 17 | @Component
|
13 | 18 | class AppAuthenticationManager(
|
14 |
| - private val userService: AppUserDetailsService, val bCryptPasswordEncoder: BCryptPasswordEncoder, |
| 19 | + private val userRepository: UserRepository, |
| 20 | + val bCryptPasswordEncoder: BCryptPasswordEncoder, |
15 | 21 | ) : AuthenticationManager {
|
16 | 22 | @Throws(AuthenticationException::class)
|
17 |
| - override fun authenticate(authentication: Authentication): Authentication? { |
| 23 | + override fun authenticate(authentication: Authentication): Authentication { |
18 | 24 | val password = authentication.credentials.toString()
|
19 |
| - val user = userService.loadUserByUsername(authentication.name) |
| 25 | + val user: User = userRepository.findByUsername(authentication.name).orElseThrow { |
| 26 | + UsernameNotFoundException("The username ${authentication.name} doesn't exist") |
| 27 | + } |
20 | 28 | if (!bCryptPasswordEncoder.matches(password, user.password)) {
|
21 | 29 | throw BadCredentialsException("Bad credentials")
|
22 | 30 | }
|
23 |
| - return UsernamePasswordAuthenticationToken(user.username, user.password, user.authorities) |
| 31 | + if (user.isUsing2FA) { |
| 32 | + val verificationCode: String = (authentication.details as Map<*, *>)["verificationCode"].toString() |
| 33 | + val totp = Totp(user.secret) |
| 34 | + when { |
| 35 | + !isValidLong(verificationCode) -> throw BadCredentialsException("Invalid verfication code") |
| 36 | + !totp.verify(verificationCode) -> throw BadCredentialsException("Invalid verfication code") |
| 37 | + } |
| 38 | + } |
| 39 | + val authorities = ArrayList<GrantedAuthority>() |
| 40 | + if (user.roles != null) { |
| 41 | + user.roles!!.forEach { authorities.add(SimpleGrantedAuthority(it.roleName)) } |
| 42 | + } |
| 43 | + return UsernamePasswordAuthenticationToken(user.username, user.password, authorities) |
| 44 | + } |
| 45 | + |
| 46 | + private fun isValidLong(code: String): Boolean { |
| 47 | + try { |
| 48 | + code.toLong() |
| 49 | + } catch (e: NumberFormatException) { |
| 50 | + return false |
| 51 | + } |
| 52 | + return true |
24 | 53 | }
|
25 | 54 | }
|
0 commit comments