Add configuration option to disable signup

close #51

Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
gearnode
2025-03-19 16:43:49 +01:00
parent a72c0269bb
commit ffa4c93bfd
4 changed files with 32 additions and 10 deletions

View File

@@ -23,6 +23,7 @@ type (
authConfig struct { authConfig struct {
Cookie cookieConfig `json:"cookie"` Cookie cookieConfig `json:"cookie"`
Password passwordConfig `json:"password"` Password passwordConfig `json:"password"`
DisableSignup bool `json:"disable-signup"`
} }
cookieConfig struct { cookieConfig struct {

View File

@@ -87,6 +87,7 @@ func New() *Implm {
Duration: 24, Duration: 24,
Domain: "localhost", Domain: "localhost",
}, },
DisableSignup: false,
}, },
AWS: awsConfig{ AWS: awsConfig{
Region: "us-east-1", Region: "us-east-1",
@@ -172,6 +173,7 @@ func (impl *Implm) Run(
hp, hp,
impl.cfg.Auth.Cookie.Secret, impl.cfg.Auth.Cookie.Secret,
impl.cfg.Hostname, impl.cfg.Hostname,
impl.cfg.Auth.DisableSignup,
) )
if err != nil { if err != nil {
return fmt.Errorf("cannot create usrmgr service: %w", err) return fmt.Errorf("cannot create usrmgr service: %w", err)

View File

@@ -59,6 +59,12 @@ func SignUpHandler(usrmgrSvc *usrmgr.Service, authCfg AuthConfig) http.HandlerFu
return return
} }
var errSignupDisabled *usrmgr.ErrSignupDisabled
if errors.As(err, &errSignupDisabled) {
httpserver.RenderError(w, http.StatusBadRequest, fmt.Errorf("cannot register user: %w", err))
return
}
panic(fmt.Errorf("cannot register user: %w", err)) panic(fmt.Errorf("cannot register user: %w", err))
} }

View File

@@ -36,6 +36,7 @@ type (
hp *passwdhash.Profile hp *passwdhash.Profile
hostname string hostname string
tokenSecret string tokenSecret string
disableSignup bool
} }
ErrInvalidCredentials struct { ErrInvalidCredentials struct {
@@ -70,6 +71,8 @@ type (
message string message string
} }
ErrSignupDisabled struct{}
EmailConfirmationData struct { EmailConfirmationData struct {
UserID gid.GID `json:"uid"` UserID gid.GID `json:"uid"`
Email string `json:"email"` Email string `json:"email"`
@@ -138,18 +141,24 @@ func (e ErrInvalidTokenType) Error() string {
return e.message return e.message
} }
func (e ErrSignupDisabled) Error() string {
return "signup is disabled, contact the owner of the Probo instance"
}
func NewService( func NewService(
ctx context.Context, ctx context.Context,
pgClient *pg.Client, pgClient *pg.Client,
hp *passwdhash.Profile, hp *passwdhash.Profile,
tokenSecret string, tokenSecret string,
hostname string, hostname string,
disableSignup bool,
) (*Service, error) { ) (*Service, error) {
return &Service{ return &Service{
pg: pgClient, pg: pgClient,
hp: hp, hp: hp,
hostname: hostname, hostname: hostname,
tokenSecret: tokenSecret, tokenSecret: tokenSecret,
disableSignup: disableSignup,
}, nil }, nil
} }
@@ -157,6 +166,10 @@ func (s Service) SignUp(
ctx context.Context, ctx context.Context,
email, password, fullName string, email, password, fullName string,
) (*coredata.User, *coredata.Session, error) { ) (*coredata.User, *coredata.Session, error) {
if s.disableSignup {
return nil, nil, &ErrSignupDisabled{}
}
if !strings.Contains(email, "@") { if !strings.Contains(email, "@") {
return nil, nil, &ErrInvalidEmail{email} return nil, nil, &ErrInvalidEmail{email}
} }