openid.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. from ._base import client_v2_patterns
  16. from synapse.http.servlet import RestServlet, parse_json_object_from_request
  17. from synapse.api.errors import AuthError
  18. from synapse.util.stringutils import random_string
  19. from twisted.internet import defer
  20. import logging
  21. logger = logging.getLogger(__name__)
  22. class IdTokenServlet(RestServlet):
  23. """
  24. Get a bearer token that may be passed to a third party to confirm ownership
  25. of a matrix user id.
  26. The format of the response could be made compatible with the format given
  27. in http://openid.net/specs/openid-connect-core-1_0.html#TokenResponse
  28. But instead of returning a signed "id_token" the response contains the
  29. name of the issuing matrix homeserver. This means that for now the third
  30. party will need to check the validity of the "id_token" against the
  31. federation /openid/userinfo endpoint of the homeserver.
  32. Request:
  33. POST /user/{user_id}/openid/request_token?access_token=... HTTP/1.1
  34. {}
  35. Response:
  36. HTTP/1.1 200 OK
  37. {
  38. "access_token": "ABDEFGH",
  39. "token_type": "Bearer",
  40. "matrix_server_name": "example.com",
  41. "expires_in": 3600,
  42. }
  43. """
  44. PATTERNS = client_v2_patterns(
  45. "/user/(?P<user_id>[^/]*)/openid/request_token"
  46. )
  47. EXPIRES_MS = 3600 * 1000
  48. def __init__(self, hs):
  49. super(IdTokenServlet, self).__init__()
  50. self.auth = hs.get_auth()
  51. self.store = hs.get_datastore()
  52. self.clock = hs.get_clock()
  53. self.server_name = hs.config.server_name
  54. @defer.inlineCallbacks
  55. def on_POST(self, request, user_id):
  56. requester = yield self.auth.get_user_by_req(request)
  57. if user_id != requester.user.to_string():
  58. raise AuthError(403, "Cannot request tokens for other users.")
  59. # Parse the request body to make sure it's JSON, but ignore the contents
  60. # for now.
  61. parse_json_object_from_request(request)
  62. token = random_string(24)
  63. ts_valid_until_ms = self.clock.time_msec() + self.EXPIRES_MS
  64. yield self.store.insert_open_id_token(token, ts_valid_until_ms, user_id)
  65. defer.returnValue((200, {
  66. "access_token": token,
  67. "token_type": "Bearer",
  68. "matrix_server_name": self.server_name,
  69. "expires_in": self.EXPIRES_MS / 1000,
  70. }))
  71. def register_servlets(hs, http_server):
  72. IdTokenServlet(hs).register(http_server)