Add ability to enforce lowercase usernames on registration

This commit is contained in:
Maxime Dor 2017-09-26 14:49:32 +02:00
parent 4b9d9be7be
commit fbcf21b5b3
2 changed files with 25 additions and 2 deletions

View File

@ -26,10 +26,17 @@ password_providers:
- module: "rest_auth_provider.RestAuthProvider" - module: "rest_auth_provider.RestAuthProvider"
config: config:
endpoint: "http://change.me.example.com:12345" endpoint: "http://change.me.example.com:12345"
policy:
registration:
username:
enforceLowercase: false
``` ```
Replace the `endpoint` value with the appropriate value. Replace the `endpoint` value with the appropriate value.
If you would like to avoid user creating account with upper case letter in their usernames,
use the `enforceLowercase` config item.
## Use ## Use
1. Install, configure, restart synapse 1. Install, configure, restart synapse
2. Try to login with a valid username and password for the endpoint configured 2. Try to login with a valid username and password for the endpoint configured

View File

@ -35,6 +35,10 @@ class RestAuthProvider(object):
raise RuntimeError('Missing endpoint config') raise RuntimeError('Missing endpoint config')
self.endpoint = config.endpoint self.endpoint = config.endpoint
self.regLower = config.regLower
logger.info('Endpoint: %s', self.endpoint)
logger.info('Enforce lowercase username during registration: %s', self.regLower)
@defer.inlineCallbacks @defer.inlineCallbacks
def check_password(self, user_id, password): def check_password(self, user_id, password):
@ -58,6 +62,11 @@ class RestAuthProvider(object):
if not (yield self.account_handler.check_user_exists(user_id)): if not (yield self.account_handler.check_user_exists(user_id)):
logger.info("User %s does not exist yet, creating...", user_id) logger.info("User %s does not exist yet, creating...", user_id)
localpart = user_id.split(":", 1)[0][1:] localpart = user_id.split(":", 1)[0][1:]
if localpart != localpart.lower() and self.regLower:
logger.info('User %s was not allowed to be created, enforcing lowercase policy', localpart)
defer.returnValue(False)
user_id = (yield self.account_handler.register(localpart=localpart)) user_id = (yield self.account_handler.register(localpart=localpart))
logger.info("Registration based on REST data was successful for %s", user_id) logger.info("Registration based on REST data was successful for %s", user_id)
else: else:
@ -69,7 +78,6 @@ class RestAuthProvider(object):
store = yield self.account_handler.hs.get_handlers().profile_handler.store store = yield self.account_handler.hs.get_handlers().profile_handler.store
if "display_name" in profile: if "display_name" in profile:
logger.info("mmmm2")
display_name = profile["display_name"] display_name = profile["display_name"]
logger.info("Setting display name to '%s' based on profile data", display_name) logger.info("Setting display name to '%s' based on profile data", display_name)
yield store.set_profile_displayname(localpart, display_name) yield store.set_profile_displayname(localpart, display_name)
@ -104,10 +112,18 @@ class RestAuthProvider(object):
_require_keys(config, ["endpoint"]) _require_keys(config, ["endpoint"])
class _RestConfig(object): class _RestConfig(object):
pass endpoint = ''
regLower = False
rest_config = _RestConfig() rest_config = _RestConfig()
rest_config.endpoint = config["endpoint"] rest_config.endpoint = config["endpoint"]
try:
rest_config.regLower = config['policy']['registration']['username']['enforceLowercase']
except TypeError:
# we don't care
pass
return rest_config return rest_config
def _require_keys(config, required): def _require_keys(config, required):