From 507698c7eafb625986ce650e78b57b95a3575bb2 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Sat, 6 Feb 2021 00:32:07 +0100 Subject: [PATCH] use always receiveOrBadRequest --- src/main/kotlin/component/article/routes/UpsertArticle.kt | 3 ++- src/main/kotlin/component/auth/routes/Login.kt | 3 ++- src/main/kotlin/component/auth/routes/Register.kt | 3 ++- src/main/kotlin/component/auth/routes/Sso.kt | 3 ++- .../component/comment/article/routes/CreateCommentArticle.kt | 3 ++- .../component/comment/generic/routes/CreateCommentChildren.kt | 3 ++- .../kotlin/component/constitution/routes/CreateConstitution.kt | 3 ++- src/main/kotlin/component/opinion/routes/OpinionArticle.kt | 3 ++- src/main/kotlin/component/vote/routes/PutVoteOnArticle.kt | 3 ++- src/main/kotlin/component/vote/routes/PutVoteOnComment.kt | 3 ++- src/main/kotlin/component/vote/routes/VoteConstitution.kt | 3 ++- src/main/kotlin/component/workgroup/routes/CreateWorkgroup.kt | 3 ++- src/main/kotlin/component/workgroup/routes/EditWorkgroup.kt | 3 ++- .../component/workgroup/routes/members/AddMemberToWorkgroup.kt | 3 ++- .../workgroup/routes/members/DeleteMembersOfWorkgroup.kt | 3 ++- .../workgroup/routes/members/UpdateMemberOfWorkgroup.kt | 3 ++- 16 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/main/kotlin/component/article/routes/UpsertArticle.kt b/src/main/kotlin/component/article/routes/UpsertArticle.kt index b76f7e0..2020cea 100644 --- a/src/main/kotlin/component/article/routes/UpsertArticle.kt +++ b/src/main/kotlin/component/article/routes/UpsertArticle.kt @@ -11,6 +11,7 @@ import fr.dcproject.component.notification.ArticleUpdateNotification import fr.dcproject.component.notification.Publisher import fr.dcproject.component.workgroup.WorkgroupRef import fr.dcproject.security.assert +import fr.dcproject.utils.receiveOrBadRequest import io.ktor.application.ApplicationCall import io.ktor.application.call import io.ktor.locations.KtorExperimentalLocationsAPI @@ -39,7 +40,7 @@ object UpsertArticle { } fun Route.upsertArticle(repo: ArticleRepository, publisher: Publisher, ac: ArticleAccessControl) { - suspend fun ApplicationCall.convertRequestToEntity(): ArticleForUpdate = receive().run { + suspend fun ApplicationCall.convertRequestToEntity(): ArticleForUpdate = receiveOrBadRequest().run { ArticleForUpdate( id = id ?: UUID.randomUUID(), title = title, diff --git a/src/main/kotlin/component/auth/routes/Login.kt b/src/main/kotlin/component/auth/routes/Login.kt index e117dab..57ed358 100644 --- a/src/main/kotlin/component/auth/routes/Login.kt +++ b/src/main/kotlin/component/auth/routes/Login.kt @@ -3,6 +3,7 @@ package fr.dcproject.component.auth.routes import com.fasterxml.jackson.databind.exc.MismatchedInputException import fr.dcproject.component.auth.UserRepository import fr.dcproject.component.auth.jwt.makeToken +import fr.dcproject.utils.receiveOrBadRequest import io.ktor.application.call import io.ktor.auth.UserPasswordCredential import io.ktor.http.HttpStatusCode @@ -22,7 +23,7 @@ object Login { fun Route.authLogin(userRepo: UserRepository) { post { try { - val credentials = call.receive() + val credentials = call.receiveOrBadRequest() userRepo.findByCredentials(credentials)?.let { user -> call.respondText(user.makeToken()) } ?: call.respond(HttpStatusCode.BadRequest, "Username not exist or password is wrong") diff --git a/src/main/kotlin/component/auth/routes/Register.kt b/src/main/kotlin/component/auth/routes/Register.kt index d0f4c3e..3699591 100644 --- a/src/main/kotlin/component/auth/routes/Register.kt +++ b/src/main/kotlin/component/auth/routes/Register.kt @@ -8,6 +8,7 @@ import fr.dcproject.component.auth.routes.Register.RegisterRequest.Input import fr.dcproject.component.citizen.Citizen import fr.dcproject.component.citizen.CitizenI import fr.dcproject.component.citizen.CitizenRepository +import fr.dcproject.utils.receiveOrBadRequest import io.ktor.application.call import io.ktor.features.BadRequestException import io.ktor.http.HttpStatusCode @@ -60,7 +61,7 @@ object Register { post { try { - val citizen = call.receive().toCitizen() + val citizen = call.receiveOrBadRequest().toCitizen() val createdCitizen = citizenRepo.insertWithUser(citizen)?.user ?: throw BadRequestException("Bad request") call.respondText(createdCitizen.makeToken()) } catch (e: MissingKotlinParameterException) { diff --git a/src/main/kotlin/component/auth/routes/Sso.kt b/src/main/kotlin/component/auth/routes/Sso.kt index 05be7b0..6ac2d86 100644 --- a/src/main/kotlin/component/auth/routes/Sso.kt +++ b/src/main/kotlin/component/auth/routes/Sso.kt @@ -2,6 +2,7 @@ package fr.dcproject.component.auth.routes import fr.dcproject.component.auth.PasswordlessAuth import fr.dcproject.component.auth.routes.Sso.PasswordlessRequest.Input +import fr.dcproject.utils.receiveOrBadRequest import io.ktor.application.call import io.ktor.http.HttpStatusCode import io.ktor.locations.KtorExperimentalLocationsAPI @@ -23,7 +24,7 @@ object Sso { */ fun Route.authPasswordless(passwordlessAuth: PasswordlessAuth) { post { - call.receive().run { + call.receiveOrBadRequest().run { try { passwordlessAuth.sendEmail(email, url) } catch (e: PasswordlessAuth.EmailNotFound) { diff --git a/src/main/kotlin/component/comment/article/routes/CreateCommentArticle.kt b/src/main/kotlin/component/comment/article/routes/CreateCommentArticle.kt index c708d16..5eff6d4 100644 --- a/src/main/kotlin/component/comment/article/routes/CreateCommentArticle.kt +++ b/src/main/kotlin/component/comment/article/routes/CreateCommentArticle.kt @@ -8,6 +8,7 @@ import fr.dcproject.component.comment.article.routes.CreateCommentArticle.PostAr import fr.dcproject.component.comment.generic.CommentAccessControl import fr.dcproject.component.comment.generic.CommentForUpdate import fr.dcproject.security.assert +import fr.dcproject.utils.receiveOrBadRequest import io.ktor.application.ApplicationCall import io.ktor.application.call import io.ktor.http.HttpStatusCode @@ -27,7 +28,7 @@ object CreateCommentArticle { class Input(val content: String) } - suspend fun PostArticleCommentRequest.getComment(call: ApplicationCall) = call.receive().run { + suspend fun PostArticleCommentRequest.getComment(call: ApplicationCall) = call.receiveOrBadRequest().run { CommentForUpdate( target = article, createdBy = call.citizen, diff --git a/src/main/kotlin/component/comment/generic/routes/CreateCommentChildren.kt b/src/main/kotlin/component/comment/generic/routes/CreateCommentChildren.kt index 33f9fe5..6a1e1ee 100644 --- a/src/main/kotlin/component/comment/generic/routes/CreateCommentChildren.kt +++ b/src/main/kotlin/component/comment/generic/routes/CreateCommentChildren.kt @@ -7,6 +7,7 @@ import fr.dcproject.component.comment.generic.CommentForUpdate import fr.dcproject.component.comment.generic.CommentRef import fr.dcproject.component.comment.generic.CommentRepository import fr.dcproject.security.assert +import fr.dcproject.utils.receiveOrBadRequest import io.ktor.application.call import io.ktor.features.NotFoundException import io.ktor.http.HttpStatusCode @@ -30,7 +31,7 @@ object CreateCommentChildren { post { val parent = repo.findById(it.comment.id) ?: throw NotFoundException("Comment not found") val newComment = CommentForUpdate( - content = call.receive().content, + content = call.receiveOrBadRequest().content, createdBy = citizen, parent = parent ) diff --git a/src/main/kotlin/component/constitution/routes/CreateConstitution.kt b/src/main/kotlin/component/constitution/routes/CreateConstitution.kt index 0af8436..c1d8652 100644 --- a/src/main/kotlin/component/constitution/routes/CreateConstitution.kt +++ b/src/main/kotlin/component/constitution/routes/CreateConstitution.kt @@ -12,6 +12,7 @@ import fr.dcproject.component.constitution.ConstitutionSimple.TitleSimple import fr.dcproject.component.constitution.routes.CreateConstitution.PostConstitutionRequest.Input import fr.dcproject.component.constitution.routes.CreateConstitution.PostConstitutionRequest.Input.Title import fr.dcproject.security.assert +import fr.dcproject.utils.receiveOrBadRequest import fr.postgresjson.entity.UuidEntity import io.ktor.application.call import io.ktor.locations.KtorExperimentalLocationsAPI @@ -72,7 +73,7 @@ object CreateConstitution { fun Route.createConstitution(repo: ConstitutionRepository, ac: ConstitutionAccessControl) { post { - getNewConstitution(call.receive(), citizen).let { + getNewConstitution(call.receiveOrBadRequest(), citizen).let { ac.assert { canCreate(it, citizenOrNull) } repo.upsert(it) call.respond(it) diff --git a/src/main/kotlin/component/opinion/routes/OpinionArticle.kt b/src/main/kotlin/component/opinion/routes/OpinionArticle.kt index ced5ebb..ab2a37d 100644 --- a/src/main/kotlin/component/opinion/routes/OpinionArticle.kt +++ b/src/main/kotlin/component/opinion/routes/OpinionArticle.kt @@ -7,6 +7,7 @@ import fr.dcproject.component.opinion.OpinionAccessControl import fr.dcproject.component.opinion.entity.OpinionChoiceRef import fr.dcproject.component.opinion.entity.OpinionForUpdate import fr.dcproject.security.assert +import fr.dcproject.utils.receiveOrBadRequest import fr.dcproject.utils.toUUID import io.ktor.application.call import io.ktor.http.HttpStatusCode @@ -34,7 +35,7 @@ object OpinionArticle { fun Route.setOpinionOnArticle(repo: OpinionArticleRepository, ac: OpinionAccessControl) { put { - call.receive().ids.map { id -> + call.receiveOrBadRequest().ids.map { id -> OpinionForUpdate( choice = OpinionChoiceRef(id), target = it.article, diff --git a/src/main/kotlin/component/vote/routes/PutVoteOnArticle.kt b/src/main/kotlin/component/vote/routes/PutVoteOnArticle.kt index 5a8501b..3c2adbe 100644 --- a/src/main/kotlin/component/vote/routes/PutVoteOnArticle.kt +++ b/src/main/kotlin/component/vote/routes/PutVoteOnArticle.kt @@ -8,6 +8,7 @@ import fr.dcproject.component.vote.VoteAccessControl import fr.dcproject.component.vote.VoteArticleRepository import fr.dcproject.component.vote.entity.VoteForUpdate import fr.dcproject.security.assert +import fr.dcproject.utils.receiveOrBadRequest import io.ktor.application.call import io.ktor.features.NotFoundException import io.ktor.http.HttpStatusCode @@ -29,7 +30,7 @@ object PutVoteOnArticle { fun Route.putVoteOnArticle(repo: VoteArticleRepository, ac: VoteAccessControl, articleRepo: ArticleRepository) { put { - val input = call.receive() + val input = call.receiveOrBadRequest() val article = articleRepo.findById(it.article.id) ?: throw NotFoundException("Article ${it.article.id} not found") val vote = VoteForUpdate( target = article, diff --git a/src/main/kotlin/component/vote/routes/PutVoteOnComment.kt b/src/main/kotlin/component/vote/routes/PutVoteOnComment.kt index 9a38d0e..745032c 100644 --- a/src/main/kotlin/component/vote/routes/PutVoteOnComment.kt +++ b/src/main/kotlin/component/vote/routes/PutVoteOnComment.kt @@ -7,6 +7,7 @@ import fr.dcproject.component.vote.VoteAccessControl import fr.dcproject.component.vote.VoteCommentRepository import fr.dcproject.component.vote.entity.VoteForUpdate import fr.dcproject.security.assert +import fr.dcproject.utils.receiveOrBadRequest import io.ktor.application.call import io.ktor.http.HttpStatusCode import io.ktor.locations.KtorExperimentalLocationsAPI @@ -27,7 +28,7 @@ object PutVoteOnComment { fun Route.putVoteOnComment(voteCommentRepo: VoteCommentRepository, commentRepo: CommentRepository, ac: VoteAccessControl) { put { val comment = commentRepo.findById(it.comment)!! - val content = call.receive() + val content = call.receiveOrBadRequest() val vote = VoteForUpdate( target = comment, note = content.note, diff --git a/src/main/kotlin/component/vote/routes/VoteConstitution.kt b/src/main/kotlin/component/vote/routes/VoteConstitution.kt index a777010..6a7dd0d 100644 --- a/src/main/kotlin/component/vote/routes/VoteConstitution.kt +++ b/src/main/kotlin/component/vote/routes/VoteConstitution.kt @@ -9,6 +9,7 @@ import fr.dcproject.component.vote.VoteConstitutionRepository import fr.dcproject.component.vote.entity.VoteForUpdate import fr.dcproject.component.vote.routes.VoteConstitution.ConstitutionVoteRequest.Input import fr.dcproject.security.assert +import fr.dcproject.utils.receiveOrBadRequest import io.ktor.application.call import io.ktor.features.NotFoundException import io.ktor.http.HttpStatusCode @@ -32,7 +33,7 @@ object VoteConstitution { fun Route.voteConstitution(repo: VoteConstitutionRepository, ac: VoteAccessControl, constitutionRepo: ConstitutionRepository) { put { val constitution = constitutionRepo.findById(it.constitution.id) ?: throw NotFoundException("Unable to find constitution ${it.constitution.id}") - val content = call.receive() + val content = call.receiveOrBadRequest() val vote = VoteForUpdate( target = constitution, note = content.note, diff --git a/src/main/kotlin/component/workgroup/routes/CreateWorkgroup.kt b/src/main/kotlin/component/workgroup/routes/CreateWorkgroup.kt index 7013258..fe02e0d 100644 --- a/src/main/kotlin/component/workgroup/routes/CreateWorkgroup.kt +++ b/src/main/kotlin/component/workgroup/routes/CreateWorkgroup.kt @@ -7,6 +7,7 @@ import fr.dcproject.component.workgroup.WorkgroupRepository import fr.dcproject.component.workgroup.WorkgroupSimple import fr.dcproject.component.workgroup.routes.CreateWorkgroup.PostWorkgroupRequest.Input import fr.dcproject.security.assert +import fr.dcproject.utils.receiveOrBadRequest import io.ktor.application.call import io.ktor.http.HttpStatusCode import io.ktor.locations.KtorExperimentalLocationsAPI @@ -32,7 +33,7 @@ object CreateWorkgroup { fun Route.createWorkgroup(repo: WorkgroupRepository, ac: WorkgroupAccessControl) { post { - call.receive().run { + call.receiveOrBadRequest().run { WorkgroupSimple( id ?: UUID.randomUUID(), name, diff --git a/src/main/kotlin/component/workgroup/routes/EditWorkgroup.kt b/src/main/kotlin/component/workgroup/routes/EditWorkgroup.kt index 9652c0d..04325d9 100644 --- a/src/main/kotlin/component/workgroup/routes/EditWorkgroup.kt +++ b/src/main/kotlin/component/workgroup/routes/EditWorkgroup.kt @@ -5,6 +5,7 @@ import fr.dcproject.component.workgroup.WorkgroupAccessControl import fr.dcproject.component.workgroup.WorkgroupRepository import fr.dcproject.component.workgroup.routes.EditWorkgroup.PutWorkgroupRequest.Input import fr.dcproject.security.assert +import fr.dcproject.utils.receiveOrBadRequest import io.ktor.application.call import io.ktor.http.HttpStatusCode import io.ktor.locations.KtorExperimentalLocationsAPI @@ -31,7 +32,7 @@ object EditWorkgroup { fun Route.editWorkgroup(repo: WorkgroupRepository, ac: WorkgroupAccessControl) { put { repo.findById(it.workgroupId)?.let { old -> - call.receive().run { + call.receiveOrBadRequest().run { old.copy( name = name ?: old.name, description = description ?: old.description, diff --git a/src/main/kotlin/component/workgroup/routes/members/AddMemberToWorkgroup.kt b/src/main/kotlin/component/workgroup/routes/members/AddMemberToWorkgroup.kt index 8f10bac..a6ee8b5 100644 --- a/src/main/kotlin/component/workgroup/routes/members/AddMemberToWorkgroup.kt +++ b/src/main/kotlin/component/workgroup/routes/members/AddMemberToWorkgroup.kt @@ -6,6 +6,7 @@ import fr.dcproject.component.workgroup.WorkgroupAccessControl import fr.dcproject.component.workgroup.WorkgroupRepository import fr.dcproject.component.workgroup.WorkgroupWithMembersI import fr.dcproject.security.assert +import fr.dcproject.utils.receiveOrBadRequest import io.ktor.application.ApplicationCall import io.ktor.application.call import io.ktor.http.HttpStatusCode @@ -32,7 +33,7 @@ object AddMemberToWorkgroup { } @KtorExperimentalLocationsAPI - private suspend fun ApplicationCall.getMembersFromRequest(): List> = receive().map { + private suspend fun ApplicationCall.getMembersFromRequest(): List> = receiveOrBadRequest().map { WorkgroupWithMembersI.Member( citizen = it.citizen, roles = it.roles diff --git a/src/main/kotlin/component/workgroup/routes/members/DeleteMembersOfWorkgroup.kt b/src/main/kotlin/component/workgroup/routes/members/DeleteMembersOfWorkgroup.kt index cbc3268..49ed4d2 100644 --- a/src/main/kotlin/component/workgroup/routes/members/DeleteMembersOfWorkgroup.kt +++ b/src/main/kotlin/component/workgroup/routes/members/DeleteMembersOfWorkgroup.kt @@ -6,6 +6,7 @@ import fr.dcproject.component.workgroup.WorkgroupAccessControl import fr.dcproject.component.workgroup.WorkgroupRepository import fr.dcproject.component.workgroup.WorkgroupWithMembersI import fr.dcproject.security.assert +import fr.dcproject.utils.receiveOrBadRequest import io.ktor.application.ApplicationCall import io.ktor.application.call import io.ktor.http.HttpStatusCode @@ -31,7 +32,7 @@ object DeleteMembersOfWorkgroup { } } - private suspend fun ApplicationCall.getMembersFromRequest(): List> = receive().map { + private suspend fun ApplicationCall.getMembersFromRequest(): List> = receiveOrBadRequest().map { WorkgroupWithMembersI.Member( citizen = it.citizen, roles = it.roles diff --git a/src/main/kotlin/component/workgroup/routes/members/UpdateMemberOfWorkgroup.kt b/src/main/kotlin/component/workgroup/routes/members/UpdateMemberOfWorkgroup.kt index d35d2ba..c177714 100644 --- a/src/main/kotlin/component/workgroup/routes/members/UpdateMemberOfWorkgroup.kt +++ b/src/main/kotlin/component/workgroup/routes/members/UpdateMemberOfWorkgroup.kt @@ -6,6 +6,7 @@ import fr.dcproject.component.workgroup.WorkgroupAccessControl import fr.dcproject.component.workgroup.WorkgroupRepository import fr.dcproject.component.workgroup.WorkgroupWithMembersI import fr.dcproject.security.assert +import fr.dcproject.utils.receiveOrBadRequest import io.ktor.application.ApplicationCall import io.ktor.application.call import io.ktor.http.HttpStatusCode @@ -31,7 +32,7 @@ object UpdateMemberOfWorkgroup { } } - private suspend fun ApplicationCall.getMembersFromRequest(): List> = receive().map { + private suspend fun ApplicationCall.getMembersFromRequest(): List> = receiveOrBadRequest().map { WorkgroupWithMembersI.Member( citizen = it.citizen, roles = it.roles