Files
urapt/server/restapi/users.go
T
owen 981587e83d Initial release: self-hostable APT repository server and CLI
urapt is a self-hostable APT repository server with a companion CLI for
pushing and managing Debian .deb packages.

Server (urapt-server):
- REST API + APT endpoint, SQLite storage (pure-Go modernc driver, no CGO)
- .deb files stored content-addressed on disk, reference-counted for dedup
- Server-managed RSA-4096 OpenPGP signing key (ProtonMail/go-crypto)
- APT indices (Release/InRelease/Packages[.gz/.xz]) generated on demand
  from the DB, cached in memory, signed with the server key
- Full APT model: repositories -> distributions -> components -> architectures
- Bearer-token auth for REST; HTTP Basic auth for private-repo APT reads
- First registrant becomes admin; repo-scoped permissions
  (read/write/read-write/admin) plus owner and server-admin roles
- Multipart package push with control-field extraction, list/show/delete,
  pool serving, blob ref-count cleanup
- Audit log

CLI (urapt):
- register/login/logout/whoami, token management
- repo/distro/component/arch CRUD, member management
- push/pull/ls/show/rm for packages
- apt-config helper that emits apt setup commands (key, sources.list,
  auth.conf for private repos)

Packaging & docs:
- Dockerfile (multi-stage distroless), docker-compose.yml, sample config
- README quick start, architecture overview, config reference, security notes
- PLAN.md design blueprint, CHANGELOG.md, GPL-3.0 LICENSE
- GitHub Actions CI (test, lint, cross-build for linux/darwin amd64/arm64)
- Makefile release target producing static binaries + tarballs + checksums

Tests cover the data-access layer, auth/permission checks, APT index
generation, .deb parsing, GPG signing, the REST API, and the typed API
client. Verified end-to-end on a Raspberry Pi (arm64) pushing and installing
a real package.
2026-06-28 16:57:34 -05:00

92 lines
3.0 KiB
Go

package restapi
import (
"errors"
"net/http"
"urapt/server/middleware"
"urapt/server/store"
apitypes "urapt/shared/api"
"urapt/shared/httputil"
"urapt/shared/models"
)
// ListUsers returns all users (admin only).
func (api *API) ListUsers(w http.ResponseWriter, r *http.Request) {
users, err := api.Store.ListUsers(r.Context())
if err != nil {
httputil.WriteError(w, http.StatusInternalServerError, httputil.CodeInternal, "failed to list users")
return
}
if users == nil {
users = []*models.User{}
}
httputil.WriteJSON(w, http.StatusOK, apitypes.ListResponse[*models.User]{Items: users, Page: 1, PerPage: 100, Total: len(users)})
}
// GetUser returns a single user by id (admin only).
func (api *API) GetUser(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
user, err := api.Store.GetUserByID(r.Context(), id)
if err != nil {
if errors.Is(err, store.ErrNotFound) {
httputil.WriteError(w, http.StatusNotFound, httputil.CodeNotFound, "user not found")
return
}
httputil.WriteError(w, http.StatusInternalServerError, httputil.CodeInternal, "failed to read user")
return
}
httputil.WriteJSON(w, http.StatusOK, user)
}
// UpdateUser mutates a user (currently only is_admin) (admin only).
func (api *API) UpdateUser(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
var req apitypes.UpdateUserRequest
if err := httputil.ReadJSON(r, &req, 1<<20); err != nil {
bad(w, "invalid JSON body")
return
}
target, err := api.Store.GetUserByID(r.Context(), id)
if err != nil {
if errors.Is(err, store.ErrNotFound) {
httputil.WriteError(w, http.StatusNotFound, httputil.CodeNotFound, "user not found")
return
}
httputil.WriteError(w, http.StatusInternalServerError, httputil.CodeInternal, "failed to read user")
return
}
caller := middleware.IdentityFromContext(r.Context())
if req.IsAdmin != nil {
if *req.IsAdmin && !caller.User.IsAdmin {
httputil.WriteError(w, http.StatusForbidden, httputil.CodeForbidden, "cannot grant admin")
return
}
if target.ID == caller.User.ID && !*req.IsAdmin {
httputil.WriteError(w, http.StatusBadRequest, httputil.CodeBadRequest, "cannot revoke your own admin")
return
}
}
if err := api.Store.UpdateUser(r.Context(), id, req.IsAdmin); err != nil {
httputil.WriteError(w, http.StatusInternalServerError, httputil.CodeInternal, "failed to update user")
return
}
updated, _ := api.Store.GetUserByID(r.Context(), id)
httputil.WriteJSON(w, http.StatusOK, updated)
}
// DeleteUser removes a user (admin only). Self-deletion is blocked.
func (api *API) DeleteUser(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
caller := middleware.IdentityFromContext(r.Context())
if id == caller.User.ID {
httputil.WriteError(w, http.StatusBadRequest, httputil.CodeBadRequest, "cannot delete your own account")
return
}
if err := api.Store.DeleteUser(r.Context(), id); err != nil {
httputil.WriteError(w, http.StatusInternalServerError, httputil.CodeInternal, "failed to delete user")
return
}
w.WriteHeader(http.StatusNoContent)
}