2026-10-01 23:15:57 +02:00
|
|
|
package store
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"database/sql"
|
|
|
|
|
"errors"
|
|
|
|
|
)
|
|
|
|
|
|
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
|
|
|
// EnsurePasswordUser creates or updates the local account behind
|
|
|
|
|
// GO_LOOSE_LOCAL_ADMIN_PASSWORD and bootstraps it into the installation.
|
|
|
|
|
//
|
|
|
|
|
// The local administrator is operator-supplied rather than created by a visitor,
|
|
|
|
|
// so when the installation has no system administrator yet it claims the role.
|
|
|
|
|
// Without that, a database-only deployment would have nobody able to create
|
|
|
|
|
// tenants or approve waiting-room users, because both require the role that only
|
|
|
|
|
// a first OIDC sign-in otherwise grants. The claim is a no-op once any system
|
|
|
|
|
// administrator exists, so an installation already owned by a Google account is
|
|
|
|
|
// left alone.
|
2026-10-01 23:15:57 +02:00
|
|
|
func (s *Store) EnsurePasswordUser(ctx context.Context, email, displayName, passwordHash, tenantSlug, appSlug string) error {
|
|
|
|
|
user, err := s.UpsertUser(ctx, "local:"+email, email, displayName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if _, err := s.db.ExecContext(ctx, `
|
|
|
|
|
UPDATE users
|
|
|
|
|
SET password_hash = $2, password_changed_at = now()
|
|
|
|
|
WHERE id = $1 AND password_hash IS NULL`, user.ID, passwordHash); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
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
|
|
|
if _, err := s.ClaimFirstSystemAdministrator(ctx, user.ID); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-10-01 23:15:57 +02:00
|
|
|
return s.Bootstrap(ctx, user.ID, tenantSlug, appSlug)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Store) PasswordUser(ctx context.Context, email string) (User, string, error) {
|
|
|
|
|
var user User
|
|
|
|
|
var passwordHash sql.NullString
|
|
|
|
|
err := s.db.QueryRowContext(ctx, `
|
|
|
|
|
SELECT id, email, display_name, password_hash
|
|
|
|
|
FROM users
|
|
|
|
|
WHERE lower(email) = lower($1)`, email).
|
|
|
|
|
Scan(&user.ID, &user.Email, &user.DisplayName, &passwordHash)
|
|
|
|
|
if errors.Is(err, sql.ErrNoRows) || (err == nil && !passwordHash.Valid) {
|
|
|
|
|
return User{}, "", ErrNotFound
|
|
|
|
|
}
|
|
|
|
|
return user, passwordHash.String, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-02 21:45:42 +02:00
|
|
|
func (s *Store) HasTenantAccess(ctx context.Context, userID, tenantSlug string) (bool, error) {
|
|
|
|
|
var allowed bool
|
|
|
|
|
err := s.db.QueryRowContext(ctx, `
|
|
|
|
|
SELECT EXISTS(
|
|
|
|
|
SELECT 1
|
|
|
|
|
FROM memberships m
|
|
|
|
|
JOIN tenants t ON t.id = m.tenant_id
|
|
|
|
|
WHERE m.user_id = $1 AND t.slug = $2
|
|
|
|
|
)`, userID, tenantSlug).Scan(&allowed)
|
|
|
|
|
return allowed, err
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-01 23:15:57 +02:00
|
|
|
func (s *Store) RecordLogin(ctx context.Context, userID *string, email, remoteAddress string, successful bool, reason string) error {
|
|
|
|
|
_, err := s.db.ExecContext(ctx, `
|
|
|
|
|
INSERT INTO user_login_events(user_id, email, remote_address, successful, reason)
|
|
|
|
|
VALUES ($1, lower($2), $3, $4, $5)`,
|
|
|
|
|
userID, email, remoteAddress, successful, reason)
|
|
|
|
|
return err
|
|
|
|
|
}
|