Skip to content

Commit

Permalink
add ServerRequestTenantIdParser
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang committed Nov 22, 2022
1 parent db86b83 commit b77d255
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package me.ahoo.cosec.context.request

import me.ahoo.cosec.tenant.Tenant

/**
* Request Tenant Id Parser.
*
Expand All @@ -24,3 +26,16 @@ fun interface RequestTenantIdParser<R> {
const val TENANT_ID_KEY = "tenant_id"
}
}

abstract class AbstractRequestTenantIdParser<R> : RequestTenantIdParser<R> {
override fun parse(request: R): String {
val tenantId = parseTenantId(request)
return if (tenantId.isNullOrEmpty()) {
Tenant.DEFAULT_TENANT_ID
} else {
tenantId
}
}

abstract fun parseTenantId(request: R): String?
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
*/
package me.ahoo.cosec.webflux

import me.ahoo.cosec.context.request.AbstractRequestTenantIdParser
import me.ahoo.cosec.context.request.RequestTenantIdParser
import me.ahoo.cosec.tenant.Tenant
import org.springframework.web.server.ServerWebExchange

/**
Expand All @@ -22,15 +22,9 @@ import org.springframework.web.server.ServerWebExchange
* @author ahoo wang
*/
class ReactiveRequestTenantIdParser(private val tenantIdKey: String = RequestTenantIdParser.TENANT_ID_KEY) :
RequestTenantIdParser<ServerWebExchange> {
override fun parse(request: ServerWebExchange): String {
val tenantId = request.request.headers.getFirst(tenantIdKey)
return if (tenantId.isNullOrEmpty()) {
Tenant.DEFAULT_TENANT_ID
} else {
tenantId
}
}
AbstractRequestTenantIdParser<ServerWebExchange>() {

override fun parseTenantId(request: ServerWebExchange): String? = request.request.headers.getFirst(tenantIdKey)

companion object {
@JvmField
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (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.context.request.AbstractRequestTenantIdParser
import me.ahoo.cosec.context.request.RequestTenantIdParser
import org.springframework.web.reactive.function.server.ServerRequest

/**
* ServerRequestTenantIdParser .
*
* @author ahoo wang
*/
class ServerRequestTenantIdParser(private val tenantIdKey: String = RequestTenantIdParser.TENANT_ID_KEY) :
AbstractRequestTenantIdParser<ServerRequest>() {
override fun parseTenantId(request: ServerRequest): String? = request.headers().firstHeader(tenantIdKey)

companion object {
@JvmField
val INSTANCE = ServerRequestTenantIdParser()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (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 io.mockk.every
import io.mockk.mockk
import me.ahoo.cosec.context.request.RequestTenantIdParser
import me.ahoo.cosec.tenant.Tenant
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.`is`
import org.junit.jupiter.api.Test
import org.springframework.web.reactive.function.server.ServerRequest

internal class ServerRequestTenantIdParserTest {
@Test
fun parse() {
val request = mockk<ServerRequest> {
every { headers().firstHeader(RequestTenantIdParser.TENANT_ID_KEY) } returns "tenantId"
}
val tenantId = ServerRequestTenantIdParser.INSTANCE.parse(request)
assertThat(tenantId, `is`("tenantId"))
}

@Test
fun parseNull() {
val request = mockk<ServerRequest> {
every { headers().firstHeader(RequestTenantIdParser.TENANT_ID_KEY) } returns null
}
val tenantId = ServerRequestTenantIdParser.INSTANCE.parse(request)
assertThat(tenantId, `is`(Tenant.DEFAULT_TENANT_ID))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
*/
package me.ahoo.cosec.servlet

import me.ahoo.cosec.context.request.AbstractRequestTenantIdParser
import me.ahoo.cosec.context.request.RequestTenantIdParser
import me.ahoo.cosec.tenant.Tenant
import javax.servlet.http.HttpServletRequest

/**
Expand All @@ -22,15 +22,9 @@ import javax.servlet.http.HttpServletRequest
* @author ahoo wang
*/
class ServletRequestTenantIdParser(private val tenantIdKey: String = RequestTenantIdParser.TENANT_ID_KEY) :
RequestTenantIdParser<HttpServletRequest> {
override fun parse(request: HttpServletRequest): String {
val tenantId = request.getHeader(tenantIdKey)
return if (tenantId.isNullOrEmpty()) {
Tenant.DEFAULT_TENANT_ID
} else {
tenantId
}
}
AbstractRequestTenantIdParser<HttpServletRequest>() {

override fun parseTenantId(request: HttpServletRequest): String? = request.getHeader(tenantIdKey)

companion object {
@JvmField
Expand Down
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=0.8.6
version=0.8.8
description=RBAC-based And Policy-based Multi-Tenant Security Framework
website=https://github.com/Ahoo-Wang/CoSec
issues=https://github.com/Ahoo-Wang/CoSec/issues
Expand Down

0 comments on commit b77d255

Please sign in to comment.