-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backend): implement a superuser role #785
It allows curators to do everything that a normal group member could do on behalf of the group.
- Loading branch information
1 parent
a14dcb7
commit 94973f2
Showing
39 changed files
with
970 additions
and
345 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
65 changes: 65 additions & 0 deletions
65
backend/src/main/kotlin/org/loculus/backend/auth/AuthenticatedUser.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,65 @@ | ||
package org.loculus.backend.auth | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema | ||
import org.loculus.backend.auth.Roles.SUPER_USER | ||
import org.springframework.core.MethodParameter | ||
import org.springframework.security.core.context.SecurityContextHolder | ||
import org.springframework.security.oauth2.core.oidc.StandardClaimNames | ||
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.bind.support.WebDataBinderFactory | ||
import org.springframework.web.context.request.NativeWebRequest | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver | ||
import org.springframework.web.method.support.ModelAndViewContainer | ||
|
||
object Roles { | ||
const val SUPER_USER = "super_user" | ||
const val PREPROCESSING_PIPELINE = "preprocessing_pipeline" | ||
const val GET_RELEASED_DATA = "get_released_data" | ||
} | ||
|
||
class AuthenticatedUser(private val source: JwtAuthenticationToken) { | ||
val username: String | ||
get() = source.token.claims[StandardClaimNames.PREFERRED_USERNAME] as String | ||
|
||
val isSuperUser: Boolean | ||
get() = source.authorities.any { it.authority == SUPER_USER } | ||
} | ||
|
||
@Component | ||
class UserConverter : HandlerMethodArgumentResolver { | ||
override fun supportsParameter(parameter: MethodParameter): Boolean { | ||
return AuthenticatedUser::class.java.isAssignableFrom(parameter.parameterType) | ||
} | ||
|
||
override fun resolveArgument( | ||
parameter: MethodParameter, | ||
mavContainer: ModelAndViewContainer?, | ||
webRequest: NativeWebRequest, | ||
binderFactory: WebDataBinderFactory?, | ||
): Any? { | ||
val authentication = SecurityContextHolder.getContext().authentication | ||
if (authentication is JwtAuthenticationToken) { | ||
return AuthenticatedUser(authentication) | ||
} | ||
throw IllegalArgumentException("Authentication object not of type AbstractAuthenticationToken") | ||
} | ||
} | ||
|
||
/** | ||
* Hides a parameter from the generated OpenAPI documentation. | ||
* Usage: | ||
* | ||
* ```kotlin | ||
* @RestController | ||
* class MyController { | ||
* @GetMapping("/my-endpoint") | ||
* fun myFunction(@HiddenParam authenticatedUser: AuthenticatedUser) { | ||
* // ... | ||
* } | ||
* } | ||
*/ | ||
@Target(AnnotationTarget.VALUE_PARAMETER) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
@Schema(hidden = true) | ||
annotation class HiddenParam |
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
Oops, something went wrong.