Skip to content

Commit a64d995

Browse files
authored
Merge pull request #76 from icecream78/feature/password-config
Feature/password config
2 parents 0b446aa + 02629ed commit a64d995

File tree

5 files changed

+28
-11
lines changed

5 files changed

+28
-11
lines changed

api/admin.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package api
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"net/http"
78

89
"github.com/go-chi/chi"
10+
"github.com/gofrs/uuid"
911
"github.com/netlify/gotrue/models"
1012
"github.com/netlify/gotrue/storage"
11-
"github.com/gofrs/uuid"
1213
)
1314

1415
type adminUserParams struct {
@@ -94,6 +95,7 @@ func (a *API) adminUserUpdate(w http.ResponseWriter, r *http.Request) error {
9495
adminUser := getAdminUser(ctx)
9596
instanceID := getInstanceID(ctx)
9697
params, err := a.getAdminParams(r)
98+
config := getConfig(ctx)
9799
if err != nil {
98100
return err
99101
}
@@ -112,6 +114,10 @@ func (a *API) adminUserUpdate(w http.ResponseWriter, r *http.Request) error {
112114
}
113115

114116
if params.Password != "" {
117+
if len(params.Password) < config.PasswordMinLength {
118+
return fmt.Errorf("Password should be at least %d characters", config.PasswordMinLength)
119+
}
120+
115121
if terr := user.UpdatePassword(tx, params.Password); terr != nil {
116122
return terr
117123
}

api/signup.go

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/json"
66
"errors"
7+
"fmt"
78
"net/http"
89

910
"github.com/netlify/gotrue/metering"
@@ -40,6 +41,10 @@ func (a *API) Signup(w http.ResponseWriter, r *http.Request) error {
4041
if params.Password == "" {
4142
return unprocessableEntityError("Signup requires a valid password")
4243
}
44+
if len(params.Password) < config.PasswordMinLength {
45+
return unprocessableEntityError(fmt.Sprintf("Password should be at least %d characters", config.PasswordMinLength))
46+
}
47+
4348
if err := a.validateEmail(ctx, params.Email); err != nil {
4449
return err
4550
}

api/user.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package api
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"net/http"
67

8+
"github.com/gofrs/uuid"
79
"github.com/netlify/gotrue/models"
810
"github.com/netlify/gotrue/storage"
9-
"github.com/gofrs/uuid"
1011
)
1112

1213
// UserUpdateParams parameters for updating a user
@@ -80,6 +81,10 @@ func (a *API) UserUpdate(w http.ResponseWriter, r *http.Request) error {
8081
err = a.db.Transaction(func(tx *storage.Connection) error {
8182
var terr error
8283
if params.Password != "" {
84+
if len(params.Password) < config.PasswordMinLength {
85+
return unprocessableEntityError(fmt.Sprintf("Password should be at least %d characters", config.PasswordMinLength))
86+
}
87+
8388
if terr = user.UpdatePassword(tx, params.Password); terr != nil {
8489
return internalServerError("Error during password storage").WithInternalError(terr)
8590
}

conf/configuration.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,15 @@ type MailerConfiguration struct {
109109

110110
// Configuration holds all the per-instance configuration.
111111
type Configuration struct {
112-
SiteURL string `json:"site_url" split_words:"true" required:"true"`
113-
JWT JWTConfiguration `json:"jwt"`
114-
SMTP SMTPConfiguration `json:"smtp"`
115-
Mailer MailerConfiguration `json:"mailer"`
116-
External ProviderConfiguration `json:"external"`
117-
DisableSignup bool `json:"disable_signup" split_words:"true"`
118-
Webhook WebhookConfig `json:"webhook" split_words:"true"`
119-
Cookie struct {
112+
SiteURL string `json:"site_url" split_words:"true" required:"true"`
113+
PasswordMinLength int `json:"password_min_length" default:"6"`
114+
JWT JWTConfiguration `json:"jwt"`
115+
SMTP SMTPConfiguration `json:"smtp"`
116+
Mailer MailerConfiguration `json:"mailer"`
117+
External ProviderConfiguration `json:"external"`
118+
DisableSignup bool `json:"disable_signup" split_words:"true"`
119+
Webhook WebhookConfig `json:"webhook" split_words:"true"`
120+
Cookie struct {
120121
Key string `json:"key"`
121122
Duration int `json:"duration"`
122123
} `json:"cookies"`

hack/test.env

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ GOTRUE_JWT_AUD=api.netlify.com
44
GOTRUE_DB_DRIVER=mysql
55
GOTRUE_DB_AUTOMIGRATE=true
66
GOTRUE_DB_NAMESPACE=test
7-
DATABASE_URL="root@tcp(127.0.0.1:3306)/gotrue_test?parseTime=true&sql_mode=TRADITIONAL"
7+
DATABASE_URL="root@tcp(127.0.0.1:3306)/gotrue_test?parseTime=true&sql_mode=TRADITIONAL&multiStatements=true"
88
GOTRUE_API_HOST=localhost
99
PORT=9999
1010
GOTRUE_LOG_LEVEL=debug

0 commit comments

Comments
 (0)