Skip to content

Latest commit

 

History

History
90 lines (74 loc) · 1.66 KB

README.md

File metadata and controls

90 lines (74 loc) · 1.66 KB

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

  • Java >= 11

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

  1. Create an instance of the OAuthServerMock
private val mockedOauthServer = OAuthServerMock()
  1. Start the server
@BeforeAll
@JvmStatic
fun init() {
    mockedOauthServer.start()
}
  1. Tell your app from where it can download the signing keys
@BeforeEach
fun setUp() {
    every { mockedAuthenticationConfig.jwksEndpoint } returns mockedOauthServer.getJwksUri()
}
  1. 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)
  1. Use the JWT on your request
mockMvc.perform(
    get("/your/endpoint")
        .header(AUTHORIZATION, "Bearer $jwtToken")
)
  1. 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