Skip to content

Commit

Permalink
Unmarshal request body to payload moved to helper (#140)
Browse files Browse the repository at this point in the history
* Reads languages from store folder
* Delete unnecessary print statement
* Unmarshal request body to payload moved to helper
  • Loading branch information
furkanbegen authored Aug 1, 2020
1 parent 13ba0ba commit a187b43
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 42 deletions.
10 changes: 3 additions & 7 deletions internal/api/bank_account.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit a187b43

Please sign in to comment.