forked from trueog/Staff-Auth
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
29d2ba6e2b
|
No files matched your search
@@ -1,55 +0,0 @@
|
||||
package net.trueog.staffauth.controller.oauth
|
||||
|
||||
import io.micronaut.http.HttpResponse
|
||||
import io.micronaut.http.HttpStatus
|
||||
import io.micronaut.http.annotation.*
|
||||
import io.micronaut.security.annotation.Secured
|
||||
import io.micronaut.security.rules.SecurityRule
|
||||
import net.trueog.staffauth.dto.ErrorDto
|
||||
import net.trueog.staffauth.dto.logout.LogoutDto
|
||||
import net.trueog.staffauth.service.oauth.SessionRevocationService
|
||||
import sh.ory.hydra.ApiException
|
||||
import sh.ory.hydra.api.OAuth2Api
|
||||
|
||||
/**
|
||||
* Hydra's logout UI (urls.logout = the frontend's /logout, which calls this). An app's sign-out (for example Gitea's)
|
||||
* goes to Hydra's /oauth2/sessions/logout, Hydra sends the browser here with a logout challenge, the person confirms,
|
||||
* and accepting it lets Hydra end the single sign-on session. The consent sessions are revoked as well, so every other
|
||||
* app signs the person out too. Confirmation is asked because such a request carries no proof of who sent it.
|
||||
*/
|
||||
@Controller("/logout-request")
|
||||
@Secured(SecurityRule.IS_ANONYMOUS)
|
||||
class LogoutRequestController(
|
||||
private val oAuth2Api: OAuth2Api,
|
||||
private val sessionRevocationService: SessionRevocationService
|
||||
) {
|
||||
@Get
|
||||
fun index(@QueryValue("logout_challenge") logoutChallenge: String): Map<String, Any?> {
|
||||
val logoutRequest = oAuth2Api.getOAuth2LogoutRequest(logoutChallenge)
|
||||
return mapOf(
|
||||
"clientName" to logoutRequest.client?.run { clientName?.takeIf { it.isNotBlank() } ?: clientId }
|
||||
)
|
||||
}
|
||||
|
||||
/** @return where the browser goes next */
|
||||
@Post
|
||||
fun indexSubmit(@Body logoutDto: LogoutDto): String {
|
||||
val logoutRequest = oAuth2Api.getOAuth2LogoutRequest(logoutDto.logoutChallenge)
|
||||
if (!logoutDto.logout) {
|
||||
oAuth2Api.rejectOAuth2LogoutRequest(logoutDto.logoutChallenge)
|
||||
return "/"
|
||||
}
|
||||
logoutRequest.subject?.takeIf { it.isNotBlank() }?.let { sessionRevocationService.revokeConsent(it) }
|
||||
return oAuth2Api.acceptOAuth2LogoutRequest(logoutDto.logoutChallenge).redirectTo
|
||||
}
|
||||
|
||||
@Error(exception = ApiException::class)
|
||||
fun onHydraApiException(exception: ApiException): HttpResponse<ErrorDto> {
|
||||
return when (exception.code) {
|
||||
401, 404 -> HttpResponse.unauthorized<Unit>().body(ErrorDto.Default("INVALID_LOGOUT_CHALLENGE"))
|
||||
410 -> HttpResponse.unauthorized<Unit>().body(ErrorDto.Default("LOGOUT_REQUEST_USED"))
|
||||
else -> HttpResponse.status<Unit>(HttpStatus.valueOf(exception.code.takeIf { it != 0 } ?: 500))
|
||||
.body(ErrorDto.Default("HYDRA"))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
package net.trueog.staffauth.dto.logout
|
||||
|
||||
import io.micronaut.serde.annotation.Serdeable
|
||||
|
||||
@Serdeable
|
||||
data class LogoutDto(val logout: Boolean, val logoutChallenge: String)
|
||||
@@ -13,14 +13,10 @@ import net.trueog.staffauth.exception.user.DeleteSelfException
|
||||
import net.trueog.staffauth.exception.user.DuplicateMinecraftUuidException
|
||||
import net.trueog.staffauth.exception.user.InvalidMinecraftUuidException
|
||||
import net.trueog.staffauth.repository.UserRepository
|
||||
import net.trueog.staffauth.service.oauth.SessionRevocationService
|
||||
import sh.ory.hydra.api.OAuth2Api
|
||||
|
||||
@Singleton
|
||||
class UserService(
|
||||
private val userRepository: UserRepository,
|
||||
private val minecraftClient: MinecraftClient,
|
||||
private val sessionRevocationService: SessionRevocationService
|
||||
) {
|
||||
class UserService(private val userRepository: UserRepository, private val minecraftClient: MinecraftClient, private val oAuth2Api: OAuth2Api) {
|
||||
fun list() = userRepository.findAllOrderById().map {
|
||||
val username = minecraftClient.getByUuid(it.minecraftUuid)?.name
|
||||
UserDto.fromEntity(it, username)
|
||||
@@ -82,5 +78,8 @@ class UserService(
|
||||
return deleted
|
||||
}
|
||||
|
||||
private fun revokeSessions(user: User) = sessionRevocationService.revokeAll(user.uuid.toString())
|
||||
private fun revokeSessions(user: User) {
|
||||
oAuth2Api.revokeOAuth2LoginSessions(user.uuid.toString(), null)
|
||||
oAuth2Api.revokeOAuth2ConsentSessions(user.uuid.toString(), null, null, true)
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
package net.trueog.staffauth.service.oauth
|
||||
|
||||
import io.micronaut.context.event.ApplicationEventListener
|
||||
import io.micronaut.security.authentication.Authentication
|
||||
import io.micronaut.security.event.LogoutEvent
|
||||
import jakarta.inject.Singleton
|
||||
|
||||
/**
|
||||
* The panel's logout (Micronaut Security's /logout) only cleared the panel's own cookie, so the Hydra single sign-on
|
||||
* session lived on and every app signed the person straight back in. Micronaut publishes a LogoutEvent with the
|
||||
* authentication before it redirects; end the subject's Hydra sessions there. Listeners run synchronously, so a Hydra
|
||||
* failure fails the logout request instead of pretending the person was signed out.
|
||||
*/
|
||||
@Singleton
|
||||
class LogoutListener(private val sessionRevocationService: SessionRevocationService) :
|
||||
ApplicationEventListener<LogoutEvent> {
|
||||
override fun onApplicationEvent(event: LogoutEvent) {
|
||||
val authentication = event.source as? Authentication ?: return
|
||||
val subject = authentication.attributes["sub"] as? String ?: return
|
||||
sessionRevocationService.revokeAll(subject)
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package net.trueog.staffauth.service.oauth
|
||||
|
||||
import jakarta.inject.Singleton
|
||||
import sh.ory.hydra.api.OAuth2Api
|
||||
|
||||
/**
|
||||
* Ends a subject's Staff-Auth sign-ins at Hydra. Used on role change, deactivation and deletion (UserService) and on
|
||||
* logout (LogoutListener, LogoutRequestController).
|
||||
*
|
||||
* Revoking the consent sessions revokes every token the apps hold for the subject; Gitea (its user sync and
|
||||
* trueog-authwatch) and WeKan (trueog-wekan-authwatch) notice that and sign the person out, and the next sign-in goes
|
||||
* through Staff-Auth again.
|
||||
*/
|
||||
@Singleton
|
||||
class SessionRevocationService(private val oAuth2Api: OAuth2Api) {
|
||||
/** Login sessions (single sign-on) and consent sessions (the apps' tokens). */
|
||||
fun revokeAll(subject: String) {
|
||||
oAuth2Api.revokeOAuth2LoginSessions(subject, null)
|
||||
revokeConsent(subject)
|
||||
}
|
||||
|
||||
/** Consent sessions only: for a logout Hydra itself is completing, which ends the login session. */
|
||||
fun revokeConsent(subject: String) {
|
||||
oAuth2Api.revokeOAuth2ConsentSessions(subject, null, null, true)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user