Everything stays UTC: ingest still records Unix nanoseconds, ClickHouse
still stores UTC, every API response is still RFC3339 with a Z, and
queries are evaluated exactly as before. This changes only how those
instants are written on screen, so two people in two timezones looking
at one log line see the same instant written two ways -- never two
different lines, and never a different sort order.
Where the preference lives differs by deployment, and the three cases
are genuinely different products rather than one with fallbacks:
- Local login: server-side per named user (display_timezone on users,
PUT /auth/timezone), so it follows the person across browsers and
survives logout. Self-service at the RoleViewer floor, same as the
password change -- a viewer is the role most likely to be *only*
reading logs, so gating it higher would make it useless.
- Public demo: sessionStorage, so every new session starts at UTC. A
shared account's visitors have nothing to do with each other.
- Neither: localStorage, since there's no per-user record to write to.
api/cmd/api/main.go now imports time/tzdata. The image is
distroless/static with no /usr/share/zoneinfo, so LoadLocation would
otherwise reject every real zone name and the validation would refuse
every valid input.
Two details worth knowing when reading $lib/time.ts. Sub-second digits
are copied verbatim from the source string rather than round-tripped
through a JS Date, which is millisecond-precision and would silently
drop six digits of a ClickHouse nanosecond timestamp; expanding a result
row shows the localized value and the full-precision UTC original
together. And chart axes format their own labels, because ECharts'
type: 'time' axis renders in the browser's zone with no override --
which today puts a chart's clock out of step with the table beside it.
Timestamps are detected by value, not by column name: query output is
arbitrary, so a column called "timestamp" holding something else must
not be mangled, and `stats max(timestamp) as newest` must still be
formatted.
Verified against real zones including both sides of a DST boundary
(America/New_York at -05:00 in January, -04:00 in July), a half-hour
offset, and date rollover.
174 lines
4.3 KiB
Go
174 lines
4.3 KiB
Go
package localauth
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/cairnobs/cairnobs/api/authz"
|
|
)
|
|
|
|
// fakeStore implements both store (handler.go) and sessionStore
|
|
// (authorizer.go) -- a real *Store satisfies both too, this is just the
|
|
// in-memory test double, same "fake enforces the same invariants the
|
|
// real pgx-backed Store does" posture dashboards/handler_test.go's
|
|
// fakeStore documents.
|
|
type fakeStore struct {
|
|
users map[string]*User // by id
|
|
hashes map[string]string
|
|
byUsername map[string]string // username -> id
|
|
sessions map[string]Session
|
|
nextID int
|
|
createErr error
|
|
}
|
|
|
|
func newFakeStore() *fakeStore {
|
|
return &fakeStore{
|
|
users: map[string]*User{},
|
|
hashes: map[string]string{},
|
|
byUsername: map[string]string{},
|
|
sessions: map[string]Session{},
|
|
}
|
|
}
|
|
|
|
func (f *fakeStore) CreateUser(_ context.Context, username, passwordHash string, role authz.Role) (*User, error) {
|
|
if f.createErr != nil {
|
|
return nil, f.createErr
|
|
}
|
|
if _, ok := f.byUsername[username]; ok {
|
|
return nil, ErrUsernameTaken
|
|
}
|
|
f.nextID++
|
|
id := "user-" + strconv.Itoa(f.nextID)
|
|
// DisplayTimezone mirrors the schema's NOT NULL DEFAULT 'UTC' (see
|
|
// migration 0042) -- a fake that left it empty would let a handler
|
|
// bug that drops the default pass unnoticed.
|
|
u := &User{ID: id, Username: username, Role: role, DisplayTimezone: "UTC", CreatedAt: time.Now()}
|
|
f.users[id] = u
|
|
f.hashes[id] = passwordHash
|
|
f.byUsername[username] = id
|
|
return u, nil
|
|
}
|
|
|
|
func (f *fakeStore) ListUsers(_ context.Context) ([]User, error) {
|
|
var out []User
|
|
for _, u := range f.users {
|
|
out = append(out, *u)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func (f *fakeStore) GetUserForLogin(_ context.Context, username string) (*User, string, error) {
|
|
id, ok := f.byUsername[username]
|
|
if !ok {
|
|
return nil, "", ErrNotFound
|
|
}
|
|
return f.users[id], f.hashes[id], nil
|
|
}
|
|
|
|
func (f *fakeStore) GetUserByID(_ context.Context, id string) (*User, error) {
|
|
u, ok := f.users[id]
|
|
if !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
return u, nil
|
|
}
|
|
|
|
func (f *fakeStore) DeleteUser(_ context.Context, id string) error {
|
|
u, ok := f.users[id]
|
|
if !ok {
|
|
return ErrNotFound
|
|
}
|
|
delete(f.users, id)
|
|
delete(f.hashes, id)
|
|
delete(f.byUsername, u.Username)
|
|
for hash, sess := range f.sessions {
|
|
if sess.UserID == id {
|
|
delete(f.sessions, hash)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeStore) SetPasswordHash(_ context.Context, userID, hash string) error {
|
|
if _, ok := f.users[userID]; !ok {
|
|
return ErrNotFound
|
|
}
|
|
f.hashes[userID] = hash
|
|
for h, sess := range f.sessions {
|
|
if sess.UserID == userID {
|
|
delete(f.sessions, h)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeStore) SetRole(_ context.Context, userID string, role authz.Role) error {
|
|
u, ok := f.users[userID]
|
|
if !ok {
|
|
return ErrNotFound
|
|
}
|
|
u.Role = role
|
|
for h, sess := range f.sessions {
|
|
if sess.UserID == userID {
|
|
delete(f.sessions, h)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SetDisplayTimezone deliberately does not touch f.sessions -- unlike
|
|
// SetRole/SetPasswordHash above, changing a rendering preference is not
|
|
// a reason to sign anyone out, and the test for that asserts it.
|
|
func (f *fakeStore) SetDisplayTimezone(_ context.Context, userID, tz string) error {
|
|
u, ok := f.users[userID]
|
|
if !ok {
|
|
return ErrNotFound
|
|
}
|
|
u.DisplayTimezone = tz
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeStore) CountLocalUsers(_ context.Context) (int, error) {
|
|
return len(f.users), nil
|
|
}
|
|
|
|
func (f *fakeStore) CountUsersWithRole(_ context.Context, role authz.Role) (int, error) {
|
|
n := 0
|
|
for _, u := range f.users {
|
|
if u.Role == role {
|
|
n++
|
|
}
|
|
}
|
|
return n, nil
|
|
}
|
|
|
|
func (f *fakeStore) GetPasswordHashByID(_ context.Context, id string) (string, error) {
|
|
if _, ok := f.users[id]; !ok {
|
|
return "", ErrNotFound
|
|
}
|
|
return f.hashes[id], nil
|
|
}
|
|
|
|
func (f *fakeStore) CreateSession(_ context.Context, userID, tenantID string, role authz.Role, ttl time.Duration) (string, error) {
|
|
raw, hash, err := newOpaqueToken()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
f.sessions[hash] = Session{UserID: userID, TenantID: tenantID, Role: role, ExpiresAt: time.Now().Add(ttl)}
|
|
return raw, nil
|
|
}
|
|
|
|
func (f *fakeStore) GetSession(_ context.Context, tokenHash string) (*Session, error) {
|
|
sess, ok := f.sessions[tokenHash]
|
|
if !ok || sess.ExpiresAt.Before(time.Now()) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return &sess, nil
|
|
}
|
|
|
|
func (f *fakeStore) DeleteSessionByHash(_ context.Context, tokenHash string) error {
|
|
delete(f.sessions, tokenHash)
|
|
return nil
|
|
}
|