feat: verified sign-up and several organizations per account
New accounts confirm their email address before signing in: sign-up mails a link valid for 24 hours, and its page confirms only when the visitor presses the button, so mail scanners opening links do not sign in. Confirming signs the account in; unconfirmed accounts are told so only after giving the right password, can ask for a new link, and are deleted after a week. UPTIME_SIGNUP_VERIFY requires confirmation always in the cloud edition and self-hosted wherever mail can be sent; without a mail server, sign-up that needs it stays closed. One account per mailbox: users.email_key drops +suffixes, and dots for Gmail, and is unique, including for accounts created through invitations and SSO, which count as confirmed. A client address (IPv6: its /64) may create UPTIME_SIGNUPS_PER_IP accounts a day, 2 by default; refused sign-ups do not count. Sign-up asks only for name, email and password. Accounts without an organization are led to create one; signed-in users create further organizations from the switcher, within the per-account limit, and POST /api/v1/orgs does the same. Closes #112 Closes #113 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
d644312f25
commit
87b2f2a256
29 files changed
+1698
-377
No files matched your search
+15
-13
@@ -389,19 +389,21 @@ type StatusPageMonitor struct {
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID uuid.UUID
|
||||
Email string
|
||||
DisplayName string
|
||||
PasswordHash *string
|
||||
DisabledAt *time.Time
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
TotpSecret []byte
|
||||
TotpEnabledAt *time.Time
|
||||
Locale *string
|
||||
IsAdmin bool
|
||||
DisabledReason string
|
||||
LimitOrgs *int32
|
||||
ID uuid.UUID
|
||||
Email string
|
||||
DisplayName string
|
||||
PasswordHash *string
|
||||
DisabledAt *time.Time
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
TotpSecret []byte
|
||||
TotpEnabledAt *time.Time
|
||||
Locale *string
|
||||
IsAdmin bool
|
||||
DisabledReason string
|
||||
LimitOrgs *int32
|
||||
EmailVerifiedAt *time.Time
|
||||
EmailKey *string
|
||||
}
|
||||
|
||||
type UserIdentity struct {
|
||||
|
||||
@@ -47,6 +47,7 @@ type Querier interface {
|
||||
ClearStatusPageGroups(ctx context.Context, pageID uuid.UUID) error
|
||||
ClearStatusPageMonitors(ctx context.Context, pageID uuid.UUID) error
|
||||
CountMonitors(ctx context.Context, orgID uuid.UUID) (int64, error)
|
||||
CountOwnedOrgs(ctx context.Context, userID uuid.UUID) (int64, error)
|
||||
CountRoleAssignments(ctx context.Context, roleID uuid.UUID) (int64, error)
|
||||
CountStatusPages(ctx context.Context, orgID uuid.UUID) (int64, error)
|
||||
CountUnusedRecoveryCodes(ctx context.Context, userID uuid.UUID) (int64, error)
|
||||
@@ -99,6 +100,7 @@ type Querier interface {
|
||||
DeleteSuccessEvidence(ctx context.Context, arg DeleteSuccessEvidenceParams) error
|
||||
DeleteUser(ctx context.Context, id uuid.UUID) (int64, error)
|
||||
DisableTOTP(ctx context.Context, id uuid.UUID) error
|
||||
EmailKeyTaken(ctx context.Context, emailKey *string) (bool, error)
|
||||
EnableTOTP(ctx context.Context, arg EnableTOTPParams) error
|
||||
EnqueueNotification(ctx context.Context, arg EnqueueNotificationParams) error
|
||||
// An empty name names a new region after its key and keeps an existing name.
|
||||
@@ -224,6 +226,7 @@ type Querier interface {
|
||||
MaintenanceForMonitors(ctx context.Context, ids []uuid.UUID) ([]MaintenanceForMonitorsRow, error)
|
||||
MakeAdmin(ctx context.Context, id uuid.UUID) error
|
||||
MarkDomainVerified(ctx context.Context, id uuid.UUID) error
|
||||
MarkEmailVerified(ctx context.Context, id uuid.UUID) (User, error)
|
||||
MarkInvitationAccepted(ctx context.Context, id uuid.UUID) (int64, error)
|
||||
MarkNotificationFailed(ctx context.Context, arg MarkNotificationFailedParams) error
|
||||
MarkNotificationSent(ctx context.Context, id uuid.UUID) error
|
||||
@@ -262,6 +265,8 @@ type Querier interface {
|
||||
// Delivered and given-up notifications; pending ones are still being worked.
|
||||
PurgeNotifications(ctx context.Context, createdAt time.Time) (int64, error)
|
||||
PurgeOrgCheckData(ctx context.Context, org uuid.UUID) error
|
||||
// Accounts never confirmed; they belong to no organization.
|
||||
PurgeUnverifiedUsers(ctx context.Context, createdAt time.Time) (int64, error)
|
||||
// Raw results bucketed by width since a point in time, for short ranges.
|
||||
RangeBuckets(ctx context.Context, arg RangeBucketsParams) ([]RangeBucketsRow, error)
|
||||
// The daily aggregate rebucketed by width, for ranges of months.
|
||||
|
||||
@@ -1,7 +1,21 @@
|
||||
-- name: CreateUser :one
|
||||
INSERT INTO users (email, display_name, password_hash, locale) VALUES ($1, $2, $3, $4)
|
||||
INSERT INTO users (email, display_name, password_hash, locale, email_key, email_verified_at)
|
||||
VALUES ($1, $2, $3, $4, sqlc.arg(email_key), sqlc.narg(email_verified_at))
|
||||
RETURNING *;
|
||||
|
||||
-- name: MarkEmailVerified :one
|
||||
UPDATE users SET email_verified_at = coalesce(email_verified_at, now()) WHERE id = $1
|
||||
RETURNING *;
|
||||
|
||||
-- name: PurgeUnverifiedUsers :execrows
|
||||
-- Accounts never confirmed; they belong to no organization.
|
||||
DELETE FROM users u
|
||||
WHERE u.email_verified_at IS NULL AND u.created_at < $1
|
||||
AND NOT EXISTS (SELECT 1 FROM memberships m WHERE m.user_id = u.id);
|
||||
|
||||
-- name: CountOwnedOrgs :one
|
||||
SELECT count(*) FROM memberships WHERE user_id = $1 AND is_owner;
|
||||
|
||||
-- name: GetUser :one
|
||||
SELECT * FROM users WHERE id = $1;
|
||||
|
||||
@@ -31,3 +45,6 @@ UPDATE users SET is_admin = $2, updated_at = now() WHERE email = $1;
|
||||
|
||||
-- name: MakeAdmin :exec
|
||||
UPDATE users SET is_admin = true, updated_at = now() WHERE id = $1;
|
||||
|
||||
-- name: EmailKeyTaken :one
|
||||
SELECT EXISTS (SELECT 1 FROM users WHERE email_key = $1);
|
||||
@@ -7,6 +7,7 @@ package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
@@ -22,16 +23,30 @@ func (q *Queries) AnyUserExists(ctx context.Context) (bool, error) {
|
||||
return exists, err
|
||||
}
|
||||
|
||||
const countOwnedOrgs = `-- name: CountOwnedOrgs :one
|
||||
SELECT count(*) FROM memberships WHERE user_id = $1 AND is_owner
|
||||
`
|
||||
|
||||
func (q *Queries) CountOwnedOrgs(ctx context.Context, userID uuid.UUID) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, countOwnedOrgs, userID)
|
||||
var count int64
|
||||
err := row.Scan(&count)
|
||||
return count, err
|
||||
}
|
||||
|
||||
const createUser = `-- name: CreateUser :one
|
||||
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, is_admin, disabled_reason, limit_orgs
|
||||
INSERT INTO users (email, display_name, password_hash, locale, email_key, email_verified_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING id, email, display_name, password_hash, disabled_at, created_at, updated_at, totp_secret, totp_enabled_at, locale, is_admin, disabled_reason, limit_orgs, email_verified_at, email_key
|
||||
`
|
||||
|
||||
type CreateUserParams struct {
|
||||
Email string
|
||||
DisplayName string
|
||||
PasswordHash *string
|
||||
Locale *string
|
||||
Email string
|
||||
DisplayName string
|
||||
PasswordHash *string
|
||||
Locale *string
|
||||
EmailKey *string
|
||||
EmailVerifiedAt *time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, error) {
|
||||
@@ -40,6 +55,8 @@ func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, e
|
||||
arg.DisplayName,
|
||||
arg.PasswordHash,
|
||||
arg.Locale,
|
||||
arg.EmailKey,
|
||||
arg.EmailVerifiedAt,
|
||||
)
|
||||
var i User
|
||||
err := row.Scan(
|
||||
@@ -56,6 +73,8 @@ func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, e
|
||||
&i.IsAdmin,
|
||||
&i.DisabledReason,
|
||||
&i.LimitOrgs,
|
||||
&i.EmailVerifiedAt,
|
||||
&i.EmailKey,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -81,6 +100,17 @@ func (q *Queries) DisableTOTP(ctx context.Context, id uuid.UUID) error {
|
||||
return err
|
||||
}
|
||||
|
||||
const emailKeyTaken = `-- name: EmailKeyTaken :one
|
||||
SELECT EXISTS (SELECT 1 FROM users WHERE email_key = $1)
|
||||
`
|
||||
|
||||
func (q *Queries) EmailKeyTaken(ctx context.Context, emailKey *string) (bool, error) {
|
||||
row := q.db.QueryRow(ctx, emailKeyTaken, emailKey)
|
||||
var exists bool
|
||||
err := row.Scan(&exists)
|
||||
return exists, err
|
||||
}
|
||||
|
||||
const enableTOTP = `-- name: EnableTOTP :exec
|
||||
UPDATE users SET totp_secret = $2, totp_enabled_at = now(), updated_at = now() WHERE id = $1
|
||||
`
|
||||
@@ -96,7 +126,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, locale, is_admin, disabled_reason, limit_orgs FROM users WHERE id = $1
|
||||
SELECT id, email, display_name, password_hash, disabled_at, created_at, updated_at, totp_secret, totp_enabled_at, locale, is_admin, disabled_reason, limit_orgs, email_verified_at, email_key FROM users WHERE id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetUser(ctx context.Context, id uuid.UUID) (User, error) {
|
||||
@@ -116,12 +146,14 @@ func (q *Queries) GetUser(ctx context.Context, id uuid.UUID) (User, error) {
|
||||
&i.IsAdmin,
|
||||
&i.DisabledReason,
|
||||
&i.LimitOrgs,
|
||||
&i.EmailVerifiedAt,
|
||||
&i.EmailKey,
|
||||
)
|
||||
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, locale, is_admin, disabled_reason, limit_orgs FROM users WHERE email = $1
|
||||
SELECT id, email, display_name, password_hash, disabled_at, created_at, updated_at, totp_secret, totp_enabled_at, locale, is_admin, disabled_reason, limit_orgs, email_verified_at, email_key FROM users WHERE email = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetUserByEmail(ctx context.Context, email string) (User, error) {
|
||||
@@ -141,6 +173,8 @@ func (q *Queries) GetUserByEmail(ctx context.Context, email string) (User, error
|
||||
&i.IsAdmin,
|
||||
&i.DisabledReason,
|
||||
&i.LimitOrgs,
|
||||
&i.EmailVerifiedAt,
|
||||
&i.EmailKey,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -154,6 +188,49 @@ func (q *Queries) MakeAdmin(ctx context.Context, id uuid.UUID) error {
|
||||
return err
|
||||
}
|
||||
|
||||
const markEmailVerified = `-- name: MarkEmailVerified :one
|
||||
UPDATE users SET email_verified_at = coalesce(email_verified_at, now()) WHERE id = $1
|
||||
RETURNING id, email, display_name, password_hash, disabled_at, created_at, updated_at, totp_secret, totp_enabled_at, locale, is_admin, disabled_reason, limit_orgs, email_verified_at, email_key
|
||||
`
|
||||
|
||||
func (q *Queries) MarkEmailVerified(ctx context.Context, id uuid.UUID) (User, error) {
|
||||
row := q.db.QueryRow(ctx, markEmailVerified, id)
|
||||
var i User
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Email,
|
||||
&i.DisplayName,
|
||||
&i.PasswordHash,
|
||||
&i.DisabledAt,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.TotpSecret,
|
||||
&i.TotpEnabledAt,
|
||||
&i.Locale,
|
||||
&i.IsAdmin,
|
||||
&i.DisabledReason,
|
||||
&i.LimitOrgs,
|
||||
&i.EmailVerifiedAt,
|
||||
&i.EmailKey,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const purgeUnverifiedUsers = `-- name: PurgeUnverifiedUsers :execrows
|
||||
DELETE FROM users u
|
||||
WHERE u.email_verified_at IS NULL AND u.created_at < $1
|
||||
AND NOT EXISTS (SELECT 1 FROM memberships m WHERE m.user_id = u.id)
|
||||
`
|
||||
|
||||
// Accounts never confirmed; they belong to no organization.
|
||||
func (q *Queries) PurgeUnverifiedUsers(ctx context.Context, createdAt time.Time) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, purgeUnverifiedUsers, createdAt)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const setPasswordHash = `-- name: SetPasswordHash :exec
|
||||
UPDATE users SET password_hash = $2, updated_at = now() WHERE id = $1
|
||||
`
|
||||
|
||||
Reference in new issue
Block a user