From 2a583ed3991cfc543e5661f668ad3f66e81005bf Mon Sep 17 00:00:00 2001 From: Hubert Van De Walle Date: Mon, 20 Apr 2020 16:26:19 +0200 Subject: [PATCH] Split user controller into register and login --- api/src/controllers/Controllers.kt | 3 +- api/src/controllers/LoginController.kt | 39 ++++++++++++++ api/src/controllers/RegisterController.kt | 27 +++++++++- api/src/controllers/UserController.kt | 66 ----------------------- 4 files changed, 67 insertions(+), 68 deletions(-) create mode 100644 api/src/controllers/LoginController.kt delete mode 100644 api/src/controllers/UserController.kt diff --git a/api/src/controllers/Controllers.kt b/api/src/controllers/Controllers.kt index 8bf78f7..0ed5c15 100644 --- a/api/src/controllers/Controllers.kt +++ b/api/src/controllers/Controllers.kt @@ -13,7 +13,8 @@ import org.kodein.di.generic.singleton val controllerModule = Kodein.Module(name = "Controller") { bind() from setBinding() - bind().inSet() with singleton { UserController(this.kodein) } + bind().inSet() with singleton { RegisterController(this.kodein) } + bind().inSet() with singleton { LoginController(this.kodein) } bind().inSet() with singleton { NotesController(this.kodein) } bind().inSet() with singleton { TitleController(this.kodein) } } \ No newline at end of file diff --git a/api/src/controllers/LoginController.kt b/api/src/controllers/LoginController.kt new file mode 100644 index 0000000..176faf9 --- /dev/null +++ b/api/src/controllers/LoginController.kt @@ -0,0 +1,39 @@ +package be.vandewalleh.controllers + +import be.vandewalleh.auth.SimpleJWT +import be.vandewalleh.auth.UsernamePasswordCredential +import be.vandewalleh.controllers.base.KodeinController +import be.vandewalleh.services.UserService +import io.ktor.application.call +import io.ktor.http.HttpStatusCode +import io.ktor.request.receive +import io.ktor.response.respond +import io.ktor.routing.Routing +import io.ktor.routing.post +import org.kodein.di.Kodein +import org.kodein.di.generic.instance +import org.mindrot.jbcrypt.BCrypt + +class LoginController(kodein: Kodein) : KodeinController("/login", kodein) { + private val simpleJwt by instance() + private val userService by instance() + + data class TokenResponse(val token: String) + + override fun Routing.routes() { + post { + + val credential = call.receive() + + val (email, password) = userService.getEmailAndPasswordFromUsername(credential.username) + ?: return@post call.respondStatus(HttpStatusCode.Unauthorized) + + + if (!BCrypt.checkpw(credential.password, password)) { + return@post call.respondStatus(HttpStatusCode.Unauthorized) + } + + return@post call.respond(TokenResponse(simpleJwt.sign(email))) + } + } +} \ No newline at end of file diff --git a/api/src/controllers/RegisterController.kt b/api/src/controllers/RegisterController.kt index f0b1b5d..93aba9e 100644 --- a/api/src/controllers/RegisterController.kt +++ b/api/src/controllers/RegisterController.kt @@ -1,10 +1,35 @@ package be.vandewalleh.controllers import be.vandewalleh.controllers.base.KodeinController +import be.vandewalleh.services.UserRegistrationDto +import be.vandewalleh.services.UserService +import io.ktor.application.call +import io.ktor.http.HttpStatusCode +import io.ktor.request.receive +import io.ktor.response.respond import io.ktor.routing.Routing +import io.ktor.routing.post import org.kodein.di.Kodein +import org.kodein.di.generic.instance +import org.mindrot.jbcrypt.BCrypt + +class RegisterController(kodein: Kodein) : KodeinController("/register", kodein) { + private val userService by instance() -class RegisterController(kodein: Kodein) : KodeinController("", kodein) { override fun Routing.routes() { + post { + val user = call.receive() + + if (userService.userExists(user.username, user.email)) + return@post call.respond(HttpStatusCode.Conflict) + + val hashedPassword = BCrypt.hashpw(user.password, BCrypt.gensalt()) + + userService.createUser( + UserRegistrationDto(user.username, user.email, hashedPassword) + ) + + return@post call.respondStatus(HttpStatusCode.Created) + } } } \ No newline at end of file diff --git a/api/src/controllers/UserController.kt b/api/src/controllers/UserController.kt deleted file mode 100644 index 25d3bd0..0000000 --- a/api/src/controllers/UserController.kt +++ /dev/null @@ -1,66 +0,0 @@ -package be.vandewalleh.controllers - -import be.vandewalleh.auth.SimpleJWT -import be.vandewalleh.auth.UsernamePasswordCredential -import be.vandewalleh.controllers.base.KodeinController -import be.vandewalleh.services.UserRegistrationDto -import be.vandewalleh.services.UserService -import io.ktor.application.call -import io.ktor.http.HttpStatusCode -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 -import org.mindrot.jbcrypt.BCrypt - -class UserController(kodein: Kodein) : KodeinController(kodein) { - private val simpleJwt by instance() - private val userService by instance() - - override fun Routing.registerRoutes() { - post { - data class Response(val token: String) - - val credential = call.receive() - - val (email, password) = userService.getEmailAndPasswordFromUsername(credential.username) - ?: return@post call.respondStatus(HttpStatusCode.Unauthorized) - - - if (!BCrypt.checkpw(credential.password, password)) { - return@post call.respondStatus(HttpStatusCode.Unauthorized) - } - - return@post call.respond(Response(simpleJwt.sign(email))) - } - - post { - data class Response(val message: String) - - val user = call.receive() - - if (userService.userExists(user.username, user.email)) - return@post call.respond(HttpStatusCode.Conflict) - - val hashedPassword = BCrypt.hashpw(user.password, BCrypt.gensalt()) - - userService.createUser( - UserRegistrationDto(user.username, user.email, hashedPassword) - ) - - return@post call.respondStatus(HttpStatusCode.Created) - } - } - - object Routes { - @Location("/login") - class Login - - @Location("/register") - class Register - - } -} \ No newline at end of file