2026-03-14 20:52:11 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2026-04-04 16:48:57 +00:00
|
|
|
"fmt"
|
2026-03-29 12:35:04 +02:00
|
|
|
"io"
|
2026-03-14 20:52:11 +01:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
|
"github.com/owncord/server/db"
|
2026-03-15 07:07:59 +01:00
|
|
|
"github.com/owncord/server/permissions"
|
2026-04-05 21:01:29 +00:00
|
|
|
"github.com/owncord/server/service"
|
2026-03-14 20:52:11 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// createInviteRequest is the JSON body for POST /api/v1/invites.
|
|
|
|
|
type createInviteRequest struct {
|
|
|
|
|
MaxUses int `json:"max_uses"`
|
|
|
|
|
ExpiresInHours int `json:"expires_in_hours"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// inviteResponse is the API shape for an invite.
|
|
|
|
|
type inviteResponse struct {
|
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
|
Code string `json:"code"`
|
|
|
|
|
MaxUses *int `json:"max_uses"`
|
|
|
|
|
Uses int `json:"uses"`
|
|
|
|
|
ExpiresAt *string `json:"expires_at"`
|
|
|
|
|
Revoked bool `json:"revoked"`
|
|
|
|
|
CreatedAt string `json:"created_at"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MountInviteRoutes registers invite endpoints on the given router.
|
|
|
|
|
// All routes require authentication and MANAGE_INVITES permission.
|
2026-04-05 21:01:29 +00:00
|
|
|
func MountInviteRoutes(r chi.Router, database *db.DB, svc *service.Services) {
|
2026-03-14 20:52:11 +01:00
|
|
|
r.Route("/api/v1/invites", func(r chi.Router) {
|
|
|
|
|
r.Use(AuthMiddleware(database))
|
2026-03-15 07:07:59 +01:00
|
|
|
r.Use(RequirePermission(permissions.ManageInvites))
|
2026-03-14 20:52:11 +01:00
|
|
|
|
2026-04-05 21:01:29 +00:00
|
|
|
r.Post("/", handleCreateInvite(svc))
|
|
|
|
|
r.Get("/", handleListInvites(svc))
|
|
|
|
|
r.Delete("/{code}", handleRevokeInvite(svc))
|
2026-03-14 20:52:11 +01:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// handleCreateInvite processes POST /api/v1/invites.
|
2026-04-05 21:01:29 +00:00
|
|
|
func handleCreateInvite(svc *service.Services) http.HandlerFunc {
|
2026-03-14 20:52:11 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var req createInviteRequest
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
2026-03-29 12:35:04 +02:00
|
|
|
if err != io.EOF {
|
|
|
|
|
writeJSON(w, http.StatusBadRequest, errorResponse{
|
2026-04-05 21:01:29 +00:00
|
|
|
Error: "BAD_REQUEST", Message: "malformed JSON body",
|
2026-03-29 12:35:04 +02:00
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-03-14 20:52:11 +01:00
|
|
|
req = createInviteRequest{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user, ok := r.Context().Value(UserKey).(*db.User)
|
|
|
|
|
if !ok || user == nil {
|
|
|
|
|
writeJSON(w, http.StatusUnauthorized, errorResponse{
|
2026-04-05 21:01:29 +00:00
|
|
|
Error: "UNAUTHORIZED", Message: "not authenticated",
|
2026-03-14 20:52:11 +01:00
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 21:01:29 +00:00
|
|
|
// H-4: Cap invite expiration to 30 days.
|
|
|
|
|
if req.ExpiresInHours > service.MaxInviteExpiryHours() {
|
2026-04-04 16:48:57 +00:00
|
|
|
writeJSON(w, http.StatusBadRequest, errorResponse{
|
|
|
|
|
Error: "BAD_REQUEST",
|
2026-04-05 21:01:29 +00:00
|
|
|
Message: fmt.Sprintf("expires_in_hours cannot exceed %d (30 days)", service.MaxInviteExpiryHours()),
|
2026-04-04 16:48:57 +00:00
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-03-14 20:52:11 +01:00
|
|
|
|
2026-04-05 21:01:29 +00:00
|
|
|
inv, err := svc.Invites.CreateInvite(user.ID, req.MaxUses, req.ExpiresInHours)
|
2026-03-14 20:52:11 +01:00
|
|
|
if err != nil {
|
2026-04-05 21:01:29 +00:00
|
|
|
writeServiceError(w, err)
|
2026-03-14 20:52:11 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
writeJSON(w, http.StatusCreated, toInviteResponse(inv))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// handleListInvites processes GET /api/v1/invites.
|
2026-04-05 21:01:29 +00:00
|
|
|
func handleListInvites(svc *service.Services) http.HandlerFunc {
|
2026-03-14 20:52:11 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2026-04-05 21:01:29 +00:00
|
|
|
invites, err := svc.Invites.ListInvites()
|
2026-03-14 20:52:11 +01:00
|
|
|
if err != nil {
|
2026-04-05 21:01:29 +00:00
|
|
|
writeServiceError(w, err)
|
2026-03-14 20:52:11 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp := make([]inviteResponse, 0, len(invites))
|
|
|
|
|
for _, inv := range invites {
|
|
|
|
|
resp = append(resp, toInviteResponse(inv))
|
|
|
|
|
}
|
|
|
|
|
writeJSON(w, http.StatusOK, resp)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// handleRevokeInvite processes DELETE /api/v1/invites/:code.
|
2026-04-05 21:01:29 +00:00
|
|
|
func handleRevokeInvite(svc *service.Services) http.HandlerFunc {
|
2026-03-14 20:52:11 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
code := chi.URLParam(r, "code")
|
2026-04-05 21:01:29 +00:00
|
|
|
if err := svc.Invites.RevokeInvite(code); err != nil {
|
|
|
|
|
writeServiceError(w, err)
|
2026-03-14 20:52:11 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// toInviteResponse converts a db.Invite to the API response shape.
|
|
|
|
|
func toInviteResponse(inv *db.Invite) inviteResponse {
|
|
|
|
|
var maxUses *int
|
|
|
|
|
if inv.MaxUses != nil {
|
|
|
|
|
v := *inv.MaxUses
|
|
|
|
|
maxUses = &v
|
|
|
|
|
}
|
|
|
|
|
return inviteResponse{
|
|
|
|
|
ID: inv.ID,
|
|
|
|
|
Code: inv.Code,
|
|
|
|
|
MaxUses: maxUses,
|
|
|
|
|
Uses: inv.Uses,
|
|
|
|
|
ExpiresAt: inv.ExpiresAt,
|
|
|
|
|
Revoked: inv.Revoked,
|
|
|
|
|
CreatedAt: inv.CreatedAt,
|
|
|
|
|
}
|
|
|
|
|
}
|