Skip to content

Commit

Permalink
Enhance Authentication generics API (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang authored Dec 14, 2022
1 parent f6b2611 commit dcd1b34
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import reactor.core.publisher.Mono
*
* @author ahoo wang
*/
interface Authentication<C : Credentials, P : CoSecPrincipal> {
interface Authentication<C : Credentials, out P : CoSecPrincipal> {
val supportCredentials: Class<C>
fun authenticate(credentials: C): Mono<P>
fun authenticate(credentials: C): Mono<out P>
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ class CompositeAuthentication(
override val supportCredentials: Class<Credentials>
get() = Credentials::class.java

override fun authenticate(credentials: Credentials): Mono<CoSecPrincipal> {
override fun authenticate(credentials: Credentials): Mono<out CoSecPrincipal> {
val credentialsType = credentials.javaClass
return authenticate(credentialsType, credentials)
}

fun authenticate(credentialsType: Class<out Credentials>, credentials: Credentials): Mono<CoSecPrincipal> {
fun authenticate(credentialsType: Class<out Credentials>, credentials: Credentials): Mono<out CoSecPrincipal> {
return authenticationProvider.getRequired<Credentials, CoSecPrincipal, Authentication<Credentials, CoSecPrincipal>>(
credentialsType
).authenticate(credentials)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,25 @@
package me.ahoo.cosec.authentication.token

import me.ahoo.cosec.api.authentication.Authentication
import me.ahoo.cosec.api.authentication.Credentials
import me.ahoo.cosec.api.principal.CoSecPrincipal
import me.ahoo.cosec.api.token.CompositeToken
import me.ahoo.cosec.api.token.TokenPrincipal
import me.ahoo.cosec.token.TokenVerifier
import reactor.core.publisher.Mono
import reactor.kotlin.core.publisher.toMono

abstract class AbstractRefreshTokenAuthentication<C : RefreshTokenCredentials, P : TokenPrincipal>(
abstract class AbstractRefreshTokenAuthentication<C : RefreshTokenCredentials, out P : CoSecPrincipal>(
override val supportCredentials: Class<C>
) : Authentication<C, P>

class SimpleRefreshTokenAuthentication(private val tokenVerifier: TokenVerifier) :
AbstractRefreshTokenAuthentication<RefreshTokenCredentials, TokenPrincipal>(RefreshTokenCredentials::class.java) {
override fun authenticate(credentials: RefreshTokenCredentials): Mono<TokenPrincipal> {
AbstractRefreshTokenAuthentication<RefreshTokenCredentials, CoSecPrincipal>(RefreshTokenCredentials::class.java) {
override fun authenticate(credentials: RefreshTokenCredentials): Mono<CoSecPrincipal> {
return Mono.defer {
tokenVerifier.refresh<TokenPrincipal>(credentials).toMono()
}
}
}

interface RefreshTokenCredentials : Credentials, CompositeToken
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package me.ahoo.cosec.authentication.token

import me.ahoo.cosec.api.authentication.Authentication
import me.ahoo.cosec.api.authentication.Credentials
import me.ahoo.cosec.api.principal.CoSecPrincipal
import me.ahoo.cosec.api.principal.TenantPrincipal
import reactor.core.publisher.Mono
Expand All @@ -27,7 +28,7 @@ abstract class AbstractSwitchTenantAuthentication : Authentication<SwitchTenantC
override val supportCredentials: Class<SwitchTenantCredentials>
get() = SwitchTenantCredentials::class.java

override fun authenticate(credentials: SwitchTenantCredentials): Mono<TenantPrincipal> {
override fun authenticate(credentials: SwitchTenantCredentials): Mono<out TenantPrincipal> {
return switchTenant(credentials.targetTenantId, credentials.principal)
}

Expand All @@ -38,5 +39,18 @@ abstract class AbstractSwitchTenantAuthentication : Authentication<SwitchTenantC
* @param previousPrincipal previous principal
* @return new target tenant context principal
*/
protected abstract fun switchTenant(targetTenantId: String, previousPrincipal: CoSecPrincipal): Mono<TenantPrincipal>
protected abstract fun switchTenant(
targetTenantId: String,
previousPrincipal: CoSecPrincipal
): Mono<out TenantPrincipal>
}

/**
* Switch Tenant Credentials .
*
* @author ahoo wang
*/
interface SwitchTenantCredentials : Credentials {
val targetTenantId: String
val principal: CoSecPrincipal
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,22 @@ class TokenCompositeAuthentication(
override val supportCredentials: Class<Credentials>
get() = Credentials::class.java

override fun authenticate(credentials: Credentials): Mono<CoSecPrincipal> {
override fun authenticate(credentials: Credentials): Mono<out CoSecPrincipal> {
return authenticate(credentials.javaClass, credentials)
}

fun authenticate(credentialsType: Class<out Credentials>, credentials: Credentials): Mono<CoSecPrincipal> {
fun authenticate(credentialsType: Class<out Credentials>, credentials: Credentials): Mono<out CoSecPrincipal> {
return compositeAuthentication.authenticate(credentialsType, credentials)
}

fun authenticateAsToken(credentials: Credentials): Mono<CompositeToken> {
fun authenticateAsToken(credentials: Credentials): Mono<out CompositeToken> {
return authenticateAsToken(credentials.javaClass, credentials)
}

fun authenticateAsToken(credentialsType: Class<out Credentials>, credentials: Credentials): Mono<CompositeToken> {
fun authenticateAsToken(
credentialsType: Class<out Credentials>,
credentials: Credentials
): Mono<out CompositeToken> {
return authenticate(credentialsType, credentials)
.map {
tokenConverter.asToken(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ class CompositeAuthenticationTest {

compositeAuthentication.authenticate(credentials)
.test()
.expectNext(SimplePrincipal.ANONYMOUS)
.consumeNextWith {
assertThat(it, `is`(SimplePrincipal.ANONYMOUS))
}
.verifyComplete()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
package me.ahoo.cosec.authentication

import io.mockk.mockk
import me.ahoo.cosec.api.token.TokenPrincipal
import me.ahoo.cosec.api.principal.CoSecPrincipal
import me.ahoo.cosec.authentication.token.RefreshTokenCredentials
import me.ahoo.cosec.authentication.token.SimpleRefreshTokenAuthentication
import org.hamcrest.MatcherAssert.assertThat
Expand All @@ -27,14 +27,14 @@ internal class DefaultAuthenticationProviderTest {
@Test
fun register() {
Assertions.assertThrows(IllegalArgumentException::class.java) {
DefaultAuthenticationProvider.getRequired<RefreshTokenCredentials, TokenPrincipal, SimpleRefreshTokenAuthentication>(
DefaultAuthenticationProvider.getRequired<RefreshTokenCredentials, CoSecPrincipal, SimpleRefreshTokenAuthentication>(
RefreshTokenCredentials::class.java
)
}
val refreshTokenAuthentication = SimpleRefreshTokenAuthentication(mockk())
DefaultAuthenticationProvider.register(refreshTokenAuthentication)
assertThat(
DefaultAuthenticationProvider.getRequired<RefreshTokenCredentials, TokenPrincipal, SimpleRefreshTokenAuthentication>(
DefaultAuthenticationProvider.getRequired<RefreshTokenCredentials, CoSecPrincipal, SimpleRefreshTokenAuthentication>(
RefreshTokenCredentials::class.java
),
`is`(refreshTokenAuthentication)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ class TokenCompositeAuthenticationTest {

tokenCompositeAuthentication.authenticateAsToken(credentials)
.test()
.expectNext(compositeToken)
.consumeNextWith {
assertThat(it, `is`(compositeToken))
}
.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.3.0
version=1.4.0
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 dcd1b34

Please sign in to comment.