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

Add type annotations to some of the configuration surrounding refresh tokens. #11428

Merged
merged 5 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/11428.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add type annotations to some of the configuration surrounding refresh tokens.
7 changes: 5 additions & 2 deletions synapse/config/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Optional

from synapse.api.constants import RoomCreationPreset
from synapse.config._base import Config, ConfigError
Expand Down Expand Up @@ -123,12 +124,14 @@ def read_config(self, config, **kwargs):
refreshable_access_token_lifetime = self.parse_duration(
refreshable_access_token_lifetime
)
self.refreshable_access_token_lifetime = refreshable_access_token_lifetime
self.refreshable_access_token_lifetime: Optional[
int
] = refreshable_access_token_lifetime

refresh_token_lifetime = config.get("refresh_token_lifetime")
if refresh_token_lifetime is not None:
refresh_token_lifetime = self.parse_duration(refresh_token_lifetime)
self.refresh_token_lifetime = refresh_token_lifetime
self.refresh_token_lifetime: Optional[int] = refresh_token_lifetime

# The fallback template used for authenticating using a registration token
self.registration_token_template = self.read_template("registration_token.html")
Expand Down
5 changes: 4 additions & 1 deletion synapse/handlers/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,10 @@ class and RegisterDeviceReplicationServlet.
assert access_token_expiry is None
access_token = self.macaroon_gen.generate_guest_access_token(user_id)
else:
if should_issue_refresh_token:
if (
should_issue_refresh_token
and self.refreshable_access_token_lifetime is not None
):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bugfix?

Even if so, it's in between unreleased versions so probably doesn't need calling out in the changelog.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I wonder if should_issue_refresh_token and self.refreshable_access_token_lifetime is None should raise an error?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just because Mypy can't realise that should_issue_refresh_token is only true if there is a lifetime configured (it's set several levels up).

Maybe assert self.refreshable_access_token_lifetime is not None makes more sense, though I'm not entirely sure.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Putting it in the if condition makes me think that the condition must be considered at runtime. My preference would be an assertion with a comment like

Mypy can't realise that should_issue_refresh_token is only true if there is a lifetime configured (it's set several levels up).

now_ms = self.clock.time_msec()

# Set the expiry time of the refreshable access token
Expand Down