-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
284 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 0 additions & 101 deletions
101
src/main/kotlin/com/vauthenticator/server/login/LoginWorkflowHandler.kt
This file was deleted.
Oops, something went wrong.
44 changes: 44 additions & 0 deletions
44
src/main/kotlin/com/vauthenticator/server/login/workflow/LoginWorkflowEngine.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.vauthenticator.server.login.workflow | ||
|
||
import jakarta.servlet.http.HttpServletRequest | ||
import jakarta.servlet.http.HttpServletResponse | ||
import jakarta.servlet.http.HttpSession | ||
import org.springframework.security.core.Authentication | ||
import org.springframework.security.core.context.SecurityContextHolder | ||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler | ||
import java.util.* | ||
import kotlin.jvm.optionals.getOrElse | ||
|
||
interface LoginWorkflowEngine { | ||
fun workflowsNextHop(session: HttpSession): LoginWorkflowHandler | ||
} | ||
|
||
class CompositeLoginWorkflowEngine( | ||
private val handlers: List<LoginWorkflowHandler>, | ||
private val defaultSuccessHandler: AuthenticationSuccessHandler | ||
) : LoginWorkflowEngine, AuthenticationSuccessHandler { | ||
|
||
override fun onAuthenticationSuccess( | ||
request: HttpServletRequest, | ||
response: HttpServletResponse, | ||
authentication: Authentication | ||
) { | ||
SecurityContextHolder.getContext().authentication = authentication | ||
this.defaultSuccessHandler.onAuthenticationSuccess(request, response, authentication) | ||
} | ||
|
||
override fun workflowsNextHop(session: HttpSession): LoginWorkflowHandler { | ||
val index = Optional.ofNullable(session.getAttribute("CompositeLoginWorkflowEngine_index")).getOrElse { 0 } as Int | ||
val nextHandlerIndex = index + 1 | ||
|
||
return if (nextHandlerIndex > handlers.size) { | ||
DefaultLoginWorkflowHandler | ||
} else { | ||
session.setAttribute("CompositeLoginWorkflowEngine_index", nextHandlerIndex) | ||
handlers[index] | ||
} | ||
|
||
} | ||
|
||
} | ||
|
39 changes: 39 additions & 0 deletions
39
src/main/kotlin/com/vauthenticator/server/login/workflow/LoginWorkflowEngineController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.vauthenticator.server.login.workflow | ||
|
||
import jakarta.servlet.http.HttpServletRequest | ||
import jakarta.servlet.http.HttpServletResponse | ||
import jakarta.servlet.http.HttpSession | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.security.core.Authentication | ||
import org.springframework.security.web.DefaultRedirectStrategy | ||
import org.springframework.security.web.RedirectStrategy | ||
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler | ||
import org.springframework.stereotype.Controller | ||
import org.springframework.web.bind.annotation.GetMapping | ||
|
||
@Controller | ||
class LoginWorkflowEngineController(private val engine: LoginWorkflowEngine) { | ||
|
||
private val logger = LoggerFactory.getLogger(LoginWorkflowEngineController::class.java) | ||
|
||
val defaultNextHope = SavedRequestAwareAuthenticationSuccessHandler() | ||
private val redirectStrategy: RedirectStrategy = DefaultRedirectStrategy() | ||
|
||
@GetMapping(LOGIN_ENGINE_BROKER_PAGE) | ||
fun view( | ||
session: HttpSession, | ||
request: HttpServletRequest, | ||
response: HttpServletResponse, | ||
authentication: Authentication | ||
) { | ||
val workflowsNextHop = engine.workflowsNextHop(session) | ||
logger.debug("workflowsNextHop: $workflowsNextHop") | ||
|
||
if (workflowsNextHop.canHandle(request, response)) { | ||
logger.debug("go to redirect on ${workflowsNextHop.view()}") | ||
redirectStrategy.sendRedirect(request, response, workflowsNextHop.view()) | ||
} else { | ||
defaultNextHope.onAuthenticationSuccess(request, response, authentication) | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/com/vauthenticator/server/login/workflow/LoginWorkflowHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.vauthenticator.server.login.workflow | ||
|
||
import jakarta.servlet.http.HttpServletRequest | ||
import jakarta.servlet.http.HttpServletResponse | ||
|
||
|
||
const val LOGIN_ENGINE_BROKER_PAGE = "/login-workflow" | ||
|
||
interface LoginWorkflowHandler { | ||
|
||
fun view(): String | ||
|
||
fun canHandle(request: HttpServletRequest, response: HttpServletResponse): Boolean | ||
} | ||
|
||
object DefaultLoginWorkflowHandler : LoginWorkflowHandler { | ||
override fun view() = "" | ||
|
||
override fun canHandle(request: HttpServletRequest, response: HttpServletResponse) = false | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
src/main/kotlin/com/vauthenticator/server/mfa/MfaLoginWorkflowHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 15 additions & 11 deletions
26
...n/com/vauthenticator/server/password/changepassword/ChangePasswordLoginWorkflowHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,30 @@ | ||
package com.vauthenticator.server.password.changepassword | ||
|
||
import com.vauthenticator.server.login.LoginWorkflowHandler | ||
import com.vauthenticator.server.account.AccountMandatoryAction | ||
import com.vauthenticator.server.account.repository.AccountRepository | ||
import com.vauthenticator.server.login.workflow.LoginWorkflowHandler | ||
import jakarta.servlet.http.HttpServletRequest | ||
import jakarta.servlet.http.HttpServletResponse | ||
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler | ||
import org.springframework.security.core.context.SecurityContextHolder | ||
import org.springframework.security.web.authentication.AuthenticationSuccessHandler | ||
|
||
class ChangePasswordLoginWorkflowHandler(val url: String) : | ||
LoginWorkflowHandler { | ||
const val CHANGE_PASSWORD_URL = "/change-password" | ||
|
||
private val handler = SimpleUrlAuthenticationSuccessHandler(url) | ||
class ChangePasswordLoginWorkflowHandler( | ||
val accountRepository: AccountRepository, | ||
val handler: AuthenticationSuccessHandler | ||
) : LoginWorkflowHandler { | ||
|
||
init { | ||
handler.setAlwaysUseDefaultTargetUrl(true) | ||
} | ||
|
||
override fun view(): String = "/change-password" | ||
override fun view(): String = CHANGE_PASSWORD_URL | ||
|
||
override fun canHandle( | ||
request: HttpServletRequest, | ||
response: HttpServletResponse, | ||
): Boolean { | ||
return true | ||
val username = SecurityContextHolder.getContext().authentication.name | ||
return accountRepository.accountFor(username) | ||
.map { account -> account.mandatoryAction == AccountMandatoryAction.RESET_PASSWORD } | ||
.orElseGet { false } | ||
} | ||
|
||
} |
Oops, something went wrong.