|
| 1 | +# Copyright 2022 The Matrix.org Foundation C.I.C. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from twisted.test.proto_helpers import MemoryReactor |
| 16 | + |
| 17 | +from synapse.rest import admin |
| 18 | +from synapse.rest.client import login, login_token_request |
| 19 | +from synapse.server import HomeServer |
| 20 | +from synapse.util import Clock |
| 21 | + |
| 22 | +from tests import unittest |
| 23 | +from tests.unittest import override_config |
| 24 | + |
| 25 | + |
| 26 | +class LoginTokenRequestServletTestCase(unittest.HomeserverTestCase): |
| 27 | + |
| 28 | + servlets = [ |
| 29 | + login.register_servlets, |
| 30 | + admin.register_servlets, |
| 31 | + login_token_request.register_servlets, |
| 32 | + ] |
| 33 | + |
| 34 | + def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer: |
| 35 | + self.hs = self.setup_test_homeserver() |
| 36 | + self.hs.config.registration.enable_registration = True |
| 37 | + self.hs.config.registration.registrations_require_3pid = [] |
| 38 | + self.hs.config.registration.auto_join_rooms = [] |
| 39 | + self.hs.config.captcha.enable_registration_captcha = False |
| 40 | + |
| 41 | + return self.hs |
| 42 | + |
| 43 | + def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None: |
| 44 | + self.user = "user123" |
| 45 | + self.password = "password" |
| 46 | + |
| 47 | + def test_disabled(self) -> None: |
| 48 | + channel = self.make_request("POST", "/login/token", {}, access_token=None) |
| 49 | + self.assertEqual(channel.code, 400) |
| 50 | + |
| 51 | + self.register_user(self.user, self.password) |
| 52 | + token = self.login(self.user, self.password) |
| 53 | + |
| 54 | + channel = self.make_request("POST", "/login/token", {}, access_token=token) |
| 55 | + self.assertEqual(channel.code, 400) |
| 56 | + |
| 57 | + @override_config({"experimental_features": {"msc3882_enabled": True}}) |
| 58 | + def test_require_auth(self) -> None: |
| 59 | + channel = self.make_request("POST", "/login/token", {}, access_token=None) |
| 60 | + self.assertEqual(channel.code, 401) |
| 61 | + |
| 62 | + @override_config({"experimental_features": {"msc3882_enabled": True}}) |
| 63 | + def test_uia_on(self) -> None: |
| 64 | + user_id = self.register_user(self.user, self.password) |
| 65 | + token = self.login(self.user, self.password) |
| 66 | + |
| 67 | + channel = self.make_request("POST", "/login/token", {}, access_token=token) |
| 68 | + self.assertEqual(channel.code, 401) |
| 69 | + self.assertIn({"stages": ["m.login.password"]}, channel.json_body["flows"]) |
| 70 | + |
| 71 | + session = channel.json_body["session"] |
| 72 | + |
| 73 | + uia = { |
| 74 | + "auth": { |
| 75 | + "type": "m.login.password", |
| 76 | + "identifier": {"type": "m.id.user", "user": self.user}, |
| 77 | + "password": self.password, |
| 78 | + "session": session, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + channel = self.make_request("POST", "/login/token", uia, access_token=token) |
| 83 | + self.assertEqual(channel.code, 200) |
| 84 | + self.assertEqual(channel.json_body["expires_in"], 300) |
| 85 | + |
| 86 | + login_token = channel.json_body["login_token"] |
| 87 | + |
| 88 | + channel = self.make_request( |
| 89 | + "POST", |
| 90 | + "/login", |
| 91 | + content={"type": "m.login.token", "token": login_token}, |
| 92 | + ) |
| 93 | + self.assertEqual(channel.code, 200, channel.result) |
| 94 | + self.assertEqual(channel.json_body["user_id"], user_id) |
| 95 | + |
| 96 | + @override_config( |
| 97 | + {"experimental_features": {"msc3882_enabled": True, "msc3882_ui_auth": False}} |
| 98 | + ) |
| 99 | + def test_uia_off(self) -> None: |
| 100 | + user_id = self.register_user(self.user, self.password) |
| 101 | + token = self.login(self.user, self.password) |
| 102 | + |
| 103 | + channel = self.make_request("POST", "/login/token", {}, access_token=token) |
| 104 | + self.assertEqual(channel.code, 200) |
| 105 | + self.assertEqual(channel.json_body["expires_in"], 300) |
| 106 | + |
| 107 | + login_token = channel.json_body["login_token"] |
| 108 | + |
| 109 | + channel = self.make_request( |
| 110 | + "POST", |
| 111 | + "/login", |
| 112 | + content={"type": "m.login.token", "token": login_token}, |
| 113 | + ) |
| 114 | + self.assertEqual(channel.code, 200, channel.result) |
| 115 | + self.assertEqual(channel.json_body["user_id"], user_id) |
| 116 | + |
| 117 | + @override_config( |
| 118 | + { |
| 119 | + "experimental_features": { |
| 120 | + "msc3882_enabled": True, |
| 121 | + "msc3882_ui_auth": False, |
| 122 | + "msc3882_token_timeout": "15s", |
| 123 | + } |
| 124 | + } |
| 125 | + ) |
| 126 | + def test_expires_in(self) -> None: |
| 127 | + self.register_user(self.user, self.password) |
| 128 | + token = self.login(self.user, self.password) |
| 129 | + |
| 130 | + channel = self.make_request("POST", "/login/token", {}, access_token=token) |
| 131 | + self.assertEqual(channel.code, 200) |
| 132 | + self.assertEqual(channel.json_body["expires_in"], 15) |
0 commit comments