Add configuration option to disable signup
close #51 Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
@@ -21,8 +21,9 @@ import (
|
|||||||
|
|
||||||
type (
|
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 {
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
@@ -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))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,10 +32,11 @@ import (
|
|||||||
|
|
||||||
type (
|
type (
|
||||||
Service struct {
|
Service struct {
|
||||||
pg *pg.Client
|
pg *pg.Client
|
||||||
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}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user