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

Commit

Permalink
Stop generating refresh tokens
Browse files Browse the repository at this point in the history
Since we're not doing refresh tokens any more, we should start killing off the
dead code paths. /tokenrefresh itself is a bit of a thornier subject, since
there might be apps out there using it, but we can at least not generate
refresh tokens on new logins.
  • Loading branch information
richvdh committed Nov 28, 2016
1 parent b614653 commit 5c4edc8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 45 deletions.
20 changes: 4 additions & 16 deletions synapse/handlers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,10 @@ def validate_password_login(self, user_id, password):
return self._check_password(user_id, password)

@defer.inlineCallbacks
def get_login_tuple_for_user_id(self, user_id, device_id=None,
initial_display_name=None):
def get_access_token_for_user_id(self, user_id, device_id=None,
initial_display_name=None):
"""
Gets login tuple for the user with the given user ID.
Creates a new access/refresh token for the user.
Creates a new access token for the user with the given user ID.
The user is assumed to have been authenticated by some other
machanism (e.g. CAS), and the user_id converted to the canonical case.
Expand All @@ -400,16 +398,13 @@ def get_login_tuple_for_user_id(self, user_id, device_id=None,
initial_display_name (str): display name to associate with the
device if it needs re-registering
Returns:
A tuple of:
The access token for the user's session.
The refresh token for the user's session.
Raises:
StoreError if there was a problem storing the token.
LoginError if there was an authentication problem.
"""
logger.info("Logging in user %s on device %s", user_id, device_id)
access_token = yield self.issue_access_token(user_id, device_id)
refresh_token = yield self.issue_refresh_token(user_id, device_id)

# the device *should* have been registered before we got here; however,
# it's possible we raced against a DELETE operation. The thing we
Expand All @@ -420,7 +415,7 @@ def get_login_tuple_for_user_id(self, user_id, device_id=None,
user_id, device_id, initial_display_name
)

defer.returnValue((access_token, refresh_token))
defer.returnValue(access_token)

@defer.inlineCallbacks
def check_user_exists(self, user_id):
Expand Down Expand Up @@ -531,13 +526,6 @@ def issue_access_token(self, user_id, device_id=None):
device_id)
defer.returnValue(access_token)

@defer.inlineCallbacks
def issue_refresh_token(self, user_id, device_id=None):
refresh_token = self.generate_refresh_token(user_id)
yield self.store.add_refresh_token_to_user(user_id, refresh_token,
device_id)
defer.returnValue(refresh_token)

def generate_access_token(self, user_id, extra_caveats=None,
duration_in_ms=(60 * 60 * 1000)):
extra_caveats = extra_caveats or []
Expand Down
28 changes: 10 additions & 18 deletions synapse/rest/client/v1/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,13 @@ def do_password_login(self, login_submission):
password=login_submission["password"],
)
device_id = yield self._register_device(user_id, login_submission)
access_token, refresh_token = (
yield auth_handler.get_login_tuple_for_user_id(
user_id, device_id,
login_submission.get("initial_device_display_name")
)
access_token = yield auth_handler.get_access_token_for_user_id(
user_id, device_id,
login_submission.get("initial_device_display_name"),
)
result = {
"user_id": user_id, # may have changed
"access_token": access_token,
"refresh_token": refresh_token,
"home_server": self.hs.hostname,
"device_id": device_id,
}
Expand All @@ -161,16 +158,13 @@ def do_token_login(self, login_submission):
yield auth_handler.validate_short_term_login_token_and_get_user_id(token)
)
device_id = yield self._register_device(user_id, login_submission)
access_token, refresh_token = (
yield auth_handler.get_login_tuple_for_user_id(
user_id, device_id,
login_submission.get("initial_device_display_name")
)
access_token = yield auth_handler.get_access_token_for_user_id(
user_id, device_id,
login_submission.get("initial_device_display_name"),
)
result = {
"user_id": user_id, # may have changed
"access_token": access_token,
"refresh_token": refresh_token,
"home_server": self.hs.hostname,
"device_id": device_id,
}
Expand Down Expand Up @@ -207,16 +201,14 @@ def do_jwt_login(self, login_submission):
device_id = yield self._register_device(
registered_user_id, login_submission
)
access_token, refresh_token = (
yield auth_handler.get_login_tuple_for_user_id(
registered_user_id, device_id,
login_submission.get("initial_device_display_name")
)
access_token = yield auth_handler.get_access_token_for_user_id(
registered_user_id, device_id,
login_submission.get("initial_device_display_name"),
)

result = {
"user_id": registered_user_id,
"access_token": access_token,
"refresh_token": refresh_token,
"home_server": self.hs.hostname,
}
else:
Expand Down
5 changes: 2 additions & 3 deletions synapse/rest/client/v2_alpha/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,8 @@ def _create_registration_details(self, user_id, params):
"""
device_id = yield self._register_device(user_id, params)

access_token, refresh_token = (
yield self.auth_handler.get_login_tuple_for_user_id(
access_token = (
yield self.auth_handler.get_access_token_for_user_id(
user_id, device_id=device_id,
initial_display_name=params.get("initial_device_display_name")
)
Expand All @@ -396,7 +396,6 @@ def _create_registration_details(self, user_id, params):
"user_id": user_id,
"access_token": access_token,
"home_server": self.hs.hostname,
"refresh_token": refresh_token,
"device_id": device_id,
})

Expand Down
12 changes: 4 additions & 8 deletions tests/rest/client/v2_alpha/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,18 @@ def test_POST_appservice_registration_valid(self):
self.registration_handler.appservice_register = Mock(
return_value=user_id
)
self.auth_handler.get_login_tuple_for_user_id = Mock(
return_value=(token, "kermits_refresh_token")
self.auth_handler.get_access_token_for_user_id = Mock(
return_value=token
)

(code, result) = yield self.servlet.on_POST(self.request)
self.assertEquals(code, 200)
det_data = {
"user_id": user_id,
"access_token": token,
"refresh_token": "kermits_refresh_token",
"home_server": self.hs.hostname
}
self.assertDictContainsSubset(det_data, result)
self.assertIn("refresh_token", result)

@defer.inlineCallbacks
def test_POST_appservice_registration_invalid(self):
Expand Down Expand Up @@ -126,8 +124,8 @@ def test_POST_user_valid(self):
"password": "monkey"
}, None)
self.registration_handler.register = Mock(return_value=(user_id, None))
self.auth_handler.get_login_tuple_for_user_id = Mock(
return_value=(token, "kermits_refresh_token")
self.auth_handler.get_access_token_for_user_id = Mock(
return_value=token
)
self.device_handler.check_device_registered = \
Mock(return_value=device_id)
Expand All @@ -137,12 +135,10 @@ def test_POST_user_valid(self):
det_data = {
"user_id": user_id,
"access_token": token,
"refresh_token": "kermits_refresh_token",
"home_server": self.hs.hostname,
"device_id": device_id,
}
self.assertDictContainsSubset(det_data, result)
self.assertIn("refresh_token", result)
self.auth_handler.get_login_tuple_for_user_id(
user_id, device_id=device_id, initial_device_display_name=None)

Expand Down

0 comments on commit 5c4edc8

Please sign in to comment.