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
+52
-11
@@ -30,8 +30,11 @@ type signupRequest struct {
|
||||
Email string `json:"email" binding:"required,email,max=320"`
|
||||
Password string `json:"password" binding:"required,max=1024"`
|
||||
DisplayName string `json:"display_name" binding:"max=200"`
|
||||
OrgName string `json:"org_name" binding:"required,max=200"`
|
||||
OrgSlug string `json:"org_slug" binding:"required,max=63"`
|
||||
// OrgName and OrgSlug create a first organization at once, when no
|
||||
// email confirmation is needed; otherwise the account creates one
|
||||
// after confirming.
|
||||
OrgName string `json:"org_name" binding:"max=200"`
|
||||
OrgSlug string `json:"org_slug" binding:"max=63"`
|
||||
}
|
||||
|
||||
func (s *Server) signup(c *gin.Context) {
|
||||
@@ -50,6 +53,15 @@ func (s *Server) signup(c *gin.Context) {
|
||||
fail(c, http.StatusForbidden, "signup_closed")
|
||||
return
|
||||
}
|
||||
if left, err := s.signupsLeft(c); err != nil {
|
||||
internalError(c, err)
|
||||
return
|
||||
} else if !left {
|
||||
fail(c, http.StatusTooManyRequests, "signup_limit")
|
||||
return
|
||||
}
|
||||
verify := s.verificationRequired()
|
||||
withOrg := !verify && strings.TrimSpace(req.OrgName) != "" && strings.TrimSpace(req.OrgSlug) != ""
|
||||
hash, err := auth.HashPassword(req.Password)
|
||||
if errors.Is(err, auth.ErrPasswordTooShort) {
|
||||
fail(c, http.StatusUnprocessableEntity, "password_too_short", "Min", auth.MinPasswordLength)
|
||||
@@ -63,20 +75,27 @@ func (s *Server) signup(c *gin.Context) {
|
||||
var user store.User
|
||||
err = s.db.Tx(ctx, tenant.Org(orgID, uuid.Nil), func(q *store.Queries) error {
|
||||
var err error
|
||||
user, err = q.CreateUser(ctx, store.CreateUserParams{
|
||||
params := store.CreateUserParams{
|
||||
Email: normalizeEmail(req.Email),
|
||||
DisplayName: strings.TrimSpace(req.DisplayName),
|
||||
PasswordHash: &hash,
|
||||
Locale: ptr(i18n.Lang(ctx)),
|
||||
})
|
||||
EmailKey: ptr(emailKey(req.Email)),
|
||||
}
|
||||
if !verify {
|
||||
params.EmailVerifiedAt = ptr(time.Now())
|
||||
}
|
||||
user, err = q.CreateUser(ctx, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := q.CreateOrg(ctx, store.CreateOrgParams{ID: orgID, Slug: strings.ToLower(req.OrgSlug), Name: strings.TrimSpace(req.OrgName)}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := q.AddMembership(ctx, store.AddMembershipParams{OrgID: orgID, UserID: user.ID, IsOwner: true}); err != nil {
|
||||
return err
|
||||
if withOrg {
|
||||
if _, err := q.CreateOrg(ctx, store.CreateOrgParams{ID: orgID, Slug: strings.ToLower(req.OrgSlug), Name: strings.TrimSpace(req.OrgName)}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := q.AddMembership(ctx, store.AddMembershipParams{OrgID: orgID, UserID: user.ID, IsOwner: true}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// Whoever sets up a self-hosted installation operates it.
|
||||
if s.cfg.Signup == config.SignupFirstUser && s.cfg.Edition == config.EditionSelfHosted {
|
||||
@@ -85,7 +104,7 @@ func (s *Server) signup(c *gin.Context) {
|
||||
return nil
|
||||
})
|
||||
switch {
|
||||
case isUniqueViolation(err, "users_email_key"):
|
||||
case isUniqueViolation(err, "users_email_key"), isUniqueViolation(err, "users_email_key_idx"):
|
||||
fail(c, http.StatusConflict, "email_taken")
|
||||
return
|
||||
case isUniqueViolation(err, "orgs_slug_key"):
|
||||
@@ -98,10 +117,27 @@ func (s *Server) signup(c *gin.Context) {
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
s.countSignup(c)
|
||||
if verify {
|
||||
auditActor(c, user.ID)
|
||||
if err := s.sendVerification(ctx, user); err != nil {
|
||||
s.log.ErrorContext(ctx, "sending verification failed", "error", err)
|
||||
fail(c, http.StatusBadGateway, "verification_mail_failed")
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"status": "verification_sent", "redirect": "/signup/sent"})
|
||||
return
|
||||
}
|
||||
if !withOrg {
|
||||
orgID = uuid.Nil
|
||||
}
|
||||
s.startSession(c, auth.NewSession{UserID: user.ID, OrgID: orgID, Locale: i18n.Lang(ctx), Method: auth.MethodPassword})
|
||||
}
|
||||
|
||||
func (s *Server) signupAllowed(ctx context.Context) (bool, error) {
|
||||
if s.signupClosedWithoutMail() {
|
||||
return false, nil
|
||||
}
|
||||
switch s.cfg.Signup {
|
||||
case config.SignupOpen:
|
||||
return true, nil
|
||||
@@ -159,11 +195,16 @@ func (s *Server) login(c *gin.Context) {
|
||||
invalidCredentials(c)
|
||||
return
|
||||
}
|
||||
// Only someone who knows the password learns of the suspension.
|
||||
// Only someone who knows the password learns of the suspension or a
|
||||
// missing confirmation.
|
||||
if user.DisabledAt != nil {
|
||||
fail(c, http.StatusForbidden, "account_suspended")
|
||||
return
|
||||
}
|
||||
if user.EmailVerifiedAt == nil {
|
||||
fail(c, http.StatusForbidden, "email_unverified")
|
||||
return
|
||||
}
|
||||
if rehash {
|
||||
s.rehashPassword(ctx, user.ID, req.Password)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user