Skip to content

Commit

Permalink
add ReactiveSecurityContexts (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang authored Dec 5, 2022
1 parent 5842249 commit dadc738
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 8 deletions.
1 change: 1 addition & 0 deletions cosec-webflux/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ dependencies {
api("org.springframework:spring-web")
api("org.springframework:spring-webflux")
testImplementation("me.ahoo.cosid:cosid-test")
testImplementation("io.projectreactor:reactor-test")
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
*/
package me.ahoo.cosec.webflux

import me.ahoo.cosec.api.context.SecurityContext
import me.ahoo.cosec.context.SecurityContextParser
import me.ahoo.cosec.webflux.ReactiveSecurityContexts.writeSecurityContext
import me.ahoo.cosec.webflux.ServerWebExchanges.setSecurityContext
import org.springframework.core.Ordered
import org.springframework.web.server.ServerWebExchange
import org.springframework.web.server.WebFilter
import org.springframework.web.server.WebFilterChain
import reactor.core.publisher.Mono
import reactor.kotlin.core.publisher.toMono
import reactor.util.context.Context

/**
* ReactiveInjectSecurityContextWebFilter .
Expand All @@ -41,7 +40,7 @@ class ReactiveInjectSecurityContextWebFilter(
.build().let {
exchange.setSecurityContext(securityContext)
return chain.filter(it)
.contextWrite { ctx: Context -> ctx.put(SecurityContext.KEY, securityContext) }
.writeSecurityContext(securityContext)
}
} catch (ignored: Throwable) {
// ignored
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)].
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package me.ahoo.cosec.webflux

import me.ahoo.cosec.api.context.SecurityContext
import reactor.core.publisher.Mono
import reactor.util.context.Context
import reactor.util.context.ContextView

object ReactiveSecurityContexts {

fun ContextView.getSecurityContext(): SecurityContext {
return get(SecurityContext.KEY)
}

fun Context.setSecurityContext(securityContext: SecurityContext): Context {
return this.put(SecurityContext.KEY, securityContext)
}

/**
* Write Security Context.
*/
fun <T> Mono<T>.writeSecurityContext(securityContext: SecurityContext): Mono<T> {
return contextWrite {
it.setSecurityContext(securityContext)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@
package me.ahoo.cosec.webflux

import me.ahoo.cosec.api.authorization.Authorization
import me.ahoo.cosec.api.context.SecurityContext
import me.ahoo.cosec.context.SecurityContextParser
import me.ahoo.cosec.context.request.RequestParser
import me.ahoo.cosec.policy.serialization.CoSecJsonSerializer
import me.ahoo.cosec.webflux.ReactiveSecurityContexts.writeSecurityContext
import me.ahoo.cosec.webflux.ServerWebExchanges.setSecurityContext
import org.springframework.http.HttpStatus
import org.springframework.web.server.ServerWebExchange
import reactor.core.publisher.Mono
import reactor.kotlin.core.publisher.toMono
import reactor.util.context.Context

abstract class ReactiveSecurityFilter(
val securityContextParser: SecurityContextParser<ServerWebExchange>,
Expand All @@ -40,8 +39,7 @@ abstract class ReactiveSecurityFilter(
.principal(securityContext.principal.toMono())
.build().let {
exchange.setSecurityContext(securityContext)
return@flatMap chain(it)
.contextWrite { ctx: Context -> ctx.put(SecurityContext.KEY, securityContext) }
return@flatMap chain(it).writeSecurityContext(securityContext)
}
}
val principal = securityContext.principal
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright [2021-present] [ahoo wang <[email protected]> (https://github.com/Ahoo-Wang)].
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package me.ahoo.cosec.webflux

import me.ahoo.cosec.api.context.SecurityContext
import me.ahoo.cosec.context.SimpleSecurityContext
import me.ahoo.cosec.webflux.ReactiveSecurityContexts.getSecurityContext
import me.ahoo.cosec.webflux.ReactiveSecurityContexts.writeSecurityContext
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.junit.jupiter.api.Test
import reactor.core.publisher.Mono
import reactor.kotlin.test.test

class ReactiveSecurityContextsTest {

@Test
fun writeSecurityContext() {
Mono.empty<Void>()
.writeSecurityContext(SimpleSecurityContext.ANONYMOUS)
.test()
.expectAccessibleContext()
.assertThat {
assertThat(it.getSecurityContext(), equalTo(SimpleSecurityContext.ANONYMOUS))
}
.hasKey(SecurityContext.KEY)
.then()
.verifyComplete()
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# limitations under the License.
#
group=me.ahoo.cosec
version=1.2.2
version=1.2.3
description=RBAC-based And Policy-based Multi-Tenant Reactive Security Framework
website=https://github.com/Ahoo-Wang/CoSec
issues=https://github.com/Ahoo-Wang/CoSec/issues
Expand Down

0 comments on commit dadc738

Please sign in to comment.