@@ -9,29 +9,31 @@ import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
99import io.ktor.http.ContentType
1010import io.ktor.http.HttpMethod
1111import io.ktor.http.HttpStatusCode
12+ import io.ktor.http.encodedPath
13+ import io.ktor.resources.Resource
1214import io.ktor.serialization.jackson.jackson
1315import io.ktor.server.application.Application
1416import io.ktor.server.application.ApplicationCall
15- import io.ktor.server.application.call
1617import io.ktor.server.application.install
18+ import io.ktor.server.application.log
1719import io.ktor.server.auth.Authentication
1820import io.ktor.server.auth.OAuthAccessTokenResponse
1921import io.ktor.server.auth.OAuthServerSettings
2022import io.ktor.server.auth.authenticate
2123import io.ktor.server.auth.authentication
2224import io.ktor.server.auth.oauth
2325import io.ktor.server.engine.embeddedServer
24- import io.ktor.server.locations.KtorExperimentalLocationsAPI
25- import io.ktor.server.locations.Location
26- import io.ktor.server.locations.Locations
27- import io.ktor.server.locations.location
28- import io.ktor.server.locations.locations
29- import io.ktor.server.locations.url
3026import io.ktor.server.netty.Netty
27+ import io.ktor.server.resources.Resources
28+ import io.ktor.server.resources.href
29+ import io.ktor.server.resources.resource
3130import io.ktor.server.response.respondText
31+ import io.ktor.server.routing.application
3232import io.ktor.server.routing.get
3333import io.ktor.server.routing.param
3434import io.ktor.server.routing.routing
35+ import io.ktor.server.util.url
36+ import kotlinx.serialization.Serializable
3537
3638fun main () {
3739 embeddedServer(Netty , port = 8080 ) {
@@ -54,48 +56,57 @@ fun main() {
5456 }.start(true )
5557}
5658
57- @OptIn(KtorExperimentalLocationsAPI ::class )
5859fun Application.module (authConfig : AuthConfig ) {
5960 val idProviders = authConfig.providers.map { it.settings }.associateBy { it.name }
6061
61- install(Locations )
62+ install(Resources )
6263 install(Authentication ) {
6364 oauth(" oauth2" ) {
6465 client = httpClient
6566 providerLookup = {
66- idProviders[application.locations.resolve<Login >(Login ::class , this ).type] ? : idProviders.values.first()
67+ val t = this .parameters[" type" ].orEmpty()
68+ idProviders[t] ? : idProviders.values.first()
6769 }
6870 urlProvider = {
69- url(Login (it.name))
71+ url {
72+ encodedPath = application.href(Login (it.name))
73+ }
7074 }
7175 }
7276 }
7377
7478 routing {
79+ trace { application.log.info(it.buildText()) }
7580 authenticate(" oauth2" ) {
7681 get {
7782 call.respondText(" nothing to see here really" )
7883 }
79- location<Login > {
84+ resource<Login > {
85+ // /login/{type}?error=...
8086 param(" error" ) {
81- handle {
82- call.respondText(ContentType .Text .Html , HttpStatusCode .BadRequest ) {
87+ get {
88+ call.respondText(
89+ ContentType .Text .Html ,
90+ HttpStatusCode .BadRequest
91+ ) {
8392 " received error on login: ${call.parameters.getAll(" error" ).orEmpty()} "
8493 }
8594 }
8695 }
87- handle {
96+ // /login/{type}
97+ get {
8898 call.respondText(" welcome ${call.subject()} " )
8999 }
90100 }
101+
91102 }
92103 }
93104}
94105
95- @Location( " /login/{type?} " )
96- @OptIn( KtorExperimentalLocationsAPI :: class )
106+ @Serializable
107+ @Resource( " /login/{type?} " )
97108class Login (
98- val type : String = " " ,
109+ val type : String? = " " ,
99110)
100111
101112class AuthConfig (
0 commit comments