-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
AttributeValue
for CoSecPrincipal.attributes (#120)
- Loading branch information
Showing
23 changed files
with
260 additions
and
30 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
22 changes: 22 additions & 0 deletions
22
cosec-api/src/main/kotlin/me/ahoo/cosec/api/principal/AttributeValue.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,22 @@ | ||
/* | ||
* 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.api.principal | ||
|
||
interface AttributeValue<V> { | ||
val value: V | ||
|
||
fun asString(): String | ||
|
||
fun <T> asObject(objectClass: Class<T>): T | ||
} |
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
29 changes: 29 additions & 0 deletions
29
cosec-core/src/main/kotlin/me/ahoo/cosec/context/RequestSecurityContexts.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,29 @@ | ||
/* | ||
* 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.context | ||
|
||
import me.ahoo.cosec.api.context.SecurityContext | ||
import me.ahoo.cosec.api.context.request.Request | ||
|
||
object RequestSecurityContexts { | ||
const val KEY = "COSEC_SECURITY_CONTEXT_REQUEST" | ||
|
||
fun <R : Request> SecurityContext.setRequest(request: R) { | ||
setAttributeValue(KEY, request) | ||
} | ||
|
||
fun <R : Request> SecurityContext.getRequest(): R? { | ||
return getAttributeValue<R>(KEY) | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
cosec-core/src/main/kotlin/me/ahoo/cosec/principal/ObjectAttributeValue.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,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.principal | ||
|
||
import me.ahoo.cosec.api.principal.AttributeValue | ||
import me.ahoo.cosec.serialization.CoSecJsonSerializer | ||
|
||
class ObjectAttributeValue<V>(override val value: V) : AttributeValue<V> { | ||
companion object { | ||
@Suppress("UNCHECKED_CAST") | ||
fun <V> V.asAttributeValue(): AttributeValue<V> { | ||
return when (this) { | ||
is AttributeValue<*> -> this | ||
is String -> TextAttributeValue(this) | ||
else -> ObjectAttributeValue(this) | ||
} as AttributeValue<V> | ||
} | ||
} | ||
|
||
override fun asString(): String { | ||
return CoSecJsonSerializer.writeValueAsString(value) | ||
} | ||
|
||
override fun <T> asObject(objectClass: Class<T>): T { | ||
@Suppress("UNCHECKED_CAST") | ||
return value as T | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
cosec-core/src/main/kotlin/me/ahoo/cosec/principal/TextAttributeValue.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,30 @@ | ||
/* | ||
* 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.principal | ||
|
||
import me.ahoo.cosec.api.principal.AttributeValue | ||
import me.ahoo.cosec.serialization.CoSecJsonSerializer | ||
|
||
class TextAttributeValue( | ||
override val value: String | ||
) : AttributeValue<String> { | ||
|
||
override fun asString(): String { | ||
return value | ||
} | ||
|
||
override fun <T> asObject(objectClass: Class<T>): T { | ||
return CoSecJsonSerializer.readValue(value, objectClass) | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
cosec-core/src/test/kotlin/me/ahoo/cosec/principal/ObjectAttributeValueTest.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,49 @@ | ||
/* | ||
* 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.principal | ||
|
||
import me.ahoo.cosec.principal.ObjectAttributeValue.Companion.asAttributeValue | ||
import org.junit.jupiter.api.Test | ||
|
||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.hamcrest.Matchers.* | ||
|
||
class ObjectAttributeValueTest { | ||
|
||
@Test | ||
fun duplicateAsAttributeValue() { | ||
val attributeValue = "value".asAttributeValue() | ||
assertThat(attributeValue, sameInstance(attributeValue.asAttributeValue())) | ||
} | ||
|
||
@Test | ||
fun stringAsAttributeValue() { | ||
val value = "1" | ||
val attributeValue = value.asAttributeValue() | ||
assertThat(attributeValue, instanceOf(TextAttributeValue::class.java)) | ||
assertThat(attributeValue.value, equalTo(value)) | ||
assertThat(attributeValue.asString(), equalTo(value)) | ||
assertThat(attributeValue.asObject(Int::class.java), equalTo(1)) | ||
} | ||
|
||
@Test | ||
fun objectAsAttributeValue() { | ||
val value = this | ||
val attributeValue = value.asAttributeValue() | ||
assertThat(attributeValue, instanceOf(ObjectAttributeValue::class.java)) | ||
assertThat(attributeValue.value, equalTo(value)) | ||
assertThat(attributeValue.asString(), equalTo("{}")) | ||
assertThat(attributeValue.asObject(ObjectAttributeValueTest::class.java), equalTo(this)) | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
cosec-jwt/src/main/kotlin/me/ahoo/cosec/jwt/ClaimAttributeValue.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,33 @@ | ||
/* | ||
* 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.jwt | ||
|
||
import com.auth0.jwt.interfaces.Claim | ||
import me.ahoo.cosec.api.principal.AttributeValue | ||
|
||
class ClaimAttributeValue(override val value: Claim) : AttributeValue<Claim> { | ||
companion object { | ||
fun Claim.asClaimAttributeValue(): ClaimAttributeValue { | ||
return ClaimAttributeValue(this) | ||
} | ||
} | ||
|
||
override fun asString(): String { | ||
return value.asString() | ||
} | ||
|
||
override fun <T> asObject(objectClass: Class<T>): T { | ||
return value.`as`(objectClass) | ||
} | ||
} |
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.