Performance improvements

This commit is contained in:
2020-06-14 15:31:36 +02:00
parent 8a9e878d5f
commit f40349ea98
9 changed files with 85 additions and 57 deletions
+11 -10
View File
@@ -1,6 +1,7 @@
package be.vandewalleh.routing
import be.vandewalleh.auth.SimpleJWT
import be.vandewalleh.auth.UserDbIdPrincipal
import be.vandewalleh.auth.UsernamePasswordCredential
import be.vandewalleh.extensions.respondStatus
import be.vandewalleh.services.UserService
@@ -26,16 +27,16 @@ fun Routing.auth(kodein: Kodein) {
post("/user/login") {
val credential = call.receive<UsernamePasswordCredential>()
val (email, password) = userService.getEmailAndPasswordFromUsername(credential.username)
val user = userService.getFromUsername(credential.username)
?: return@post call.respondStatus(HttpStatusCode.Unauthorized)
if (!BCrypt.checkpw(credential.password, password)) {
if (!BCrypt.checkpw(credential.password, user.password)) {
return@post call.respondStatus(HttpStatusCode.Unauthorized)
}
val response = DualToken(
token = authSimpleJwt.sign(email),
refreshToken = refreshSimpleJwt.sign(email)
token = authSimpleJwt.sign(user.id),
refreshToken = refreshSimpleJwt.sign(user.id)
)
return@post call.respond(response)
}
@@ -43,16 +44,16 @@ fun Routing.auth(kodein: Kodein) {
post("/user/refresh_token") {
val token = call.receive<RefreshToken>().refreshToken
val email = try {
val id = try {
val decodedJWT = refreshSimpleJwt.verifier.verify(token)
decodedJWT.getClaim("email").asString()
decodedJWT.getClaim("id").asInt()
} catch (e: JWTVerificationException) {
return@post call.respondStatus(HttpStatusCode.Unauthorized)
}
val response = DualToken(
token = authSimpleJwt.sign(email),
refreshToken = refreshSimpleJwt.sign(email)
token = authSimpleJwt.sign(id),
refreshToken = refreshSimpleJwt.sign(id)
)
return@post call.respond(response)
}
@@ -60,8 +61,8 @@ fun Routing.auth(kodein: Kodein) {
authenticate {
get("/user/me") {
// retrieve email from token
val email = call.principal<UserIdPrincipal>()!!.name
val info = userService.getUserInfo(email)
val id = call.principal<UserDbIdPrincipal>()!!.id
val info = userService.getUserInfo(id)
if (info != null) call.respond(mapOf("user" to info))
else call.respondStatus(HttpStatusCode.Unauthorized)
}
+6 -8
View File
@@ -5,10 +5,10 @@ import be.vandewalleh.extensions.receiveNotePatch
import be.vandewalleh.extensions.respondStatus
import be.vandewalleh.extensions.userId
import be.vandewalleh.services.NotesService
import io.ktor.application.*
import io.ktor.auth.*
import io.ktor.http.*
import io.ktor.response.*
import io.ktor.application.call
import io.ktor.auth.authenticate
import io.ktor.http.HttpStatusCode
import io.ktor.response.respond
import io.ktor.routing.*
import org.kodein.di.Kodein
import org.kodein.di.generic.instance
@@ -22,10 +22,8 @@ fun Routing.title(kodein: Kodein) {
val userId = call.userId()
val noteUuid = call.parameters.noteUuid()
val exists = notesService.noteExists(userId, noteUuid)
if (!exists) return@get call.respondStatus(HttpStatusCode.NotFound)
val response = notesService.getNote(noteUuid)
val response =
notesService.getNote(userId, noteUuid) ?: return@get call.respondStatus(HttpStatusCode.NotFound)
call.respond(response)
}