Managers invite people into their organization and workspace admins into their workspace; the email links to a new /invite page that creates the account (or attaches an existing one without an organization) and adds the workspace membership. Invites bypass the signup rules, store only a token hash, expire after seven days, and return the link so it can be passed on when mail is not configured. Inviting someone already in the organization adds them to the workspace directly. Magic links reuse PocketBase's email OTP: the email now carries a link to /magic-link next to the code, and the admin settings gain a switch for it. Managers can also send a user a password-reset email. The dev proxies read DOCWELL_DEV_API so a dev server can target another docwell-server. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
87 lines
3.3 KiB
Go
87 lines
3.3 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"docwell/server/internal/access"
|
|
"docwell/server/internal/auth"
|
|
"docwell/server/internal/directory"
|
|
)
|
|
|
|
// InviteService is the part of directory.Service that handles invites.
|
|
type InviteService interface {
|
|
CreateInvite(ctx context.Context, c access.Caller, in directory.CreateInviteInput) (directory.Invite, error)
|
|
ListInvites(ctx context.Context, c access.Caller, in directory.ListInvitesInput) ([]directory.Invite, error)
|
|
RevokeInvite(ctx context.Context, c access.Caller, id string) error
|
|
ResendInvite(ctx context.Context, c access.Caller, id string) (directory.Invite, error)
|
|
GetInviteInfo(ctx context.Context, token string) (directory.InviteInfo, error)
|
|
AcceptInvite(ctx context.Context, me *auth.User, in directory.AcceptInviteInput) (string, error)
|
|
SendPasswordReset(ctx context.Context, c access.Caller, id string) error
|
|
}
|
|
|
|
func (a *api) inviteRoutes(mux *routeMux) {
|
|
mux.HandleFunc("GET /api/v1/invites", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
|
|
q := r.URL.Query()
|
|
list, err := a.dir.ListInvites(r.Context(), caller(r), directory.ListInvitesInput{
|
|
Workspace: q.Get("workspace"), Organization: q.Get("organization"),
|
|
})
|
|
a.respond(w, list, err)
|
|
}))
|
|
mux.HandleFunc("POST /api/v1/invites", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
|
|
var in directory.CreateInviteInput
|
|
if !a.decode(w, r, &in) {
|
|
return
|
|
}
|
|
inv, err := a.dir.CreateInvite(r.Context(), caller(r), in)
|
|
a.respondStatus(w, http.StatusCreated, inv, err)
|
|
}))
|
|
mux.HandleFunc("DELETE /api/v1/invites/{id}", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
|
|
a.respondNoContent(w, a.dir.RevokeInvite(r.Context(), caller(r), r.PathValue("id")))
|
|
}))
|
|
mux.HandleFunc("POST /api/v1/invites/{id}/resend", a.requireUser(func(w http.ResponseWriter, r *http.Request) {
|
|
inv, err := a.dir.ResendInvite(r.Context(), caller(r), r.PathValue("id"))
|
|
a.respond(w, inv, err)
|
|
}))
|
|
|
|
// The invite page: anyone holding the token may look and accept.
|
|
mux.HandleFunc("GET /api/v1/auth/invites/{token}", func(w http.ResponseWriter, r *http.Request) {
|
|
info, err := a.dir.GetInviteInfo(r.Context(), r.PathValue("token"))
|
|
a.respond(w, info, err)
|
|
})
|
|
mux.HandleFunc("POST /api/v1/auth/invites/accept", func(w http.ResponseWriter, r *http.Request) {
|
|
var in directory.AcceptInviteInput
|
|
if !a.decode(w, r, &in) {
|
|
return
|
|
}
|
|
// Signed in: attach this account. Signed out: create one and sign it in.
|
|
if token := auth.BearerToken(r); token != "" {
|
|
me, err := a.auth.Verify(r.Context(), token)
|
|
if err != nil {
|
|
a.writeError(w, err)
|
|
return
|
|
}
|
|
if _, err := a.dir.AcceptInvite(r.Context(), &me, in); err != nil {
|
|
a.writeError(w, err)
|
|
return
|
|
}
|
|
me, err = a.auth.Verify(r.Context(), token)
|
|
a.respond(w, auth.Session{Token: token, User: me}, err)
|
|
return
|
|
}
|
|
email, err := a.dir.AcceptInvite(r.Context(), nil, in)
|
|
if err != nil {
|
|
a.writeError(w, err)
|
|
return
|
|
}
|
|
sess, err := a.auth.Login(r.Context(), email, in.Password, "")
|
|
a.respond(w, sess, err)
|
|
})
|
|
|
|
mux.HandleFunc("POST /api/v1/users/{id}/password-reset", a.requireManager(func(w http.ResponseWriter, r *http.Request) {
|
|
a.respondNoContent(w, a.dir.SendPasswordReset(r.Context(), caller(r), r.PathValue("id")))
|
|
}))
|
|
}
|