navikt / mock-oauth2-server

A scriptable/customizable web server for testing HTTP clients using OAuth2/OpenID Connect or applications with a dependency to a running OAuth2 server (i.e. APIs requiring signed JWTs from a known issuer)
MIT License
223 stars 54 forks source link

`OAuth2TokenProvider` should allow a flexible `systemTime` #691

Closed xuanswe closed 2 weeks ago

xuanswe commented 1 month ago

A global fixed systemTime is not enough to write tests in practice at all.

I propose to change the current implementation

class OAuth2TokenProvider(
        private val keyProvider: KeyProvider = KeyProvider(),
        val systemTime: Instant? = null,
    )

to the backward compatible implemenation like below

typealias TimeProvider = () -> Instant?

class OAuth2TokenProvider(
  private val keyProvider: KeyProvider = KeyProvider(),
  private val timeProvider: TimeProvider,
) {
  val systemTime
    get() = timeProvider()

  @JvmOverloads
  constructor(
    keyProvider: KeyProvider = KeyProvider(),
    systemTime: Instant? = null,
  ) : this(keyProvider, { systemTime })
}

I am not sure if we need to adapt anywhere else (ex. OAuth2TokenProviderDeserializer) with this change or not.

xuanswe commented 1 month ago

@ybelMekk do you agree so that I can contribute a PR?

xuanswe commented 1 month ago

From my understanding, OAuth2TokenProvider is created only in 2 places:

xuanswe commented 1 month ago

@ybelMekk PR created :)

xuanswe commented 2 weeks ago

Hi, could you please review the PR?

ybelMekk commented 2 weeks ago

Could u please run :formatKotlin and then push the changes?

xuanswe commented 2 weeks ago

@ybelMekk Thanks for merging and releasing! :)