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

Update MSC2918 refresh token support to confirm with the latest revision: accept the refresh_tokens parameter in the request body rather than in the URL parameters. #11430

Merged
merged 8 commits into from
Nov 26, 2021
1 change: 1 addition & 0 deletions changelog.d/11430.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update [MSC2918 refresh token](https://github.com/matrix-org/matrix-doc/blob/main/proposals/2918-refreshtokens.md#msc2918-refresh-tokens) support to confirm with the latest revision: accept the `refresh_tokens` parameter in the request body rather than in the URL parameters.
12 changes: 7 additions & 5 deletions synapse/rest/client/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
from synapse.http.servlet import (
RestServlet,
assert_params_in_dict,
parse_boolean,
parse_bytes_from_args,
parse_json_object_from_request,
parse_string,
Expand Down Expand Up @@ -165,11 +164,14 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, LoginResponse]:
login_submission = parse_json_object_from_request(request)

if self._msc2918_enabled:
# Check if this login should also issue a refresh token, as per
# MSC2918
should_issue_refresh_token = parse_boolean(
request, name=LoginRestServlet.REFRESH_TOKEN_PARAM, default=False
# Check if this login should also issue a refresh token, as per MSC2918
should_issue_refresh_token = login_submission.get(
"org.matrix.msc2918.refresh_token", False
)
if not isinstance(should_issue_refresh_token, bool):
raise SynapseError(
400, "`org.matrix.msc2918.refresh_token` should be true or false."
)
else:
should_issue_refresh_token = False

Expand Down
9 changes: 6 additions & 3 deletions synapse/rest/client/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
from synapse.http.servlet import (
RestServlet,
assert_params_in_dict,
parse_boolean,
parse_json_object_from_request,
parse_string,
)
Expand Down Expand Up @@ -449,9 +448,13 @@ async def on_POST(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
if self._msc2918_enabled:
# Check if this registration should also issue a refresh token, as
# per MSC2918
should_issue_refresh_token = parse_boolean(
request, name="org.matrix.msc2918.refresh_token", default=False
should_issue_refresh_token = body.get(
"org.matrix.msc2918.refresh_token", False
)
if not isinstance(should_issue_refresh_token, bool):
raise SynapseError(
400, "`org.matrix.msc2918.refresh_token` should be true or false."
)
else:
should_issue_refresh_token = False

Expand Down
58 changes: 44 additions & 14 deletions tests/rest/client/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,11 @@ def test_login_issue_refresh_token(self):
A login response should include a refresh_token only if asked.
"""
# Test login
body = {"type": "m.login.password", "user": "test", "password": self.user_pass}
body = {
"type": "m.login.password",
"user": "test",
"password": self.user_pass,
}

login_without_refresh = self.make_request(
"POST", "/_matrix/client/r0/login", body
Expand All @@ -539,8 +543,8 @@ def test_login_issue_refresh_token(self):

login_with_refresh = self.make_request(
"POST",
"/_matrix/client/r0/login?org.matrix.msc2918.refresh_token=true",
body,
"/_matrix/client/r0/login",
{"org.matrix.msc2918.refresh_token": True, **body},
)
self.assertEqual(login_with_refresh.code, 200, login_with_refresh.result)
self.assertIn("refresh_token", login_with_refresh.json_body)
Expand All @@ -566,11 +570,12 @@ def test_register_issue_refresh_token(self):

register_with_refresh = self.make_request(
"POST",
"/_matrix/client/r0/register?org.matrix.msc2918.refresh_token=true",
"/_matrix/client/r0/register",
{
"username": "test3",
"password": self.user_pass,
"auth": {"type": LoginType.DUMMY},
"org.matrix.msc2918.refresh_token": True,
},
)
self.assertEqual(register_with_refresh.code, 200, register_with_refresh.result)
Expand All @@ -581,10 +586,15 @@ def test_token_refresh(self):
"""
A refresh token can be used to issue a new access token.
"""
body = {"type": "m.login.password", "user": "test", "password": self.user_pass}
body = {
"type": "m.login.password",
"user": "test",
"password": self.user_pass,
"org.matrix.msc2918.refresh_token": True,
}
login_response = self.make_request(
"POST",
"/_matrix/client/r0/login?org.matrix.msc2918.refresh_token=true",
"/_matrix/client/r0/login",
body,
)
self.assertEqual(login_response.code, 200, login_response.result)
Expand Down Expand Up @@ -614,10 +624,15 @@ def test_refreshable_access_token_expiration(self):
"""
The access token should have some time as specified in the config.
"""
body = {"type": "m.login.password", "user": "test", "password": self.user_pass}
body = {
"type": "m.login.password",
"user": "test",
"password": self.user_pass,
"org.matrix.msc2918.refresh_token": True,
}
login_response = self.make_request(
"POST",
"/_matrix/client/r0/login?org.matrix.msc2918.refresh_token=true",
"/_matrix/client/r0/login",
body,
)
self.assertEqual(login_response.code, 200, login_response.result)
Expand Down Expand Up @@ -666,10 +681,15 @@ def test_refresh_token_expiry(self):
refresh the session.
"""

body = {"type": "m.login.password", "user": "test", "password": self.user_pass}
body = {
"type": "m.login.password",
"user": "test",
"password": self.user_pass,
"org.matrix.msc2918.refresh_token": True,
}
login_response = self.make_request(
"POST",
"/_matrix/client/r0/login?org.matrix.msc2918.refresh_token=true",
"/_matrix/client/r0/login",
body,
)
self.assertEqual(login_response.code, HTTPStatus.OK, login_response.result)
Expand Down Expand Up @@ -711,10 +731,15 @@ def test_ultimate_session_expiry(self):
The session can be configured to have an ultimate, limited lifetime.
"""

body = {"type": "m.login.password", "user": "test", "password": self.user_pass}
body = {
"type": "m.login.password",
"user": "test",
"password": self.user_pass,
"org.matrix.msc2918.refresh_token": True,
}
login_response = self.make_request(
"POST",
"/_matrix/client/r0/login?org.matrix.msc2918.refresh_token=true",
"/_matrix/client/r0/login",
body,
)
self.assertEqual(login_response.code, 200, login_response.result)
Expand Down Expand Up @@ -763,10 +788,15 @@ def test_refresh_token_invalidation(self):
|-> fourth_refresh (fails)
"""

body = {"type": "m.login.password", "user": "test", "password": self.user_pass}
body = {
"type": "m.login.password",
"user": "test",
"password": self.user_pass,
"org.matrix.msc2918.refresh_token": True,
}
login_response = self.make_request(
"POST",
"/_matrix/client/r0/login?org.matrix.msc2918.refresh_token=true",
"/_matrix/client/r0/login",
body,
)
self.assertEqual(login_response.code, 200, login_response.result)
Expand Down