passed/routes/get_password.go

43 lines
927 B
Go
Raw Normal View History

2024-10-29 23:08:11 +00:00
package routes
import (
"encoding/base64"
"encoding/json"
"net/http"
"git.1e99.eu/1e99/passed/storage"
)
func GetPassword(store storage.Store, encoding *base64.Encoding) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
id := req.PathValue("id")
2024-10-30 10:17:48 +00:00
password, err := store.Get(id)
2024-10-29 23:08:11 +00:00
switch {
case err == storage.ErrNotFound:
http.Error(res, "Password not found", http.StatusNotFound)
return
case err != nil:
http.Error(res, "", http.StatusInternalServerError)
return
}
2024-10-30 10:17:48 +00:00
err = store.Delete(id)
if err != nil {
http.Error(res, "", http.StatusInternalServerError)
return
}
2024-10-29 23:08:11 +00:00
2024-10-30 10:17:48 +00:00
encodedPassword := encoding.EncodeToString(password)
2024-10-29 23:08:11 +00:00
resBody := struct {
Password string `json:"password"`
}{
Password: encodedPassword,
}
err = json.NewEncoder(res).Encode(&resBody)
if err != nil {
http.Error(res, "", http.StatusInternalServerError)
return
}
}
}