OAuth Server Mock
OAuth2 mocked server for unit testing
Features
- Method for signed JWT creation, with user defined claims
- Http endpoint for retrieval of public keys
Requirements
Quick Start
Gradle
testImplementation("net.uiqui:mock-oauth-server:1.1.1")
Maven
<dependency>
<groupId>net.uiqui</groupId>
<artifactId>mock-oauth-server</artifactId>
<version>1.1.1</version>
<scope>test</scope>
</dependency>
How to Use
- Create an instance of the OAuthServerMock
private val mockedOauthServer = OAuthServerMock()
- Start the server
@BeforeAll
@JvmStatic
fun init() {
mockedOauthServer.start()
}
- Tell your app from where it can download the signing keys
@BeforeEach
fun setUp() {
every { mockedAuthenticationConfig.jwksEndpoint } returns mockedOauthServer.getJwksUri()
}
- Generate a JWT with the required claims
val requiredClaims = mapOf(
"iss" to "OAuth-Server-Mock",
"aud" to "this-unit-test",
"appid" to "ad4fc666-c793-11ec-9d64-0242ac120002"
)
val jwtToken = mockedOauthServer.generateJWT(requiredClaims)
- Use the JWT on your request
mockMvc.perform(
get("/your/endpoint")
.header(AUTHORIZATION, "Bearer $jwtToken")
)
- Shutdown the server
@AfterAll
@JvmStatic
fun cleanUp() {
mockedOauthServer.shutdown()
}
You can find an example of an application using Spring Boot Security and mock-oauth-server here
License
This project is licensed under the terms of the MIT license