feat(auth): persist preferred language per user and session
Refs #42 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
7dd1f87ffe
commit
dde7b890b8
7 files changed
+60
-15
No files matched your search
@@ -22,6 +22,7 @@ type Session struct {
|
|||||||
Token string
|
Token string
|
||||||
UserID uuid.UUID
|
UserID uuid.UUID
|
||||||
OrgID uuid.UUID
|
OrgID uuid.UUID
|
||||||
|
Locale string
|
||||||
CSRF string
|
CSRF string
|
||||||
CreatedAt time.Time
|
CreatedAt time.Time
|
||||||
}
|
}
|
||||||
@@ -56,11 +57,12 @@ func RandomToken() string {
|
|||||||
return base64.RawURLEncoding.EncodeToString(b)
|
return base64.RawURLEncoding.EncodeToString(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Sessions) Create(ctx context.Context, user, org uuid.UUID) (*Session, error) {
|
func (s *Sessions) Create(ctx context.Context, user, org uuid.UUID, locale string) (*Session, error) {
|
||||||
sess := &Session{
|
sess := &Session{
|
||||||
Token: RandomToken(),
|
Token: RandomToken(),
|
||||||
UserID: user,
|
UserID: user,
|
||||||
OrgID: org,
|
OrgID: org,
|
||||||
|
Locale: locale,
|
||||||
CSRF: RandomToken(),
|
CSRF: RandomToken(),
|
||||||
CreatedAt: s.now(),
|
CreatedAt: s.now(),
|
||||||
}
|
}
|
||||||
@@ -69,6 +71,7 @@ func (s *Sessions) Create(ctx context.Context, user, org uuid.UUID) (*Session, e
|
|||||||
p.HSet(ctx, k, map[string]any{
|
p.HSet(ctx, k, map[string]any{
|
||||||
"user": sess.UserID.String(),
|
"user": sess.UserID.String(),
|
||||||
"org": orgString(org),
|
"org": orgString(org),
|
||||||
|
"locale": locale,
|
||||||
"csrf": sess.CSRF,
|
"csrf": sess.CSRF,
|
||||||
"created": sess.CreatedAt.Unix(),
|
"created": sess.CreatedAt.Unix(),
|
||||||
})
|
})
|
||||||
@@ -120,6 +123,14 @@ func (s *Sessions) SwitchOrg(ctx context.Context, sess *Session, org uuid.UUID)
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Sessions) SetLocale(ctx context.Context, sess *Session, locale string) error {
|
||||||
|
if err := s.rdb.HSet(ctx, key(sess.Token), "locale", locale).Err(); err != nil {
|
||||||
|
return fmt.Errorf("auth: set locale: %w", err)
|
||||||
|
}
|
||||||
|
sess.Locale = locale
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Sessions) Delete(ctx context.Context, sess *Session) error {
|
func (s *Sessions) Delete(ctx context.Context, sess *Session) error {
|
||||||
k := key(sess.Token)
|
k := key(sess.Token)
|
||||||
_, err := s.rdb.TxPipelined(ctx, func(p redis.Pipeliner) error {
|
_, err := s.rdb.TxPipelined(ctx, func(p redis.Pipeliner) error {
|
||||||
@@ -163,7 +174,7 @@ func decode(token string, v map[string]string) (*Session, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &Session{Token: token, UserID: user, OrgID: org, CSRF: v["csrf"], CreatedAt: time.Unix(created, 0)}, nil
|
return &Session{Token: token, UserID: user, OrgID: org, Locale: v["locale"], CSRF: v["csrf"], CreatedAt: time.Unix(created, 0)}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func orgString(id uuid.UUID) string {
|
func orgString(id uuid.UUID) string {
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ func TestSessionRoundTrip(t *testing.T) {
|
|||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
user, org := uuid.New(), uuid.New()
|
user, org := uuid.New(), uuid.New()
|
||||||
|
|
||||||
created, err := s.Create(ctx, user, org)
|
created, err := s.Create(ctx, user, org, "de")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -42,7 +42,7 @@ func TestSessionRoundTrip(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if got.UserID != user || got.OrgID != org || got.CSRF != created.CSRF {
|
if got.UserID != user || got.OrgID != org || got.Locale != "de" || got.CSRF != created.CSRF {
|
||||||
t.Fatalf("mismatch: %+v vs %+v", got, created)
|
t.Fatalf("mismatch: %+v vs %+v", got, created)
|
||||||
}
|
}
|
||||||
if !got.CheckCSRF(created.CSRF) || got.CheckCSRF("") || got.CheckCSRF("x") {
|
if !got.CheckCSRF(created.CSRF) || got.CheckCSRF("") || got.CheckCSRF("x") {
|
||||||
@@ -59,7 +59,7 @@ func TestSessionRoundTrip(t *testing.T) {
|
|||||||
func TestSessionAbsoluteLifetime(t *testing.T) {
|
func TestSessionAbsoluteLifetime(t *testing.T) {
|
||||||
s, now := testSessions(t)
|
s, now := testSessions(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
sess, err := s.Create(ctx, uuid.New(), uuid.Nil)
|
sess, err := s.Create(ctx, uuid.New(), uuid.Nil, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -73,9 +73,9 @@ func TestDeleteAllForUser(t *testing.T) {
|
|||||||
s, _ := testSessions(t)
|
s, _ := testSessions(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
user := uuid.New()
|
user := uuid.New()
|
||||||
a, _ := s.Create(ctx, user, uuid.Nil)
|
a, _ := s.Create(ctx, user, uuid.Nil, "")
|
||||||
b, _ := s.Create(ctx, user, uuid.Nil)
|
b, _ := s.Create(ctx, user, uuid.Nil, "")
|
||||||
other, _ := s.Create(ctx, uuid.New(), uuid.Nil)
|
other, _ := s.Create(ctx, uuid.New(), uuid.Nil, "")
|
||||||
|
|
||||||
if err := s.DeleteAllForUser(ctx, user); err != nil {
|
if err := s.DeleteAllForUser(ctx, user); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -93,7 +93,7 @@ func TestDeleteAllForUser(t *testing.T) {
|
|||||||
func TestTokenNotStoredInPlain(t *testing.T) {
|
func TestTokenNotStoredInPlain(t *testing.T) {
|
||||||
s, _ := testSessions(t)
|
s, _ := testSessions(t)
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
sess, _ := s.Create(ctx, uuid.New(), uuid.Nil)
|
sess, _ := s.Create(ctx, uuid.New(), uuid.Nil, "")
|
||||||
if n, _ := s.rdb.Exists(ctx, "session:"+sess.Token).Result(); n != 0 {
|
if n, _ := s.rdb.Exists(ctx, "session:"+sess.Token).Result(); n != 0 {
|
||||||
t.Fatal("raw token used as redis key")
|
t.Fatal("raw token used as redis key")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
-- +goose Up
|
||||||
|
-- Validated against the supported catalogs in code; the check only guards the format.
|
||||||
|
ALTER TABLE users ADD COLUMN locale text CHECK (locale ~ '^[a-z]{2}(-[A-Z]{2})?$');
|
||||||
|
|
||||||
|
-- +goose Down
|
||||||
|
ALTER TABLE users DROP COLUMN locale;
|
||||||
@@ -36,4 +36,5 @@ type User struct {
|
|||||||
UpdatedAt time.Time
|
UpdatedAt time.Time
|
||||||
TotpSecret []byte
|
TotpSecret []byte
|
||||||
TotpEnabledAt *time.Time
|
TotpEnabledAt *time.Time
|
||||||
|
Locale *string
|
||||||
}
|
}
|
||||||
@@ -28,6 +28,7 @@ type Querier interface {
|
|||||||
ListUserOrgs(ctx context.Context, userID uuid.UUID) ([]ListUserOrgsRow, error)
|
ListUserOrgs(ctx context.Context, userID uuid.UUID) ([]ListUserOrgsRow, error)
|
||||||
RemoveMembership(ctx context.Context, arg RemoveMembershipParams) (int64, error)
|
RemoveMembership(ctx context.Context, arg RemoveMembershipParams) (int64, error)
|
||||||
SetPasswordHash(ctx context.Context, arg SetPasswordHashParams) error
|
SetPasswordHash(ctx context.Context, arg SetPasswordHashParams) error
|
||||||
|
SetUserLocale(ctx context.Context, arg SetUserLocaleParams) error
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ Querier = (*Queries)(nil)
|
var _ Querier = (*Queries)(nil)
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
-- name: CreateUser :one
|
-- name: CreateUser :one
|
||||||
INSERT INTO users (email, display_name, password_hash) VALUES ($1, $2, $3)
|
INSERT INTO users (email, display_name, password_hash, locale) VALUES ($1, $2, $3, $4)
|
||||||
RETURNING *;
|
RETURNING *;
|
||||||
|
|
||||||
-- name: GetUser :one
|
-- name: GetUser :one
|
||||||
@@ -22,3 +22,6 @@ UPDATE users SET totp_secret = NULL, totp_enabled_at = NULL, updated_at = now()
|
|||||||
|
|
||||||
-- name: DeleteUser :execrows
|
-- name: DeleteUser :execrows
|
||||||
DELETE FROM users WHERE id = $1;
|
DELETE FROM users WHERE id = $1;
|
||||||
|
|
||||||
|
-- name: SetUserLocale :exec
|
||||||
|
UPDATE users SET locale = $2, updated_at = now() WHERE id = $1;
|
||||||
@@ -23,18 +23,24 @@ func (q *Queries) AnyUserExists(ctx context.Context) (bool, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const createUser = `-- name: CreateUser :one
|
const createUser = `-- name: CreateUser :one
|
||||||
INSERT INTO users (email, display_name, password_hash) VALUES ($1, $2, $3)
|
INSERT INTO users (email, display_name, password_hash, locale) VALUES ($1, $2, $3, $4)
|
||||||
RETURNING id, email, display_name, password_hash, disabled_at, created_at, updated_at, totp_secret, totp_enabled_at
|
RETURNING id, email, display_name, password_hash, disabled_at, created_at, updated_at, totp_secret, totp_enabled_at, locale
|
||||||
`
|
`
|
||||||
|
|
||||||
type CreateUserParams struct {
|
type CreateUserParams struct {
|
||||||
Email string
|
Email string
|
||||||
DisplayName string
|
DisplayName string
|
||||||
PasswordHash *string
|
PasswordHash *string
|
||||||
|
Locale *string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, error) {
|
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, error) {
|
||||||
row := q.db.QueryRow(ctx, createUser, arg.Email, arg.DisplayName, arg.PasswordHash)
|
row := q.db.QueryRow(ctx, createUser,
|
||||||
|
arg.Email,
|
||||||
|
arg.DisplayName,
|
||||||
|
arg.PasswordHash,
|
||||||
|
arg.Locale,
|
||||||
|
)
|
||||||
var i User
|
var i User
|
||||||
err := row.Scan(
|
err := row.Scan(
|
||||||
&i.ID,
|
&i.ID,
|
||||||
@@ -46,6 +52,7 @@ func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, e
|
|||||||
&i.UpdatedAt,
|
&i.UpdatedAt,
|
||||||
&i.TotpSecret,
|
&i.TotpSecret,
|
||||||
&i.TotpEnabledAt,
|
&i.TotpEnabledAt,
|
||||||
|
&i.Locale,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
@@ -86,7 +93,7 @@ func (q *Queries) EnableTOTP(ctx context.Context, arg EnableTOTPParams) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const getUser = `-- name: GetUser :one
|
const getUser = `-- name: GetUser :one
|
||||||
SELECT id, email, display_name, password_hash, disabled_at, created_at, updated_at, totp_secret, totp_enabled_at FROM users WHERE id = $1
|
SELECT id, email, display_name, password_hash, disabled_at, created_at, updated_at, totp_secret, totp_enabled_at, locale FROM users WHERE id = $1
|
||||||
`
|
`
|
||||||
|
|
||||||
func (q *Queries) GetUser(ctx context.Context, id uuid.UUID) (User, error) {
|
func (q *Queries) GetUser(ctx context.Context, id uuid.UUID) (User, error) {
|
||||||
@@ -102,12 +109,13 @@ func (q *Queries) GetUser(ctx context.Context, id uuid.UUID) (User, error) {
|
|||||||
&i.UpdatedAt,
|
&i.UpdatedAt,
|
||||||
&i.TotpSecret,
|
&i.TotpSecret,
|
||||||
&i.TotpEnabledAt,
|
&i.TotpEnabledAt,
|
||||||
|
&i.Locale,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|
||||||
const getUserByEmail = `-- name: GetUserByEmail :one
|
const getUserByEmail = `-- name: GetUserByEmail :one
|
||||||
SELECT id, email, display_name, password_hash, disabled_at, created_at, updated_at, totp_secret, totp_enabled_at FROM users WHERE email = $1
|
SELECT id, email, display_name, password_hash, disabled_at, created_at, updated_at, totp_secret, totp_enabled_at, locale FROM users WHERE email = $1
|
||||||
`
|
`
|
||||||
|
|
||||||
func (q *Queries) GetUserByEmail(ctx context.Context, email string) (User, error) {
|
func (q *Queries) GetUserByEmail(ctx context.Context, email string) (User, error) {
|
||||||
@@ -123,6 +131,7 @@ func (q *Queries) GetUserByEmail(ctx context.Context, email string) (User, error
|
|||||||
&i.UpdatedAt,
|
&i.UpdatedAt,
|
||||||
&i.TotpSecret,
|
&i.TotpSecret,
|
||||||
&i.TotpEnabledAt,
|
&i.TotpEnabledAt,
|
||||||
|
&i.Locale,
|
||||||
)
|
)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
@@ -140,3 +149,17 @@ func (q *Queries) SetPasswordHash(ctx context.Context, arg SetPasswordHashParams
|
|||||||
_, err := q.db.Exec(ctx, setPasswordHash, arg.ID, arg.PasswordHash)
|
_, err := q.db.Exec(ctx, setPasswordHash, arg.ID, arg.PasswordHash)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const setUserLocale = `-- name: SetUserLocale :exec
|
||||||
|
UPDATE users SET locale = $2, updated_at = now() WHERE id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
type SetUserLocaleParams struct {
|
||||||
|
ID uuid.UUID
|
||||||
|
Locale *string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) SetUserLocale(ctx context.Context, arg SetUserLocaleParams) error {
|
||||||
|
_, err := q.db.Exec(ctx, setUserLocale, arg.ID, arg.Locale)
|
||||||
|
return err
|
||||||
|
}
|
||||||
Reference in new issue
Block a user