https://kotlinlang.org logo
#ktor
Title
# ktor
t

Tucker Barbour

06/06/2022, 8:01 PM
I had a working implementation of a ktor server application using oauth where the oauth provider was selected based on the url path. I was able to accomplish this using the now deprecated Locations plugin. I’m reading through the source for the newer Resource plugin but I’m having trouble accomplishing the same thing.
Copy code
@Location("/") class index
@Location("/auth/{type?}") class login(val type: String = "")
@Location("/auth/{type}/callback") class callback(val type: String)

val loginProviders = listOf(
  OAuthServerSettings.OAuth2ServerSettings(..),
  OAuthServerSettings.OAuth2ServerSettings(..)
).associateBy { it.name }

fun Application.module() {
  install(Authentication) {
    oauth("oauth") {
      providerLookup = {
        loginProviders[application.locations.resolve<login>(login::class, this).type]
      }
      urlProvider = { p -> redirectUrl(callback(p.name), false) }
    }
  }
}
This is the gist with a bunch of other stuff left out. The key is begin able to map the url path to the name of the login provider. This ultimately allows a client to select which oauth provider to use. Any help or guidance would be appreciated.
a

Aleksei Tirman [JB]

06/07/2022, 8:53 AM
You can do it with the following code:
Copy code
providerLookup = {
    val resources = application.plugin(Resources)
    val resource = resources.resourcesFormat.decodeFromParameters(serializer<login>(), parameters)
    loginProviders[resource.type]
}
t

Tucker Barbour

06/07/2022, 9:33 AM
I’ll give that a go, thanks!
3 Views