-
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.
support
CompositeAuthentication
(#23)
* support `CompositeAuthentication` * Improve code coverage : `CompositeAuthentication`
- Loading branch information
Showing
12 changed files
with
231 additions
and
8 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
38 changes: 38 additions & 0 deletions
38
cosec-core/src/main/kotlin/me/ahoo/cosec/authentication/CompositeAuthentication.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,38 @@ | ||
/* | ||
* 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.authentication | ||
|
||
import me.ahoo.cosec.api.authentication.Authentication | ||
import me.ahoo.cosec.api.authentication.AuthenticationProvider | ||
import me.ahoo.cosec.api.authentication.Credentials | ||
import me.ahoo.cosec.api.principal.CoSecPrincipal | ||
import reactor.core.publisher.Mono | ||
|
||
class CompositeAuthentication( | ||
private val authenticationProvider: AuthenticationProvider | ||
) : Authentication<Credentials, CoSecPrincipal> { | ||
override val supportCredentials: Class<Credentials> | ||
get() = Credentials::class.java | ||
|
||
override fun authenticate(credentials: Credentials): Mono<CoSecPrincipal> { | ||
val credentialsType = credentials.javaClass | ||
return authenticate(credentialsType, credentials) | ||
} | ||
|
||
fun authenticate(credentialsType: Class<out Credentials>, credentials: Credentials): Mono<CoSecPrincipal> { | ||
return authenticationProvider.getRequired<Credentials, CoSecPrincipal, Authentication<Credentials, CoSecPrincipal>>( | ||
credentialsType | ||
).authenticate(credentials) | ||
} | ||
} |
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
48 changes: 48 additions & 0 deletions
48
cosec-core/src/main/kotlin/me/ahoo/cosec/token/TokenCompositeAuthentication.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,48 @@ | ||
/* | ||
* 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.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.authentication.CompositeAuthentication | ||
import reactor.core.publisher.Mono | ||
|
||
class TokenCompositeAuthentication( | ||
private val compositeAuthentication: CompositeAuthentication, | ||
private val tokenConverter: TokenConverter | ||
) : Authentication<Credentials, CoSecPrincipal> { | ||
override val supportCredentials: Class<Credentials> | ||
get() = Credentials::class.java | ||
|
||
override fun authenticate(credentials: Credentials): Mono<CoSecPrincipal> { | ||
return authenticate(credentials.javaClass, credentials) | ||
} | ||
|
||
fun authenticate(credentialsType: Class<out Credentials>, credentials: Credentials): Mono<CoSecPrincipal> { | ||
return compositeAuthentication.authenticate(credentialsType, credentials) | ||
} | ||
|
||
fun authenticateAsToken(credentials: Credentials): Mono<CompositeToken> { | ||
return authenticateAsToken(credentials.javaClass, credentials) | ||
} | ||
|
||
fun authenticateAsToken(credentialsType: Class<out Credentials>, credentials: Credentials): Mono<CompositeToken> { | ||
return authenticate(credentialsType, credentials) | ||
.map { | ||
tokenConverter.asToken(it) | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
cosec-core/src/test/kotlin/me/ahoo/cosec/authentication/CompositeAuthenticationTest.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.authentication | ||
|
||
import io.mockk.every | ||
import io.mockk.mockk | ||
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.principal.SimplePrincipal | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.hamcrest.Matchers.* | ||
import org.junit.jupiter.api.Test | ||
import reactor.kotlin.core.publisher.toMono | ||
import reactor.kotlin.test.test | ||
|
||
class CompositeAuthenticationTest { | ||
private val compositeAuthentication = CompositeAuthentication(DefaultAuthenticationProvider) | ||
|
||
@Test | ||
fun getSupportCredentials() { | ||
assertThat(compositeAuthentication.supportCredentials, `is`(Credentials::class.java)) | ||
} | ||
|
||
@Test | ||
fun authenticate() { | ||
val credentials = mockk<Credentials>() | ||
val authentication = mockk<Authentication<Credentials, CoSecPrincipal>> { | ||
every { authenticate(any()) } returns SimplePrincipal.ANONYMOUS.toMono() | ||
} | ||
DefaultAuthenticationProvider.register(credentials.javaClass, authentication) | ||
|
||
compositeAuthentication.authenticate(credentials) | ||
.test() | ||
.expectNext(SimplePrincipal.ANONYMOUS) | ||
.verifyComplete() | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
cosec-core/src/test/kotlin/me/ahoo/cosec/token/TokenCompositeAuthenticationTest.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,54 @@ | ||
/* | ||
* 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.token | ||
|
||
import io.mockk.every | ||
import io.mockk.mockk | ||
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.authentication.CompositeAuthentication | ||
import me.ahoo.cosec.authentication.DefaultAuthenticationProvider | ||
import me.ahoo.cosec.principal.SimplePrincipal | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.hamcrest.Matchers.* | ||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.jupiter.api.Test | ||
import reactor.kotlin.core.publisher.toMono | ||
import reactor.kotlin.test.test | ||
|
||
class TokenCompositeAuthenticationTest { | ||
|
||
@Test | ||
fun authenticateAsToken() { | ||
val compositeAuthentication = CompositeAuthentication(DefaultAuthenticationProvider) | ||
val compositeToken = SimpleCompositeToken("accessToken", "refreshToken") | ||
val tokenConverter = mockk<TokenConverter>() { | ||
every { asToken(any()) } returns compositeToken | ||
} | ||
val tokenCompositeAuthentication = TokenCompositeAuthentication(compositeAuthentication, tokenConverter) | ||
assertThat(tokenCompositeAuthentication.supportCredentials, `is`(Credentials::class.java)) | ||
|
||
val credentials = mockk<Credentials>() | ||
val authentication = mockk<Authentication<Credentials, CoSecPrincipal>> { | ||
every { authenticate(any()) } returns SimplePrincipal.ANONYMOUS.toMono() | ||
} | ||
DefaultAuthenticationProvider.register(credentials.javaClass, authentication) | ||
|
||
tokenCompositeAuthentication.authenticateAsToken(credentials) | ||
.test() | ||
.expectNext(compositeToken) | ||
.verifyComplete() | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,4 +38,4 @@ class ReactiveSecurityContextsTest { | |
.then() | ||
.verifyComplete() | ||
} | ||
} | ||
} |
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