Add /auth endpoint

This commit is contained in:
Hubert Van De Walle 2020-04-12 01:22:01 +02:00
parent 942fb1a1ec
commit d9346a29f7
5 changed files with 48 additions and 4 deletions

View File

@ -27,7 +27,8 @@ lateinit var kodein: Kodein
@Suppress("unused") // Referenced in application.conf
fun Application.module() {
features()
// must be first to be loaded
configurationFeature()
kodein = Kodein {
import(controllerModule)
@ -36,6 +37,8 @@ fun Application.module() {
bind<Feature>() with singleton { Migration(this.kodein) }
}
features()
log.debug(kodein.container.tree.bindings.description())
val feature: Feature by kodein.instance()

View File

@ -0,0 +1,10 @@
package be.vandewalleh.auth
import io.ktor.auth.Credential
/**
* Represents a simple user [username] and [password] credential pair
* @property username
* @property password
*/
data class UsernamePasswordCredential(val username: String, val password: String) : Credential

View File

@ -13,4 +13,5 @@ val controllerModule = Kodein.Module(name = "Controller") {
bind() from setBinding<KodeinController>()
bind<KodeinController>().inSet() with singleton { TestController(this.kodein) }
bind<KodeinController>().inSet() with singleton { UserController(this.kodein) }
}

View File

@ -0,0 +1,33 @@
package be.vandewalleh.controllers
import be.vandewalleh.auth.SimpleJWT
import be.vandewalleh.auth.UsernamePasswordCredential
import io.ktor.application.call
import io.ktor.locations.Location
import io.ktor.locations.post
import io.ktor.request.receive
import io.ktor.response.respond
import io.ktor.routing.Routing
import org.kodein.di.Kodein
import org.kodein.di.generic.instance
class UserController(kodein: Kodein) : KodeinController(kodein) {
private val simpleJwt by instance<SimpleJWT>()
override fun Routing.registerRoutes() {
post<Routes.Auth> {
val post = call.receive<UsernamePasswordCredential>()
// TODO check db
if (post.username != "test" || post.password != "test")
error("Invalid Credentials")
call.respond(mapOf("token" to simpleJwt.sign("test@test.be")))
}
}
object Routes {
@Location("/auth")
class Auth
}
}

View File

@ -6,9 +6,6 @@ import org.kodein.di.Kodein
import org.kodein.di.KodeinAware
fun Application.features() {
// must be first to be loaded
configurationFeature()
locationFeature()
corsFeature()
contentNegotiationFeature()