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

@@ -21,8 +21,9 @@ import (
type (
authConfig struct {
Cookie cookieConfig `json:"cookie"`
Password passwordConfig `json:"password"`
Cookie cookieConfig `json:"cookie"`
Password passwordConfig `json:"password"`
DisableSignup bool `json:"disable-signup"`
}
cookieConfig struct {

View File

@@ -87,6 +87,7 @@ func New() *Implm {
Duration: 24,
Domain: "localhost",
},
DisableSignup: false,
},
AWS: awsConfig{
Region: "us-east-1",
@@ -172,6 +173,7 @@ func (impl *Implm) Run(
hp,
impl.cfg.Auth.Cookie.Secret,
impl.cfg.Hostname,
impl.cfg.Auth.DisableSignup,
)
if err != nil {
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
}
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))
}

View File

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