diff --git a/routes/create_password.go b/routes/create_password.go index d2d8960..0c57c73 100644 --- a/routes/create_password.go +++ b/routes/create_password.go @@ -12,7 +12,8 @@ import ( func CreatePassword(store storage.Store, maxLength int, encoding *base64.Encoding) http.HandlerFunc { return func(res http.ResponseWriter, req *http.Request) { var reqBody struct { - Password string `json:"password"` + // Go automatically decodes byte arrays using Base64 + Password []byte `json:"password"` ExpiresIn time.Duration `json:"expires-in"` } err := json.NewDecoder(req.Body).Decode(&reqBody) @@ -21,13 +22,7 @@ func CreatePassword(store storage.Store, maxLength int, encoding *base64.Encodin return } - password, err := encoding.DecodeString(reqBody.Password) - if err != nil { - http.Error(res, "Bad base64 encoding", http.StatusBadRequest) - return - } - - if len(password) > maxLength { + if len(reqBody.Password) > maxLength { http.Error(res, "Password too long", 413) // Payload Too Large return } @@ -44,7 +39,7 @@ func CreatePassword(store storage.Store, maxLength int, encoding *base64.Encodin } id, err := store.Create( - password, + reqBody.Password, time.Now().Add(expiresIn), ) switch { diff --git a/routes/get_password.go b/routes/get_password.go index 9495c1e..499f5a4 100644 --- a/routes/get_password.go +++ b/routes/get_password.go @@ -27,11 +27,11 @@ func GetPassword(store storage.Store, encoding *base64.Encoding) http.HandlerFun return } - encodedPassword := encoding.EncodeToString(password) resBody := struct { - Password string `json:"password"` + // Go automatically encodes byte arrays using Base64 + Password []byte `json:"password"` }{ - Password: encodedPassword, + Password: password, } err = json.NewEncoder(res).Encode(&resBody) if err != nil {