This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Implement username availability checker #2183
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
import hmac | ||
from hashlib import sha1 | ||
from synapse.util.async import run_on_reactor | ||
from synapse.util.ratelimitutils import FederationRateLimiter | ||
|
||
|
||
# We ought to be using hmac.compare_digest() but on older pythons it doesn't | ||
|
@@ -115,6 +116,40 @@ def on_POST(self, request): | |
defer.returnValue((200, ret)) | ||
|
||
|
||
class UsernameAvailabilityRestServlet(RestServlet): | ||
PATTERNS = client_v2_patterns("/register/available") | ||
|
||
def __init__(self, hs): | ||
""" | ||
Args: | ||
hs (synapse.server.HomeServer): server | ||
""" | ||
super(UsernameAvailabilityRestServlet, self).__init__() | ||
self.hs = hs | ||
self.registration_handler = hs.get_handlers().registration_handler | ||
self.ratelimiter = FederationRateLimiter( | ||
hs.get_clock(), | ||
window_size=2000, # Time window of 2s | ||
sleep_limit=1, # Artificially delay requests if rate > sleep_limit/window_size | ||
sleep_msec=1000, # Amount of artificial delay to apply | ||
reject_limit=1, # Error with 429 if more than reject_limit requests are queued | ||
concurrent_requests=1, # Allow 1 request at a time | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
) | ||
|
||
@defer.inlineCallbacks | ||
def on_GET(self, request): | ||
ip = self.hs.get_ip_from_request(request) | ||
with self.ratelimiter.ratelimit(ip) as wait_deferred: | ||
yield wait_deferred | ||
|
||
body = parse_json_object_from_request(request) | ||
assert_params_in_request(body, ['username']) | ||
|
||
yield self.registration_handler.check_username(body['username']) | ||
|
||
defer.returnValue((200, {"available": True})) | ||
|
||
|
||
class RegisterRestServlet(RestServlet): | ||
PATTERNS = client_v2_patterns("/register$") | ||
|
||
|
@@ -555,4 +590,5 @@ def _do_guest_registration(self, params): | |
def register_servlets(hs, http_server): | ||
EmailRegisterRequestTokenRestServlet(hs).register(http_server) | ||
MsisdnRegisterRequestTokenRestServlet(hs).register(http_server) | ||
UsernameAvailabilityRestServlet(hs).register(http_server) | ||
RegisterRestServlet(hs).register(http_server) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True
here surely?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Er, the docs say,
sleep_limit (int): The number of requests received...