Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
dklimpel committed Mar 20, 2020
1 parent 4bc5bf2 commit e5e9dd5
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 44 deletions.
1 change: 1 addition & 0 deletions changelog.d/7118.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow server admins to define and enforce a password policy (MSC2000).
4 changes: 1 addition & 3 deletions synapse/api/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,7 @@ def __init__(
errcode=Codes.WEAK_PASSWORD,
):
super(PasswordRefusedError, self).__init__(
code=400,
msg=msg,
errcode=errcode,
code=400, msg=msg, errcode=errcode,
)


Expand Down
16 changes: 8 additions & 8 deletions synapse/handlers/password_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,35 +57,35 @@ def validate_password(self, password):
)

if (
self.policy.get("require_digit", False) and
self.regexp_digit.search(password) is None
self.policy.get("require_digit", False)
and self.regexp_digit.search(password) is None
):
raise PasswordRefusedError(
msg="The password must include at least one digit",
errcode=Codes.PASSWORD_NO_DIGIT,
)

if (
self.policy.get("require_symbol", False) and
self.regexp_symbol.search(password) is None
self.policy.get("require_symbol", False)
and self.regexp_symbol.search(password) is None
):
raise PasswordRefusedError(
msg="The password must include at least one symbol",
errcode=Codes.PASSWORD_NO_SYMBOL,
)

if (
self.policy.get("require_uppercase", False) and
self.regexp_uppercase.search(password) is None
self.policy.get("require_uppercase", False)
and self.regexp_uppercase.search(password) is None
):
raise PasswordRefusedError(
msg="The password must include at least one uppercase letter",
errcode=Codes.PASSWORD_NO_UPPERCASE,
)

if (
self.policy.get("require_lowercase", False) and
self.regexp_lowercase.search(password) is None
self.policy.get("require_lowercase", False)
and self.regexp_lowercase.search(password) is None
):
raise PasswordRefusedError(
msg="The password must include at least one lowercase letter",
Expand Down
2 changes: 1 addition & 1 deletion synapse/rest/client/v2_alpha/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ async def on_POST(self, request):
or len(body["password"]) > 512
):
raise SynapseError(400, "Invalid password")
self.password_policy_handler.validate_password(body['password'])
self.password_policy_handler.validate_password(body["password"])

desired_username = None
if "username" in body:
Expand Down
2 changes: 1 addition & 1 deletion synapse/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def build_DEPENDENCY(self)
"account_validity_handler",
"saml_handler",
"event_client_serializer",
'password_policy_handler',
"password_policy_handler",
"storage",
]

Expand Down
60 changes: 29 additions & 31 deletions tests/rest/client/v2_alpha/test_password_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,23 @@ def make_homeserver(self, reactor, clock):
def test_get_policy(self):
"""Tests if the /password_policy endpoint returns the configured policy."""

request, channel = self.make_request("GET", "/_matrix/client/r0/password_policy")
request, channel = self.make_request(
"GET", "/_matrix/client/r0/password_policy"
)
self.render(request)

self.assertEqual(channel.code, 200, channel.result)
self.assertEqual(channel.json_body, {
"m.minimum_length": 10,
"m.require_digit": True,
"m.require_symbol": True,
"m.require_lowercase": True,
"m.require_uppercase": True,
}, channel.result)
self.assertEqual(
channel.json_body,
{
"m.minimum_length": 10,
"m.require_digit": True,
"m.require_symbol": True,
"m.require_lowercase": True,
"m.require_uppercase": True,
},
channel.result,
)

def test_password_too_short(self):
request_data = json.dumps({"username": "kermit", "password": "shorty"})
Expand All @@ -89,9 +95,7 @@ def test_password_too_short(self):

self.assertEqual(channel.code, 400, channel.result)
self.assertEqual(
channel.json_body["errcode"],
Codes.PASSWORD_TOO_SHORT,
channel.result,
channel.json_body["errcode"], Codes.PASSWORD_TOO_SHORT, channel.result,
)

def test_password_no_digit(self):
Expand All @@ -101,9 +105,7 @@ def test_password_no_digit(self):

self.assertEqual(channel.code, 400, channel.result)
self.assertEqual(
channel.json_body["errcode"],
Codes.PASSWORD_NO_DIGIT,
channel.result,
channel.json_body["errcode"], Codes.PASSWORD_NO_DIGIT, channel.result,
)

def test_password_no_symbol(self):
Expand All @@ -113,9 +115,7 @@ def test_password_no_symbol(self):

self.assertEqual(channel.code, 400, channel.result)
self.assertEqual(
channel.json_body["errcode"],
Codes.PASSWORD_NO_SYMBOL,
channel.result,
channel.json_body["errcode"], Codes.PASSWORD_NO_SYMBOL, channel.result,
)

def test_password_no_uppercase(self):
Expand All @@ -125,9 +125,7 @@ def test_password_no_uppercase(self):

self.assertEqual(channel.code, 400, channel.result)
self.assertEqual(
channel.json_body["errcode"],
Codes.PASSWORD_NO_UPPERCASE,
channel.result,
channel.json_body["errcode"], Codes.PASSWORD_NO_UPPERCASE, channel.result,
)

def test_password_no_lowercase(self):
Expand All @@ -137,9 +135,7 @@ def test_password_no_lowercase(self):

self.assertEqual(channel.code, 400, channel.result)
self.assertEqual(
channel.json_body["errcode"],
Codes.PASSWORD_NO_LOWERCASE,
channel.result,
channel.json_body["errcode"], Codes.PASSWORD_NO_LOWERCASE, channel.result,
)

def test_password_compliant(self):
Expand All @@ -161,14 +157,16 @@ def test_password_change(self):
user_id = self.register_user("kermit", compliant_password)
tok = self.login("kermit", compliant_password)

request_data = json.dumps({
"new_password": not_compliant_password,
"auth": {
"password": compliant_password,
"type": LoginType.PASSWORD,
"user": user_id,
}
})
request_data = json.dumps(
{
"new_password": not_compliant_password,
"auth": {
"password": compliant_password,
"type": LoginType.PASSWORD,
"user": user_id,
},
}
)
request, channel = self.make_request(
"POST",
"/_matrix/client/r0/account/password",
Expand Down

0 comments on commit e5e9dd5

Please sign in to comment.