2026-10-01 23:15:57 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/mail"
|
|
|
|
|
"net/url"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/danyel/go-loose/internal/key"
|
|
|
|
|
"github.com/danyel/go-loose/internal/password"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (s *Server) configureClient(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var request struct {
|
|
|
|
|
RedirectURIs []string `json:"redirect_uris"`
|
|
|
|
|
}
|
|
|
|
|
if !decodeJSON(w, r, &request) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if len(request.RedirectURIs) == 0 {
|
|
|
|
|
writeError(w, http.StatusBadRequest, "at least one redirect URI is required")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
seen := make(map[string]bool)
|
|
|
|
|
for index, raw := range request.RedirectURIs {
|
|
|
|
|
parsed, err := url.Parse(strings.TrimSpace(raw))
|
|
|
|
|
if err != nil || parsed.Scheme == "" || parsed.Host == "" || parsed.Fragment != "" || parsed.User != nil {
|
|
|
|
|
writeError(w, http.StatusBadRequest, "redirect URIs must be absolute URLs without fragments or credentials")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
localHTTP := (s.cfg.DevLogin || s.cfg.LocalLogin) && parsed.Scheme == "http" &&
|
|
|
|
|
(parsed.Hostname() == "localhost" || parsed.Hostname() == "127.0.0.1")
|
|
|
|
|
if parsed.Scheme != "https" && !localHTTP {
|
|
|
|
|
writeError(w, http.StatusBadRequest, "redirect URIs require HTTPS except localhost in development")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
request.RedirectURIs[index] = parsed.String()
|
|
|
|
|
if seen[parsed.String()] {
|
|
|
|
|
writeError(w, http.StatusBadRequest, "redirect URIs must be unique")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
seen[parsed.String()] = true
|
|
|
|
|
}
|
|
|
|
|
secret, _, hash, err := key.GenerateToken("glc_secret_")
|
|
|
|
|
if err != nil {
|
|
|
|
|
s.internalError(w, "generate client secret", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
app, err := s.store.ConfigureClient(r.Context(), claimsFrom(r).UserID, r.PathValue("id"), request.RedirectURIs, hash)
|
|
|
|
|
if err != nil {
|
|
|
|
|
s.handleStoreError(w, "configure client login", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
writeJSON(w, http.StatusOK, map[string]any{"application": app, "client_secret": secret})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Server) inviteUser(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var request struct {
|
|
|
|
|
TenantID string `json:"tenant_id"`
|
|
|
|
|
Email string `json:"email"`
|
|
|
|
|
DisplayName string `json:"display_name"`
|
2026-10-04 16:57:30 +02:00
|
|
|
RoleID string `json:"role_id"`
|
2026-10-01 23:15:57 +02:00
|
|
|
Password string `json:"password"`
|
|
|
|
|
}
|
|
|
|
|
if !decodeJSON(w, r, &request) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
address, err := mail.ParseAddress(strings.TrimSpace(request.Email))
|
|
|
|
|
if err != nil || address.Address != request.Email {
|
|
|
|
|
writeError(w, http.StatusBadRequest, "a valid plain email address is required")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
request.Email = strings.ToLower(address.Address)
|
|
|
|
|
request.DisplayName = strings.TrimSpace(request.DisplayName)
|
|
|
|
|
if request.DisplayName == "" {
|
|
|
|
|
request.DisplayName = request.Email
|
|
|
|
|
}
|
2026-10-04 16:57:30 +02:00
|
|
|
if strings.TrimSpace(request.RoleID) == "" {
|
|
|
|
|
writeError(w, http.StatusBadRequest, "role_id is required")
|
2026-10-01 23:15:57 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
passwordHash, err := password.Hash(request.Password)
|
|
|
|
|
if err != nil {
|
|
|
|
|
writeError(w, http.StatusBadRequest, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-10-04 16:57:30 +02:00
|
|
|
user, err := s.store.InviteUser(r.Context(), claimsFrom(r).UserID, request.TenantID, request.Email, request.DisplayName, request.RoleID, passwordHash)
|
2026-10-01 23:15:57 +02:00
|
|
|
if err != nil {
|
|
|
|
|
s.handleStoreError(w, "invite user", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
feat: account menu, database roles, and single logout
Bundles the console work from the current branch: the header account
control, database-backed roles and permissions, and signing out across
the applications that share this sign-in.
The account control replaces a separate identity chip and a separate
sign-out button, which meant the header carried two controls describing
one fact. web/account.js is mounted into every page by a placeholder and
owns the keyboard handling, the outside-click behaviour and the sign-out
form once. It also fills the trigger's name, which the previous markup
left empty.
Signing out revokes the hosted-login bearer sessions this installation
issued, then redirects to the provider's end_session_endpoint when the
discovery document advertises one. Applications that authenticate
against this installation therefore lose their session too; one pointed
straight at an identity provider is out of its reach and is unchanged.
Roles and permissions become rows rather than a closed enum, so a tenant
can extend the catalog without a migration, and a role can no longer be
granted a capability that is not in it. requireSameOrigin now compares
against the origin the request arrived on instead of the configured base
URL, which defaulted to http://localhost:8080 and so rejected every
write in a deployment that never set it.
Two bugs found while verifying in a browser:
- A profile picture never rendered. avatarURL built an absolute URL
from the configured base URL, and the page's own img-src 'self'
blocked it. The console's own pages now take a root-relative path;
only /connect/userinfo, read from another origin, needs an absolute
one, and it follows the request.
- absoluteURL preferred the configuration over the request while its
comment claimed the opposite, which also misdirected the logout
redirect.
Card layout: the footer was one two-column row shared between a label
and a forty-character identifier, so the identifier took the width and
crushed its label into three stacked words. Each pair is a line now, the
value truncates with the full text in the title, and cards align their
footers across a row.
2026-10-05 19:18:07 +02:00
|
|
|
user.AvatarURL = s.avatarPath(user.AvatarKey)
|
2026-10-01 23:15:57 +02:00
|
|
|
writeJSON(w, http.StatusCreated, user)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Server) setUserAccess(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var request struct {
|
|
|
|
|
TenantID string `json:"tenant_id"`
|
2026-10-04 16:57:30 +02:00
|
|
|
RoleID string `json:"role_id"`
|
2026-10-01 23:15:57 +02:00
|
|
|
ApplicationIDs []string `json:"application_ids"`
|
|
|
|
|
}
|
|
|
|
|
if !decodeJSON(w, r, &request) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-10-04 16:57:30 +02:00
|
|
|
if strings.TrimSpace(request.RoleID) == "" {
|
|
|
|
|
writeError(w, http.StatusBadRequest, "role_id is required")
|
2026-10-01 23:15:57 +02:00
|
|
|
return
|
|
|
|
|
}
|
2026-10-04 16:57:30 +02:00
|
|
|
if err := s.store.SetUserAccess(r.Context(), claimsFrom(r).UserID, request.TenantID, r.PathValue("id"), request.RoleID, request.ApplicationIDs); err != nil {
|
2026-10-01 23:15:57 +02:00
|
|
|
s.handleStoreError(w, "update user access", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Server) approveUser(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
var request struct {
|
|
|
|
|
TenantID string `json:"tenant_id"`
|
2026-10-04 16:57:30 +02:00
|
|
|
RoleID string `json:"role_id"`
|
2026-10-01 23:15:57 +02:00
|
|
|
ApplicationIDs []string `json:"application_ids"`
|
|
|
|
|
}
|
|
|
|
|
if !decodeJSON(w, r, &request) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-10-04 16:57:30 +02:00
|
|
|
if strings.TrimSpace(request.RoleID) == "" {
|
|
|
|
|
writeError(w, http.StatusBadRequest, "role_id is required")
|
2026-10-01 23:15:57 +02:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := s.store.ApprovePendingUser(
|
|
|
|
|
r.Context(), claimsFrom(r).UserID, request.TenantID, r.PathValue("id"),
|
2026-10-04 16:57:30 +02:00
|
|
|
request.RoleID, request.ApplicationIDs,
|
2026-10-01 23:15:57 +02:00
|
|
|
); err != nil {
|
|
|
|
|
s.handleStoreError(w, "approve pending user", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
|
}
|