test_login_token_request.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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, versions
  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. GET_TOKEN_ENDPOINT = "/_matrix/client/v1/login/get_token"
  22. class LoginTokenRequestServletTestCase(unittest.HomeserverTestCase):
  23. servlets = [
  24. login.register_servlets,
  25. admin.register_servlets,
  26. login_token_request.register_servlets,
  27. versions.register_servlets, # TODO: remove once unstable revision 0 support is removed
  28. ]
  29. def make_homeserver(self, reactor: MemoryReactor, clock: Clock) -> HomeServer:
  30. self.hs = self.setup_test_homeserver()
  31. self.hs.config.registration.enable_registration = True
  32. self.hs.config.registration.registrations_require_3pid = []
  33. self.hs.config.registration.auto_join_rooms = []
  34. self.hs.config.captcha.enable_registration_captcha = False
  35. return self.hs
  36. def prepare(self, reactor: MemoryReactor, clock: Clock, hs: HomeServer) -> None:
  37. self.user = "user123"
  38. self.password = "password"
  39. def test_disabled(self) -> None:
  40. channel = self.make_request("POST", GET_TOKEN_ENDPOINT, {}, access_token=None)
  41. self.assertEqual(channel.code, 404)
  42. self.register_user(self.user, self.password)
  43. token = self.login(self.user, self.password)
  44. channel = self.make_request("POST", GET_TOKEN_ENDPOINT, {}, access_token=token)
  45. self.assertEqual(channel.code, 404)
  46. @override_config({"login_via_existing_session": {"enabled": True}})
  47. def test_require_auth(self) -> None:
  48. channel = self.make_request("POST", GET_TOKEN_ENDPOINT, {}, access_token=None)
  49. self.assertEqual(channel.code, 401)
  50. @override_config({"login_via_existing_session": {"enabled": True}})
  51. def test_uia_on(self) -> None:
  52. user_id = self.register_user(self.user, self.password)
  53. token = self.login(self.user, self.password)
  54. channel = self.make_request("POST", GET_TOKEN_ENDPOINT, {}, access_token=token)
  55. self.assertEqual(channel.code, 401)
  56. self.assertIn({"stages": ["m.login.password"]}, channel.json_body["flows"])
  57. session = channel.json_body["session"]
  58. uia = {
  59. "auth": {
  60. "type": "m.login.password",
  61. "identifier": {"type": "m.id.user", "user": self.user},
  62. "password": self.password,
  63. "session": session,
  64. },
  65. }
  66. channel = self.make_request("POST", GET_TOKEN_ENDPOINT, uia, access_token=token)
  67. self.assertEqual(channel.code, 200)
  68. self.assertEqual(channel.json_body["expires_in_ms"], 300000)
  69. login_token = channel.json_body["login_token"]
  70. channel = self.make_request(
  71. "POST",
  72. "/login",
  73. content={"type": "m.login.token", "token": login_token},
  74. )
  75. self.assertEqual(channel.code, 200, channel.result)
  76. self.assertEqual(channel.json_body["user_id"], user_id)
  77. @override_config(
  78. {"login_via_existing_session": {"enabled": True, "require_ui_auth": False}}
  79. )
  80. def test_uia_off(self) -> None:
  81. user_id = self.register_user(self.user, self.password)
  82. token = self.login(self.user, self.password)
  83. channel = self.make_request("POST", GET_TOKEN_ENDPOINT, {}, access_token=token)
  84. self.assertEqual(channel.code, 200)
  85. self.assertEqual(channel.json_body["expires_in_ms"], 300000)
  86. login_token = channel.json_body["login_token"]
  87. channel = self.make_request(
  88. "POST",
  89. "/login",
  90. content={"type": "m.login.token", "token": login_token},
  91. )
  92. self.assertEqual(channel.code, 200, channel.result)
  93. self.assertEqual(channel.json_body["user_id"], user_id)
  94. @override_config(
  95. {
  96. "login_via_existing_session": {
  97. "enabled": True,
  98. "require_ui_auth": False,
  99. "token_timeout": "15s",
  100. }
  101. }
  102. )
  103. def test_expires_in(self) -> None:
  104. self.register_user(self.user, self.password)
  105. token = self.login(self.user, self.password)
  106. channel = self.make_request("POST", GET_TOKEN_ENDPOINT, {}, access_token=token)
  107. self.assertEqual(channel.code, 200)
  108. self.assertEqual(channel.json_body["expires_in_ms"], 15000)
  109. @override_config(
  110. {
  111. "login_via_existing_session": {
  112. "enabled": True,
  113. "require_ui_auth": False,
  114. "token_timeout": "15s",
  115. }
  116. }
  117. )
  118. def test_unstable_support(self) -> None:
  119. # TODO: remove support for unstable MSC3882 is no longer needed
  120. # check feature is advertised in versions response:
  121. channel = self.make_request(
  122. "GET", "/_matrix/client/versions", {}, access_token=None
  123. )
  124. self.assertEqual(channel.code, 200)
  125. self.assertEqual(
  126. channel.json_body["unstable_features"]["org.matrix.msc3882"], True
  127. )
  128. self.register_user(self.user, self.password)
  129. token = self.login(self.user, self.password)
  130. # check feature is available via the unstable endpoint and returns an expires_in value in seconds
  131. channel = self.make_request(
  132. "POST",
  133. "/_matrix/client/unstable/org.matrix.msc3882/login/token",
  134. {},
  135. access_token=token,
  136. )
  137. self.assertEqual(channel.code, 200)
  138. self.assertEqual(channel.json_body["expires_in"], 15)