feat: multi-tenancy, login, per-org SSO and i18n (M2) #46
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")
|
||||
}
|
||||
|
||||
@@ -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
|
||||
TotpSecret []byte
|
||||
TotpEnabledAt *time.Time
|
||||
Locale *string
|
||||
}
|
||||
@@ -28,6 +28,7 @@ type Querier interface {
|
||||
ListUserOrgs(ctx context.Context, userID uuid.UUID) ([]ListUserOrgsRow, error)
|
||||
RemoveMembership(ctx context.Context, arg RemoveMembershipParams) (int64, error)
|
||||
SetPasswordHash(ctx context.Context, arg SetPasswordHashParams) error
|
||||
SetUserLocale(ctx context.Context, arg SetUserLocaleParams) error
|
||||
}
|
||||
|
||||
var _ Querier = (*Queries)(nil)
|
||||
@@ -1,5 +1,5 @@
|
||||
-- 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 *;
|
||||
|
||||
-- name: GetUser :one
|
||||
@@ -22,3 +22,6 @@ UPDATE users SET totp_secret = NULL, totp_enabled_at = NULL, updated_at = now()
|
||||
|
||||
-- name: DeleteUser :execrows
|
||||
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
|
||||
INSERT INTO users (email, display_name, password_hash) VALUES ($1, $2, $3)
|
||||
RETURNING id, email, display_name, password_hash, disabled_at, created_at, updated_at, totp_secret, totp_enabled_at
|
||||
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, locale
|
||||
`
|
||||
|
||||
type CreateUserParams struct {
|
||||
Email string
|
||||
DisplayName string
|
||||
PasswordHash *string
|
||||
Locale *string
|
||||
}
|
||||
|
||||
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
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
@@ -46,6 +52,7 @@ func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, e
|
||||
&i.UpdatedAt,
|
||||
&i.TotpSecret,
|
||||
&i.TotpEnabledAt,
|
||||
&i.Locale,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -86,7 +93,7 @@ func (q *Queries) EnableTOTP(ctx context.Context, arg EnableTOTPParams) error {
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -102,12 +109,13 @@ func (q *Queries) GetUser(ctx context.Context, id uuid.UUID) (User, error) {
|
||||
&i.UpdatedAt,
|
||||
&i.TotpSecret,
|
||||
&i.TotpEnabledAt,
|
||||
&i.Locale,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -123,6 +131,7 @@ func (q *Queries) GetUserByEmail(ctx context.Context, email string) (User, error
|
||||
&i.UpdatedAt,
|
||||
&i.TotpSecret,
|
||||
&i.TotpEnabledAt,
|
||||
&i.Locale,
|
||||
)
|
||||
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)
|
||||
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