Skip to content

Commit

Permalink
update version detekt-formatting to 1.22.0 (#5)
Browse files Browse the repository at this point in the history
* update version detekt-formatting to 1.22.0
* refactor constants definition
  • Loading branch information
Ahoo-Wang authored Nov 22, 2022
1 parent 9784867 commit 40384d9
Show file tree
Hide file tree
Showing 18 changed files with 61 additions and 68 deletions.
3 changes: 1 addition & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformJvmPlugin

plugins {
id("io.github.gradle-nexus.publish-plugin")
id("io.gitlab.arturbosch.detekt").version("1.21.0")
id("io.gitlab.arturbosch.detekt").version("1.22.0")
kotlin("jvm") version "1.7.21"
id("org.jetbrains.dokka") version "1.7.20"
id("me.champeau.jmh")
Expand Down Expand Up @@ -66,7 +66,6 @@ configure(bomProjects) {
configure(libraryProjects) {
apply<DetektPlugin>()
configure<DetektExtension> {
toolVersion = "1.21.0"
source = files(DEFAULT_SRC_DIR_JAVA, DEFAULT_SRC_DIR_KOTLIN)
config = files("${rootProject.rootDir}/config/detekt/detekt.yml")
buildUponDefaultConfig = true
Expand Down
7 changes: 2 additions & 5 deletions cosec-core/src/main/kotlin/me/ahoo/cosec/CoSec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
package me.ahoo.cosec

import me.ahoo.cosec.util.Internals
import me.ahoo.cosec.internal.InternalIds

/**
* CoSec const.
Expand All @@ -24,8 +24,5 @@ object CoSec {
const val COSEC_PREFIX = "$COSEC."

@JvmField
val EMPTY = Internals.format("0")

@JvmField
val UNKNOWN = Internals.format("nil")
val DEFAULT = InternalIds.wrap("0")
}
9 changes: 0 additions & 9 deletions cosec-core/src/main/kotlin/me/ahoo/cosec/Named.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,11 @@
*/
package me.ahoo.cosec

import me.ahoo.cosec.util.Internals

/**
* Named .
*
* @author ahoo wang
*/
interface Named {
val name: String

companion object {

const val NAME_DELIMITER = ":"

val NAME_ALL = Internals.format("all")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import me.ahoo.cosec.principal.CoSecPrincipal
import me.ahoo.cosec.principal.TenantPrincipal
import me.ahoo.cosec.tenant.Tenant
import me.ahoo.cosec.tenant.TenantCapable
import me.ahoo.cosec.util.Internals.format
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import javax.annotation.concurrent.ThreadSafe
Expand All @@ -32,8 +31,8 @@ class SecurityContext(
override val tenant: Tenant = principal.tenant
) : TenantCapable {
companion object {
const val KEY = "COSEC_SECURITY_CONTEXT"
val ANONYMOUS: SecurityContext = SecurityContext(TenantPrincipal.ANONYMOUS)
val KEY = format("COSEC_SECURITY_CONTEXT")
}

private val attributes: MutableMap<String, Any> = ConcurrentHashMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,39 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.ahoo.cosec.util
package me.ahoo.cosec.internal

/**
* Internals .
* Internal Id Tool .
*
* @author ahoo wang
*/
object Internals {
object InternalIds {
/**
* 为了避免排序问题选择'(' 作为前缀.
* 为了避免排序问题选择 `(` 作为前缀.
* `'('<'0'`
*/
const val PREFIX = "("
private const val PREFIX = "("

/**
* 为了避免排序问题选择')' 作为后缀.
* 为了避免排序问题选择 `)` 作为后缀.
* `')'<'0'`
*/
private const val SUFFIX = ")"

@JvmStatic
fun format(raw: String): String {
fun wrap(raw: String): String {
return "$PREFIX$raw$SUFFIX"
}

@JvmStatic
fun parseRaw(wrapped: String): String {
require(isInternal(wrapped)) { "wrapped:[$wrapped] is not internal." }
fun unwrap(wrapped: String): String {
require(isWrapped(wrapped)) { "wrapped:[$wrapped] is not internal." }
return wrapped.substring(PREFIX.length, wrapped.length - 1)
}

@JvmStatic
fun isInternal(wrapped: String): Boolean {
fun isWrapped(wrapped: String): Boolean {
return (wrapped.length > 2 && wrapped.startsWith(PREFIX) && wrapped.endsWith(SUFFIX))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface PolicyCapable {
* get policy ids.
* relation:
*
* [CoSecPrincipal] 1:N [me.ahoo.cosec.policy.Policy]
* [me.ahoo.cosec.principal.CoSecPrincipal] 1:N [me.ahoo.cosec.policy.Policy]
*
* @return policy ids..
*/
Expand Down
12 changes: 10 additions & 2 deletions cosec-core/src/main/kotlin/me/ahoo/cosec/policy/PolicyEvaluator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@ object DefaultPolicyEvaluator : PolicyEvaluator {
private val mockContext = SecurityContext(TenantPrincipal.ANONYMOUS)

override fun evaluate(policy: Policy) {
policy.statements.forEach {
it.verify(mockRequest, mockContext)
policy.statements.forEach { statement ->
statement.verify(mockRequest, mockContext)

statement.actions.forEach {
it.match(mockRequest, mockContext)
}

statement.conditions.forEach {
it.match(mockRequest, mockContext)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
package me.ahoo.cosec.principal

import me.ahoo.cosec.CoSec
import me.ahoo.cosec.internal.InternalIds.wrap
import me.ahoo.cosec.policy.PolicyCapable
import me.ahoo.cosec.util.Internals.format
import java.security.Principal

/**
Expand Down Expand Up @@ -49,9 +49,9 @@ interface CoSecPrincipal : Principal, PolicyCapable, RoleCapable {
//endregion
//region ANONYMOUS 未认证状态下的用户

val ANONYMOUS_ID = CoSec.EMPTY
val ANONYMOUS_ID = CoSec.DEFAULT

val ANONYMOUS_NAME = format("anonymous")
val ANONYMOUS_NAME = wrap("anonymous")

val ANONYMOUS: CoSecPrincipal = SimplePrincipal(ANONYMOUS_ID, ANONYMOUS_NAME)

Expand Down
6 changes: 3 additions & 3 deletions cosec-core/src/main/kotlin/me/ahoo/cosec/tenant/Tenant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
package me.ahoo.cosec.tenant

import me.ahoo.cosec.CoSec
import me.ahoo.cosec.util.Internals.format
import me.ahoo.cosec.internal.InternalIds.wrap

/**
* Tenant for splitting customer boundaries horizontally.
Expand Down Expand Up @@ -41,13 +41,13 @@ interface Tenant {
* 根平台租户ID.
*/
@JvmField
val PLATFORM_TENANT_ID = format("platform")
val PLATFORM_TENANT_ID = wrap("platform")

@JvmField
val PLATFORM: Tenant = SimpleTenant(PLATFORM_TENANT_ID)

@JvmField
val DEFAULT_TENANT_ID = CoSec.EMPTY
val DEFAULT_TENANT_ID = CoSec.DEFAULT

@JvmField
val DEFAULT: Tenant = SimpleTenant(DEFAULT_TENANT_ID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ import me.ahoo.cosec.principal.CoSecPrincipal
interface TokenPrincipal : TokenIdCapable, CoSecPrincipal {
companion object {
@JvmField
val ANONYMOUS: TokenPrincipal = SimpleTokenPrincipal(CoSec.EMPTY, CoSecPrincipal.ANONYMOUS)
val ANONYMOUS: TokenPrincipal = SimpleTokenPrincipal(CoSec.DEFAULT, CoSecPrincipal.ANONYMOUS)
}
}
6 changes: 3 additions & 3 deletions cosec-core/src/test/kotlin/me/ahoo/cosec/CoSecTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import org.junit.jupiter.api.Test
internal class CoSecTest {
@Test
fun test() {
val reg = Regex("^order:[a-z]*:PUT$", RegexOption.IGNORE_CASE)

assertThat(reg.matches("order:create:PUT"), equalTo(true))
assertThat(CoSec.COSEC, equalTo("cosec"))
assertThat(CoSec.COSEC_PREFIX, equalTo("cosec."))
assertThat(CoSec.DEFAULT, equalTo("(0)"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal class SecurityContextTest {
fun test() {
val context = SecurityContext(CoSecPrincipal.ANONYMOUS)
context.setAttribute("key", "value")
assertThat(context.getAttribute("key"), `is`("value"))
assertThat(context.getAttribute<String>("key"), `is`("value"))
assertThat(context.getRequiredAttribute("key"), `is`("value"))
Assertions.assertThrows(IllegalArgumentException::class.java) {
context.getRequiredAttribute("key1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.ahoo.cosec.util
package me.ahoo.cosec.internal

import me.ahoo.cosec.util.Internals.format
import me.ahoo.cosec.util.Internals.isInternal
import me.ahoo.cosec.util.Internals.parseRaw
import me.ahoo.cosec.internal.InternalIds.isWrapped
import me.ahoo.cosec.internal.InternalIds.unwrap
import me.ahoo.cosec.internal.InternalIds.wrap
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
import org.junit.jupiter.api.Assertions
Expand All @@ -25,7 +25,7 @@ import org.junit.jupiter.api.Test
*
* @author ahoo wang
*/
internal class InternalsTest {
internal class InternalIdsTest {
@Test
fun compare() {
Assertions.assertTrue('(' < '0')
Expand All @@ -34,20 +34,20 @@ internal class InternalsTest {

@Test
fun format() {
assertThat(format("id"), equalTo("(id)"))
assertThat(wrap("id"), equalTo("(id)"))
}

@Test
fun parseRawId() {
assertThat(parseRaw("(id)"), equalTo("id"))
Assertions.assertEquals("id", parseRaw("(id)"))
assertThat(unwrap("(id)"), equalTo("id"))
Assertions.assertEquals("id", unwrap("(id)"))
}

@Test
fun isInternal() {
assertThat(isInternal("(id)"), equalTo(true))
assertThat(isInternal("(i)"), equalTo(true))
assertThat(isInternal("()"), equalTo(false))
assertThat(isInternal("("), equalTo(false))
assertThat(isWrapped("(id)"), equalTo(true))
assertThat(isWrapped("(i)"), equalTo(true))
assertThat(isWrapped("()"), equalTo(false))
assertThat(isWrapped("("), equalTo(false))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package me.ahoo.cosec.oauth.client
import me.ahoo.cosec.oauth.OAuthUser
import me.ahoo.cosec.principal.CoSecPrincipal
import me.ahoo.cosec.principal.SimplePrincipal
import me.ahoo.cosec.util.Internals.format
import reactor.core.publisher.Mono
import reactor.kotlin.core.publisher.toMono

Expand All @@ -29,7 +28,7 @@ object DirectOAuthClientPrincipalConverter : OAuthClientPrincipalConverter {
override fun convert(client: String, authUser: OAuthUser): Mono<CoSecPrincipal> {
authUser.rawInfo[OAuthClientPrincipalConverter.OAUTH_CLIENT] = client
return SimplePrincipal(
id = getCoSecClientUserId(client, authUser),
id = asClientUserId(client, authUser),
name = authUser.username,
policies = emptySet(),
roles = emptySet(),
Expand All @@ -38,13 +37,13 @@ object DirectOAuthClientPrincipalConverter : OAuthClientPrincipalConverter {
}

/**
* format:{(client)authUser.getId()} as unique id.
* format: [OAuthUser.id]@[client] as unique id.
*
* @param client client
* @param authUser authUser
* @return unique id
*/
private fun getCoSecClientUserId(client: String, authUser: OAuthUser): String {
return format(client) + authUser.id
private fun asClientUserId(client: String, authUser: OAuthUser): String {
return authUser.id + "@" + client
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ package me.ahoo.cosec.oauth.client

import me.ahoo.cosec.oauth.OAuthUser
import me.ahoo.cosec.principal.CoSecPrincipal
import me.ahoo.cosec.util.Internals.format
import reactor.core.publisher.Mono

/**
Expand All @@ -27,7 +26,6 @@ fun interface OAuthClientPrincipalConverter {
fun convert(client: String, authUser: OAuthUser): Mono<CoSecPrincipal>

companion object {
@JvmField
val OAUTH_CLIENT = format("oauth_client")
const val OAUTH_CLIENT = "cosec_oauth_client"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package me.ahoo.cosec.oauth.client

import me.ahoo.cosec.oauth.OAuthUser
import me.ahoo.cosec.util.Internals
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.`is`
import org.junit.jupiter.api.Test
Expand All @@ -28,7 +27,7 @@ internal class DirectOAuthClientPrincipalConverterTest {
DirectOAuthClientPrincipalConverter.convert("clientId", authUser)
.test()
.consumeNextWith {
assertThat(it.id, `is`(Internals.format("clientId") + "id"))
assertThat(it.id, `is`("id@clientId"))
assertThat(it.name, `is`("username"))
}
.verifyComplete()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package me.ahoo.cosec.oauth.client
import io.mockk.every
import io.mockk.mockk
import me.ahoo.cosec.oauth.OAuthUser
import me.ahoo.cosec.util.Internals
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.`is`
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -48,8 +47,12 @@ internal class OAuthClientAuthenticationTest {
authentication.authenticate(OAuthClientCredentials("clientId"))
.test()
.consumeNextWith {
assertThat(it.id, `is`(Internals.format("clientId") + "id"))
assertThat(it.name, `is`("username"))
assertThat(
it.id, `is`("id@clientId")
)
assertThat(
it.name, `is`("username")
)
}
.verifyComplete()
}
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.5
version=0.8.6
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 40384d9

Please sign in to comment.