Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unmarshal request body to payload moved to helper #140

Merged
merged 5 commits into from Aug 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 3 additions & 7 deletions internal/api/bank_account.go
Expand Up @@ -90,21 +90,17 @@ func FindBankAccountByID(s storage.Store) http.HandlerFunc {
func CreateBankAccount(s storage.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

// TODO BEGIN: This part should be in a helper function
// Unmarshal request body to payload
var payload model.Payload
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&payload); err != nil {
payload, err := ToPayload(r)
if err != nil {
RespondWithError(w, http.StatusBadRequest, InvalidRequestPayload)
return
}
defer r.Body.Close()
// TODO END:

// Decrypt payload
var bankAccountDTO model.BankAccountDTO
key := r.Context().Value("transmissionKey").(string)
err := app.DecryptJSON(key, []byte(payload.Data), &bankAccountDTO)
err = app.DecryptJSON(key, []byte(payload.Data), &bankAccountDTO)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, err.Error())
return
Expand Down
10 changes: 3 additions & 7 deletions internal/api/credit_card.go
Expand Up @@ -93,21 +93,17 @@ func FindCreditCardByID(s storage.Store) http.HandlerFunc {
func CreateCreditCard(s storage.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

// TODO BEGIN: This part should be in a helper function
// Unmarshal request body to payload
var payload model.Payload
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&payload); err != nil {
payload, err := ToPayload(r)
if err != nil {
RespondWithError(w, http.StatusBadRequest, InvalidRequestPayload)
return
}
defer r.Body.Close()
// TODO END:

// Decrypt payload
var creditCardDTO model.CreditCardDTO
key := r.Context().Value("transmissionKey").(string)
err := app.DecryptJSON(key, []byte(payload.Data), &creditCardDTO)
err = app.DecryptJSON(key, []byte(payload.Data), &creditCardDTO)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, err.Error())
return
Expand Down
10 changes: 3 additions & 7 deletions internal/api/email.go
Expand Up @@ -86,21 +86,17 @@ func FindEmailByID(s storage.Store) http.HandlerFunc {
func CreateEmail(s storage.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

// TODO BEGIN: This part should be in a helper function
// Unmarshal request body to payload
var payload model.Payload
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&payload); err != nil {
payload, err := ToPayload(r)
if err != nil {
RespondWithError(w, http.StatusBadRequest, InvalidRequestPayload)
return
}
defer r.Body.Close()
// TODO END:

// Decrypt payload
var emailDTO model.EmailDTO
key := r.Context().Value("transmissionKey").(string)
err := app.DecryptJSON(key, []byte(payload.Data), &emailDTO)
err = app.DecryptJSON(key, []byte(payload.Data), &emailDTO)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, err.Error())
return
Expand Down
12 changes: 12 additions & 0 deletions internal/api/helper.go
@@ -1,6 +1,8 @@
package api

import (
"encoding/json"
"github.com/passwall/passwall-server/model"
"net/http"
"regexp"
"strconv"
Expand Down Expand Up @@ -88,3 +90,13 @@ func ToSnakeCase(str string) string {

return strings.ToLower(snake)
}

// ToPayload unmarshal request body to payload
func ToPayload(r *http.Request) (model.Payload, error) {
var payload model.Payload
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&payload); err != nil {
return model.Payload{}, err
}
return payload, nil
}
10 changes: 3 additions & 7 deletions internal/api/login.go
Expand Up @@ -94,21 +94,17 @@ func FindLoginsByID(s storage.Store) http.HandlerFunc {
func CreateLogin(s storage.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

// TODO BEGIN: This part should be in a helper function
// Unmarshal request body to payload
var payload model.Payload
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&payload); err != nil {
payload, err := ToPayload(r)
if err != nil {
RespondWithError(w, http.StatusBadRequest, InvalidRequestPayload)
return
}
defer r.Body.Close()
// TODO END:

// Decrypt payload
var loginDTO model.LoginDTO
key := r.Context().Value("transmissionKey").(string)
err := app.DecryptJSON(key, []byte(payload.Data), &loginDTO)
err = app.DecryptJSON(key, []byte(payload.Data), &loginDTO)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, err.Error())
return
Expand Down
10 changes: 3 additions & 7 deletions internal/api/note.go
Expand Up @@ -91,21 +91,17 @@ func FindNoteByID(s storage.Store) http.HandlerFunc {
func CreateNote(s storage.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

// TODO BEGIN: This part should be in a helper function
// Unmarshal request body to payload
var payload model.Payload
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&payload); err != nil {
payload, err := ToPayload(r)
if err != nil {
RespondWithError(w, http.StatusBadRequest, InvalidRequestPayload)
return
}
defer r.Body.Close()
// TODO END:

// Decrypt payload
var noteDTO model.NoteDTO
key := r.Context().Value("transmissionKey").(string)
err := app.DecryptJSON(key, []byte(payload.Data), &noteDTO)
err = app.DecryptJSON(key, []byte(payload.Data), &noteDTO)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, err.Error())
return
Expand Down
10 changes: 3 additions & 7 deletions internal/api/server.go
Expand Up @@ -91,21 +91,17 @@ func FindServerByID(s storage.Store) http.HandlerFunc {
func CreateServer(s storage.Store) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

// TODO BEGIN: This part should be in a helper function
// Unmarshal request body to payload
var payload model.Payload
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&payload); err != nil {
payload, err := ToPayload(r)
if err != nil {
RespondWithError(w, http.StatusBadRequest, InvalidRequestPayload)
return
}
defer r.Body.Close()
// TODO END:

// Decrypt payload
var serverDTO model.ServerDTO
key := r.Context().Value("transmissionKey").(string)
err := app.DecryptJSON(key, []byte(payload.Data), &serverDTO)
err = app.DecryptJSON(key, []byte(payload.Data), &serverDTO)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, err.Error())
return
Expand Down