42 lines
927 B
Go
42 lines
927 B
Go
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")
|
|
password, err := store.Get(id)
|
|
switch {
|
|
case err == storage.ErrNotFound:
|
|
http.Error(res, "Password not found", http.StatusNotFound)
|
|
return
|
|
case err != nil:
|
|
http.Error(res, "", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
err = store.Delete(id)
|
|
if err != nil {
|
|
http.Error(res, "", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
encodedPassword := encoding.EncodeToString(password)
|
|
resBody := struct {
|
|
Password string `json:"password"`
|
|
}{
|
|
Password: encodedPassword,
|
|
}
|
|
err = json.NewEncoder(res).Encode(&resBody)
|
|
if err != nil {
|
|
http.Error(res, "", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
}
|