openid.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import logging
  16. from synapse.api.errors import AuthError
  17. from synapse.http.servlet import RestServlet, parse_json_object_from_request
  18. from synapse.util.stringutils import random_string
  19. from ._base import client_patterns
  20. logger = logging.getLogger(__name__)
  21. class IdTokenServlet(RestServlet):
  22. """
  23. Get a bearer token that may be passed to a third party to confirm ownership
  24. of a matrix user id.
  25. The format of the response could be made compatible with the format given
  26. in http://openid.net/specs/openid-connect-core-1_0.html#TokenResponse
  27. But instead of returning a signed "id_token" the response contains the
  28. name of the issuing matrix homeserver. This means that for now the third
  29. party will need to check the validity of the "id_token" against the
  30. federation /openid/userinfo endpoint of the homeserver.
  31. Request:
  32. POST /user/{user_id}/openid/request_token?access_token=... HTTP/1.1
  33. {}
  34. Response:
  35. HTTP/1.1 200 OK
  36. {
  37. "access_token": "ABDEFGH",
  38. "token_type": "Bearer",
  39. "matrix_server_name": "example.com",
  40. "expires_in": 3600,
  41. }
  42. """
  43. PATTERNS = client_patterns("/user/(?P<user_id>[^/]*)/openid/request_token")
  44. EXPIRES_MS = 3600 * 1000
  45. def __init__(self, hs):
  46. super().__init__()
  47. self.auth = hs.get_auth()
  48. self.store = hs.get_datastore()
  49. self.clock = hs.get_clock()
  50. self.server_name = hs.config.server_name
  51. async def on_POST(self, request, user_id):
  52. requester = await self.auth.get_user_by_req(request)
  53. if user_id != requester.user.to_string():
  54. raise AuthError(403, "Cannot request tokens for other users.")
  55. # Parse the request body to make sure it's JSON, but ignore the contents
  56. # for now.
  57. parse_json_object_from_request(request)
  58. token = random_string(24)
  59. ts_valid_until_ms = self.clock.time_msec() + self.EXPIRES_MS
  60. await self.store.insert_open_id_token(token, ts_valid_until_ms, user_id)
  61. return (
  62. 200,
  63. {
  64. "access_token": token,
  65. "token_type": "Bearer",
  66. "matrix_server_name": self.server_name,
  67. "expires_in": self.EXPIRES_MS / 1000,
  68. },
  69. )
  70. def register_servlets(hs, http_server):
  71. IdTokenServlet(hs).register(http_server)