|
| 1 | +/* |
| 2 | + * Copyright 2002-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.config.web.server |
| 18 | + |
| 19 | +import org.springframework.security.authorization.AuthenticatedReactiveAuthorizationManager |
| 20 | +import org.springframework.security.authorization.AuthorityReactiveAuthorizationManager |
| 21 | +import org.springframework.security.authorization.AuthorizationDecision |
| 22 | +import org.springframework.security.authorization.ReactiveAuthorizationManager |
| 23 | +import org.springframework.security.core.Authentication |
| 24 | +import org.springframework.security.web.server.authorization.AuthorizationContext |
| 25 | +import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher |
| 26 | +import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatchers |
| 27 | +import org.springframework.security.web.util.matcher.RequestMatcher |
| 28 | +import reactor.core.publisher.Mono |
| 29 | + |
| 30 | +/** |
| 31 | + * A Kotlin DSL to configure [ServerHttpSecurity] exchange authorization using idiomatic |
| 32 | + * Kotlin code. |
| 33 | + * |
| 34 | + * @author Eleftheria Stein |
| 35 | + * @since 5.4 |
| 36 | + */ |
| 37 | +class AuthorizeExchangeDsl { |
| 38 | + private val authorizationRules = mutableListOf<ExchangeAuthorizationRule>() |
| 39 | + |
| 40 | + /** |
| 41 | + * Adds an exchange authorization rule for an endpoint matching the provided |
| 42 | + * matcher. |
| 43 | + * |
| 44 | + * @param matcher the [RequestMatcher] to match incoming requests against |
| 45 | + * @param access the [ReactiveAuthorizationManager] which determines the access |
| 46 | + * to the specific matcher. |
| 47 | + * Some predefined shortcuts have already been created, such as |
| 48 | + * [hasAnyAuthority], [hasAnyRole], [permitAll], [authenticated] and more |
| 49 | + */ |
| 50 | + fun authorize(matcher: ServerWebExchangeMatcher = ServerWebExchangeMatchers.anyExchange(), |
| 51 | + access: ReactiveAuthorizationManager<AuthorizationContext> = authenticated) { |
| 52 | + authorizationRules.add(MatcherExchangeAuthorizationRule(matcher, access)) |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Adds an exchange authorization rule for an endpoint matching the provided |
| 57 | + * ant pattern. |
| 58 | + * |
| 59 | + * @param antPattern the ant ant pattern to match incoming requests against. |
| 60 | + * @param access the [ReactiveAuthorizationManager] which determines the access |
| 61 | + * to the specific matcher. |
| 62 | + * Some predefined shortcuts have already been created, such as |
| 63 | + * [hasAnyAuthority], [hasAnyRole], [permitAll], [authenticated] and more |
| 64 | + */ |
| 65 | + fun authorize(antPattern: String, access: ReactiveAuthorizationManager<AuthorizationContext> = authenticated) { |
| 66 | + authorizationRules.add(PatternExchangeAuthorizationRule(antPattern, access)) |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Matches any exchange. |
| 71 | + */ |
| 72 | + val anyExchange: ServerWebExchangeMatcher = ServerWebExchangeMatchers.anyExchange() |
| 73 | + |
| 74 | + /** |
| 75 | + * Allow access for anyone. |
| 76 | + */ |
| 77 | + val permitAll: ReactiveAuthorizationManager<AuthorizationContext> = |
| 78 | + ReactiveAuthorizationManager { _: Mono<Authentication>, _: AuthorizationContext -> Mono.just(AuthorizationDecision(true)) } |
| 79 | + |
| 80 | + /** |
| 81 | + * Deny access for everyone. |
| 82 | + */ |
| 83 | + val denyAll: ReactiveAuthorizationManager<AuthorizationContext> = |
| 84 | + ReactiveAuthorizationManager { _: Mono<Authentication>, _: AuthorizationContext -> Mono.just(AuthorizationDecision(false)) } |
| 85 | + |
| 86 | + /** |
| 87 | + * Require a specific role. This is a shortcut for [hasAuthority]. |
| 88 | + */ |
| 89 | + fun hasRole(role: String): ReactiveAuthorizationManager<AuthorizationContext> = |
| 90 | + AuthorityReactiveAuthorizationManager.hasRole<AuthorizationContext>(role) |
| 91 | + |
| 92 | + /** |
| 93 | + * Require any specific role. This is a shortcut for [hasAnyAuthority]. |
| 94 | + */ |
| 95 | + fun hasAnyRole(vararg roles: String): ReactiveAuthorizationManager<AuthorizationContext> = |
| 96 | + AuthorityReactiveAuthorizationManager.hasAnyRole<AuthorizationContext>(*roles) |
| 97 | + |
| 98 | + /** |
| 99 | + * Require a specific authority. |
| 100 | + */ |
| 101 | + fun hasAuthority(authority: String): ReactiveAuthorizationManager<AuthorizationContext> = |
| 102 | + AuthorityReactiveAuthorizationManager.hasAuthority<AuthorizationContext>(authority) |
| 103 | + |
| 104 | + /** |
| 105 | + * Require any authority. |
| 106 | + */ |
| 107 | + fun hasAnyAuthority(vararg authorities: String): ReactiveAuthorizationManager<AuthorizationContext> = |
| 108 | + AuthorityReactiveAuthorizationManager.hasAnyAuthority<AuthorizationContext>(*authorities) |
| 109 | + |
| 110 | + /** |
| 111 | + * Require an authenticated user. |
| 112 | + */ |
| 113 | + val authenticated: ReactiveAuthorizationManager<AuthorizationContext> = |
| 114 | + AuthenticatedReactiveAuthorizationManager.authenticated<AuthorizationContext>() |
| 115 | + |
| 116 | + internal fun get(): (ServerHttpSecurity.AuthorizeExchangeSpec) -> Unit { |
| 117 | + return { requests -> |
| 118 | + authorizationRules.forEach { rule -> |
| 119 | + when (rule) { |
| 120 | + is MatcherExchangeAuthorizationRule -> requests.matchers(rule.matcher).access(rule.rule) |
| 121 | + is PatternExchangeAuthorizationRule -> requests.pathMatchers(rule.pattern).access(rule.rule) |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + private data class MatcherExchangeAuthorizationRule(val matcher: ServerWebExchangeMatcher, |
| 128 | + override val rule: ReactiveAuthorizationManager<AuthorizationContext>) : ExchangeAuthorizationRule(rule) |
| 129 | + |
| 130 | + private data class PatternExchangeAuthorizationRule(val pattern: String, |
| 131 | + override val rule: ReactiveAuthorizationManager<AuthorizationContext>) : ExchangeAuthorizationRule(rule) |
| 132 | + |
| 133 | + private abstract class ExchangeAuthorizationRule(open val rule: ReactiveAuthorizationManager<AuthorizationContext>) |
| 134 | +} |
| 135 | + |
0 commit comments