-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#151 add login and logout endpoint for clients to call for user when…
… handling user login and logout.
- Loading branch information
Showing
16 changed files
with
352 additions
and
100 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
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
75 changes: 75 additions & 0 deletions
75
src/main/kotlin/dniel/forwardauth/application/commandhandlers/LoginHandler.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,75 @@ | ||
package dniel.forwardauth.application.commandhandlers | ||
|
||
import dniel.forwardauth.AuthProperties | ||
import dniel.forwardauth.application.Command | ||
import dniel.forwardauth.application.CommandHandler | ||
import dniel.forwardauth.domain.authorize.AuthorizeNonce | ||
import dniel.forwardauth.domain.authorize.AuthorizeState | ||
import dniel.forwardauth.domain.authorize.AuthorizeUrl | ||
import dniel.forwardauth.domain.authorize.RequestedUrl | ||
import dniel.forwardauth.domain.events.Event | ||
import dniel.forwardauth.domain.shared.Application | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.stereotype.Component | ||
import java.net.URI | ||
|
||
/** | ||
* Handle Logout of user. | ||
* | ||
*/ | ||
@Component | ||
class LoginHandler(private val properties: AuthProperties) : CommandHandler<LoginHandler.LoginCommand> { | ||
|
||
private val LOGGER = LoggerFactory.getLogger(this::class.java) | ||
|
||
/** | ||
* This is the input parameter object for the handler to pass inn all | ||
* needed parameters to the handler. | ||
* @param forwardedHost is the name of the application used to signout. | ||
*/ | ||
data class LoginCommand(val forwardedHost: String) : Command | ||
|
||
|
||
/** | ||
* This command can produce a set of events as response from the handle method. | ||
*/ | ||
sealed class LoginEvent(val app: Application) : Event() { | ||
class LoginRedirect(val redirectUrl: URI, | ||
val nonce: AuthorizeNonce, | ||
val tokenCookieDomain: String, | ||
val maxNonceAge: Int, | ||
app: Application) : LoginEvent(app) | ||
|
||
class Error(val reason: String = "Unknown error", app: Application) : LoginEvent(app) | ||
} | ||
|
||
/** | ||
* Main handle Sign out method. | ||
* <p/> | ||
* @return an sign out event containing the result status of the sign out. | ||
*/ | ||
override fun handle(params: LoginCommand): Event { | ||
LOGGER.debug("Login with Auth0") | ||
val app = properties.findApplicationOrDefault(params.forwardedHost) | ||
|
||
// just abort if no login url is set, nowhere to redirect user after login. | ||
if(app.loginUri.isNullOrBlank()){ | ||
return LoginEvent.Error("Missing login url in configuration.", app) | ||
} | ||
|
||
val authUrl = properties.authorizeUrl | ||
val nonce = AuthorizeNonce.generate() | ||
val loginUrl = URI.create(app.loginUri) | ||
val originUrl = RequestedUrl(loginUrl.scheme, loginUrl.host, loginUrl.path, "GET") | ||
val state = AuthorizeState.create(originUrl, nonce) | ||
val authorizeUrl = AuthorizeUrl(authUrl, app, state) | ||
val tokenCookieDomain = app.tokenCookieDomain | ||
val maxNonceAge = properties.nonceMaxAge | ||
|
||
try { | ||
return LoginEvent.LoginRedirect(authorizeUrl.toURI(), nonce, tokenCookieDomain, maxNonceAge, app) | ||
} catch (e: Exception) { | ||
return LoginEvent.Error(e.message!!, app) | ||
} | ||
} | ||
} |
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
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
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
51 changes: 51 additions & 0 deletions
51
src/main/kotlin/dniel/forwardauth/infrastructure/spring/controllers/ErrorController.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,51 @@ | ||
package dniel.forwardauth.infrastructure.spring.controllers | ||
|
||
import dniel.forwardauth.infrastructure.spring.exceptions.PermissionDeniedException | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.boot.autoconfigure.web.ServerProperties | ||
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController | ||
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver | ||
import org.springframework.boot.web.servlet.error.ErrorAttributes | ||
import org.springframework.http.HttpHeaders | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.MediaType | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.bind.annotation.ControllerAdvice | ||
import org.springframework.web.bind.annotation.ExceptionHandler | ||
import org.springframework.web.bind.annotation.RestController | ||
import org.springframework.web.context.request.WebRequest | ||
import org.springframework.web.servlet.ModelAndView | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler | ||
import javax.servlet.http.HttpServletRequest | ||
import javax.servlet.http.HttpServletResponse | ||
|
||
|
||
//@Component | ||
class ErrorController(errorAttributes: ErrorAttributes?, | ||
serverProperties: ServerProperties, | ||
errorViewResolvers: List<ErrorViewResolver?>?) : BasicErrorController(errorAttributes, serverProperties.error, errorViewResolvers) { | ||
|
||
override fun errorHtml(request: HttpServletRequest?, response: HttpServletResponse?): ModelAndView { | ||
response!!.setHeader("testHtml", "test") | ||
return super.errorHtml(request, response) | ||
} | ||
|
||
override fun error(request: HttpServletRequest): ResponseEntity<Map<String, Any>> { | ||
val body = getErrorAttributes(request, | ||
isIncludeStackTrace(request, MediaType.ALL)) | ||
val status = getStatus(request) | ||
val headers = HttpHeaders() | ||
headers.setContentType(MediaType.APPLICATION_JSON_UTF8) | ||
headers.put("test", listOf("test")) | ||
return ResponseEntity(body, headers, status) | ||
} | ||
|
||
companion object { | ||
private val LOGGER = LoggerFactory.getLogger(this.javaClass) | ||
} | ||
|
||
init { | ||
LOGGER.info("Created") | ||
} | ||
} |
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
Oops, something went wrong.