-
Notifications
You must be signed in to change notification settings - Fork 398
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add required characters password strength check (#1323)
Adds the `GOTRUE_PASSWORD_REQUIRED_CHARACTERS` config option, which if set, will reject passwords that do not contain at least one character of each set of characters. It is defined like so: `abc...xyz:0123...89`. This means that at least one lowercase and one digit has to be present in the password to be accepted. All other characters are also allowed. To include the `:` character, escape it with `\:`. When a weak password is detected, the HTTP 429 error is sent with an additional JSON field `weak_password` that includes a `reasons` property -- an array of the strings: - `length` if the password is not long enough - `characters` if the password does not use all required character sets --------- Co-authored-by: Kang Ming <[email protected]>
- Loading branch information
1 parent
0540c7f
commit 3991bdb
Showing
7 changed files
with
286 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/supabase/gotrue/internal/conf" | ||
) | ||
|
||
func TestPasswordStrengthChecks(t *testing.T) { | ||
examples := []struct { | ||
MinLength int | ||
RequiredCharacters []string | ||
|
||
Password string | ||
Reasons []string | ||
}{ | ||
{ | ||
MinLength: 6, | ||
Password: "12345", | ||
Reasons: []string{ | ||
"length", | ||
}, | ||
}, | ||
{ | ||
MinLength: 6, | ||
RequiredCharacters: []string{ | ||
"a", | ||
"b", | ||
"c", | ||
}, | ||
Password: "123", | ||
Reasons: []string{ | ||
"length", | ||
"characters", | ||
}, | ||
}, | ||
{ | ||
MinLength: 6, | ||
RequiredCharacters: []string{ | ||
"a", | ||
"b", | ||
"c", | ||
}, | ||
Password: "a123", | ||
Reasons: []string{ | ||
"length", | ||
"characters", | ||
}, | ||
}, | ||
{ | ||
MinLength: 6, | ||
RequiredCharacters: []string{ | ||
"a", | ||
"b", | ||
"c", | ||
}, | ||
Password: "ab123", | ||
Reasons: []string{ | ||
"length", | ||
"characters", | ||
}, | ||
}, | ||
{ | ||
MinLength: 6, | ||
RequiredCharacters: []string{ | ||
"a", | ||
"b", | ||
"c", | ||
}, | ||
Password: "c123", | ||
Reasons: []string{ | ||
"length", | ||
"characters", | ||
}, | ||
}, | ||
{ | ||
MinLength: 6, | ||
RequiredCharacters: []string{ | ||
"a", | ||
"b", | ||
"c", | ||
}, | ||
Password: "abc123", | ||
Reasons: nil, | ||
}, | ||
} | ||
|
||
for i, example := range examples { | ||
api := &API{ | ||
config: &conf.GlobalConfiguration{ | ||
Password: conf.PasswordConfiguration{ | ||
MinLength: example.MinLength, | ||
RequiredCharacters: conf.PasswordRequiredCharacters(example.RequiredCharacters), | ||
}, | ||
}, | ||
} | ||
|
||
err := api.checkPasswordStrength(context.Background(), example.Password) | ||
if example.Reasons == nil { | ||
require.NoError(t, err, "Example %d failed with error", i) | ||
} else { | ||
require.Equal(t, err.(*WeakPasswordError).Reasons, example.Reasons, "Example %d failed with wrong reasons", i) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters