test_login_token_request.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. from twisted.test.proto_helpers import MemoryReactor
  15. from synapse.rest import admin
  16. from synapse.rest.client import login, login_token_request
  17. from synapse.server import HomeServer
  18. from synapse.util import Clock
  19. from tests import unittest
  20. from tests.unittest import override_config
  21. endpoint = "/_matrix/client/unstable/org.matrix.msc3882/login/token"
  22. class LoginTokenRequestServletTestCase(unittest.HomeserverTestCase):
  23. servlets = [
  24. login.register_servlets,
  25. admin.register_servlets,
  26. login_token_request.register_servlets,
  27. ]
  28. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  29. self.hs = self.setup_test_homeserver()
  30. self.hs.config.registration.enable_registration = True
  31. self.hs.config.registration.registrations_require_3pid = []
  32. self.hs.config.registration.auto_join_rooms = []
  33. self.hs.config.captcha.enable_registration_captcha = False
  34. return self.hs
  35. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  36. self.user = "user123"
  37. self.password = "password"
  38. def test_disabled(self) -> None:
  39. channel = self.make_request("POST", endpoint, {}, access_token=None)
  40. self.assertEqual(channel.code, 404)
  41. self.register_user(self.user, self.password)
  42. token = self.login(self.user, self.password)
  43. channel = self.make_request("POST", endpoint, {}, access_token=token)
  44. self.assertEqual(channel.code, 404)
  45. @override_config({"experimental_features": {"msc3882_enabled": True}})
  46. def test_require_auth(self) -> None:
  47. channel = self.make_request("POST", endpoint, {}, access_token=None)
  48. self.assertEqual(channel.code, 401)
  49. @override_config({"experimental_features": {"msc3882_enabled": True}})
  50. def test_uia_on(self) -> None:
  51. user_id = self.register_user(self.user, self.password)
  52. token = self.login(self.user, self.password)
  53. channel = self.make_request("POST", endpoint, {}, access_token=token)
  54. self.assertEqual(channel.code, 401)
  55. self.assertIn({"stages": ["m.login.password"]}, channel.json_body["flows"])
  56. session = channel.json_body["session"]
  57. uia = {
  58. "auth": {
  59. "type": "m.login.password",
  60. "identifier": {"type": "m.id.user", "user": self.user},
  61. "password": self.password,
  62. "session": session,
  63. },
  64. }
  65. channel = self.make_request("POST", endpoint, uia, access_token=token)
  66. self.assertEqual(channel.code, 200)
  67. self.assertEqual(channel.json_body["expires_in"], 300)
  68. login_token = channel.json_body["login_token"]
  69. channel = self.make_request(
  70. "POST",
  71. "/login",
  72. content={"type": "m.login.token", "token": login_token},
  73. )
  74. self.assertEqual(channel.code, 200, channel.result)
  75. self.assertEqual(channel.json_body["user_id"], user_id)
  76. @override_config(
  77. {"experimental_features": {"msc3882_enabled": True, "msc3882_ui_auth": False}}
  78. )
  79. def test_uia_off(self) -> None:
  80. user_id = self.register_user(self.user, self.password)
  81. token = self.login(self.user, self.password)
  82. channel = self.make_request("POST", endpoint, {}, access_token=token)
  83. self.assertEqual(channel.code, 200)
  84. self.assertEqual(channel.json_body["expires_in"], 300)
  85. login_token = channel.json_body["login_token"]
  86. channel = self.make_request(
  87. "POST",
  88. "/login",
  89. content={"type": "m.login.token", "token": login_token},
  90. )
  91. self.assertEqual(channel.code, 200, channel.result)
  92. self.assertEqual(channel.json_body["user_id"], user_id)
  93. @override_config(
  94. {
  95. "experimental_features": {
  96. "msc3882_enabled": True,
  97. "msc3882_ui_auth": False,
  98. "msc3882_token_timeout": "15s",
  99. }
  100. }
  101. )
  102. def test_expires_in(self) -> None:
  103. self.register_user(self.user, self.password)
  104. token = self.login(self.user, self.password)
  105. channel = self.make_request("POST", endpoint, {}, access_token=token)
  106. self.assertEqual(channel.code, 200)
  107. self.assertEqual(channel.json_body["expires_in"], 15)