Add /auth endpoint
This commit is contained in:
parent
942fb1a1ec
commit
d9346a29f7
@ -27,7 +27,8 @@ lateinit var kodein: Kodein
|
|||||||
|
|
||||||
@Suppress("unused") // Referenced in application.conf
|
@Suppress("unused") // Referenced in application.conf
|
||||||
fun Application.module() {
|
fun Application.module() {
|
||||||
features()
|
// must be first to be loaded
|
||||||
|
configurationFeature()
|
||||||
|
|
||||||
kodein = Kodein {
|
kodein = Kodein {
|
||||||
import(controllerModule)
|
import(controllerModule)
|
||||||
@ -36,6 +37,8 @@ fun Application.module() {
|
|||||||
bind<Feature>() with singleton { Migration(this.kodein) }
|
bind<Feature>() with singleton { Migration(this.kodein) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
features()
|
||||||
|
|
||||||
log.debug(kodein.container.tree.bindings.description())
|
log.debug(kodein.container.tree.bindings.description())
|
||||||
|
|
||||||
val feature: Feature by kodein.instance()
|
val feature: Feature by kodein.instance()
|
||||||
|
|||||||
10
api/src/auth/UsernamePasswordCredential.kt
Normal file
10
api/src/auth/UsernamePasswordCredential.kt
Normal 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
|
||||||
@ -13,4 +13,5 @@ val controllerModule = Kodein.Module(name = "Controller") {
|
|||||||
bind() from setBinding<KodeinController>()
|
bind() from setBinding<KodeinController>()
|
||||||
|
|
||||||
bind<KodeinController>().inSet() with singleton { TestController(this.kodein) }
|
bind<KodeinController>().inSet() with singleton { TestController(this.kodein) }
|
||||||
|
bind<KodeinController>().inSet() with singleton { UserController(this.kodein) }
|
||||||
}
|
}
|
||||||
33
api/src/controllers/UserController.kt
Normal file
33
api/src/controllers/UserController.kt
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -6,9 +6,6 @@ import org.kodein.di.Kodein
|
|||||||
import org.kodein.di.KodeinAware
|
import org.kodein.di.KodeinAware
|
||||||
|
|
||||||
fun Application.features() {
|
fun Application.features() {
|
||||||
// must be first to be loaded
|
|
||||||
configurationFeature()
|
|
||||||
|
|
||||||
locationFeature()
|
locationFeature()
|
||||||
corsFeature()
|
corsFeature()
|
||||||
contentNegotiationFeature()
|
contentNegotiationFeature()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user