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
|
||||
UserID uuid.UUID
|
||||
OrgID uuid.UUID
|
||||
Locale string
|
||||
CSRF string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
@@ -56,11 +57,12 @@ func RandomToken() string {
|
||||
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{
|
||||
Token: RandomToken(),
|
||||
UserID: user,
|
||||
OrgID: org,
|
||||
Locale: locale,
|
||||
CSRF: RandomToken(),
|
||||
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{
|
||||
"user": sess.UserID.String(),
|
||||
"org": orgString(org),
|
||||
"locale": locale,
|
||||
"csrf": sess.CSRF,
|
||||
"created": sess.CreatedAt.Unix(),
|
||||
})
|
||||
@@ -120,6 +123,14 @@ func (s *Sessions) SwitchOrg(ctx context.Context, sess *Session, org uuid.UUID)
|
||||
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 {
|
||||
k := key(sess.Token)
|
||||
_, 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 {
|
||||
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 {
|
||||
|
||||
@@ -34,7 +34,7 @@ func TestSessionRoundTrip(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
user, org := uuid.New(), uuid.New()
|
||||
|
||||
created, err := s.Create(ctx, user, org)
|
||||
created, err := s.Create(ctx, user, org, "de")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -42,7 +42,7 @@ func TestSessionRoundTrip(t *testing.T) {
|
||||
if err != nil {
|
||||
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)
|
||||
}
|
||||
if !got.CheckCSRF(created.CSRF) || got.CheckCSRF("") || got.CheckCSRF("x") {
|
||||
@@ -59,7 +59,7 @@ func TestSessionRoundTrip(t *testing.T) {
|
||||
func TestSessionAbsoluteLifetime(t *testing.T) {
|
||||
s, now := testSessions(t)
|
||||
ctx := context.Background()
|
||||
sess, err := s.Create(ctx, uuid.New(), uuid.Nil)
|
||||
sess, err := s.Create(ctx, uuid.New(), uuid.Nil, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -73,9 +73,9 @@ func TestDeleteAllForUser(t *testing.T) {
|
||||
s, _ := testSessions(t)
|
||||
ctx := context.Background()
|
||||
user := uuid.New()
|
||||
a, _ := s.Create(ctx, user, uuid.Nil)
|
||||
b, _ := s.Create(ctx, user, uuid.Nil)
|
||||
other, _ := s.Create(ctx, uuid.New(), uuid.Nil)
|
||||
a, _ := s.Create(ctx, user, uuid.Nil, "")
|
||||
b, _ := s.Create(ctx, user, uuid.Nil, "")
|
||||
other, _ := s.Create(ctx, uuid.New(), uuid.Nil, "")
|
||||
|
||||
if err := s.DeleteAllForUser(ctx, user); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -93,7 +93,7 @@ func TestDeleteAllForUser(t *testing.T) {
|
||||
func TestTokenNotStoredInPlain(t *testing.T) {
|
||||
s, _ := testSessions(t)
|
||||
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 {
|
||||
t.Fatal("raw token used as redis key")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user