Add user credentials backup feature
This commit is contained in:
1 parent
db6a333029
commit
ef2667e71e
9 files changed
+261
-3
No files matched your search
@@ -29,7 +29,8 @@ func statusFor(err error) int {
|
||||
errors.Is(err, meeting.ErrNoVote),
|
||||
errors.Is(err, user.ErrUnsupportedFormat),
|
||||
errors.Is(err, user.ErrExceedDimensions),
|
||||
errors.Is(err, user.ErrImageTooLarge):
|
||||
errors.Is(err, user.ErrImageTooLarge),
|
||||
errors.Is(err, user.ErrInvalidCSV):
|
||||
return http.StatusBadRequest
|
||||
case errors.Is(err, auth.ErrCanceled):
|
||||
return http.StatusRequestTimeout
|
||||
|
||||
@@ -2,6 +2,7 @@ package rest
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
@@ -10,6 +11,9 @@ import (
|
||||
"goAuth/internal/user"
|
||||
)
|
||||
|
||||
// maxUsersImportSize caps an uploaded users CSV file (5 MiB).
|
||||
const maxUsersImportSize = 5 << 20
|
||||
|
||||
type changeUserPasswordRequest struct {
|
||||
Password string `json:"password"`
|
||||
}
|
||||
@@ -187,3 +191,69 @@ func newUserResponse(u *user.User, hasAvatar bool) userResponse {
|
||||
ActivatedAt: u.ActivatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
type importUsersResponse struct {
|
||||
Created int `json:"created"`
|
||||
}
|
||||
|
||||
// exportUsers streams a login/password CSV of every user except the superuser
|
||||
// (the seeded admin), for backup purposes.
|
||||
func (h *Handler) exportUsers(w http.ResponseWriter, r *http.Request) {
|
||||
data, err := h.users.ExportUsersCSV(r.Context(), AdminLogin)
|
||||
if err != nil {
|
||||
h.fail(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/csv; charset=utf-8")
|
||||
w.Header().Set("Content-Disposition", `attachment; filename="users.csv"`)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
// importUsers creates users from an uploaded login/password CSV. The superuser
|
||||
// login is skipped, so an export/import round trip never touches it.
|
||||
func (h *Handler) importUsers(w http.ResponseWriter, r *http.Request) {
|
||||
data, err := readUsersImport(r)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid csv upload")
|
||||
return
|
||||
}
|
||||
|
||||
created, err := h.users.ImportUsersCSV(r.Context(), AdminLogin, data)
|
||||
if err != nil {
|
||||
h.fail(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
writeJson(w, http.StatusOK, importUsersResponse{Created: created})
|
||||
}
|
||||
|
||||
// readUsersImport reads the uploaded CSV either as a multipart file field named
|
||||
// "file" or as a raw request body, capped at maxUsersImportSize.
|
||||
func readUsersImport(r *http.Request) ([]byte, error) {
|
||||
contentType := r.Header.Get("Content-Type")
|
||||
const multipart = "multipart/form-data"
|
||||
|
||||
// Read one byte past the limit so an oversized upload is detected.
|
||||
const limit = maxUsersImportSize + 1
|
||||
|
||||
var reader io.Reader = r.Body
|
||||
if len(contentType) >= len(multipart) && contentType[:len(multipart)] == multipart {
|
||||
file, _, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
reader = file
|
||||
}
|
||||
|
||||
data, err := io.ReadAll(io.LimitReader(reader, limit))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(data) > maxUsersImportSize {
|
||||
return nil, user.ErrInvalidCSV
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
@@ -46,6 +46,8 @@ func (h *Handler) registerRoutes(mux *http.ServeMux) {
|
||||
mux.Handle("GET /api/users/{id}/avatar", publicChain.ThenFunc(h.getUserAvatar))
|
||||
|
||||
mux.Handle("GET /api/users", superuserChain.ThenFunc(h.listUsers))
|
||||
mux.Handle("GET /api/users/export", superuserChain.ThenFunc(h.exportUsers))
|
||||
mux.Handle("POST /api/users/import", superuserChain.ThenFunc(h.importUsers))
|
||||
mux.Handle("PUT /api/users/{id}/password", superuserChain.ThenFunc(h.changeUserPassword))
|
||||
mux.Handle("DELETE /api/users/{id}/sessions", superuserChain.ThenFunc(h.signoutUserSessions))
|
||||
|
||||
|
||||
Reference in new issue
Block a user