openid.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 twisted.internet import defer
  17. from synapse.api.errors import AuthError
  18. from synapse.http.servlet import RestServlet, parse_json_object_from_request
  19. from synapse.util.stringutils import random_string
  20. from ._base import client_patterns
  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_patterns("/user/(?P<user_id>[^/]*)/openid/request_token")
  45. EXPIRES_MS = 3600 * 1000
  46. def __init__(self, hs):
  47. super(IdTokenServlet, self).__init__()
  48. self.auth = hs.get_auth()
  49. self.store = hs.get_datastore()
  50. self.clock = hs.get_clock()
  51. self.server_name = hs.config.server_name
  52. @defer.inlineCallbacks
  53. def on_POST(self, request, user_id):
  54. requester = yield self.auth.get_user_by_req(request)
  55. if user_id != requester.user.to_string():
  56. raise AuthError(403, "Cannot request tokens for other users.")
  57. # Parse the request body to make sure it's JSON, but ignore the contents
  58. # for now.
  59. parse_json_object_from_request(request)
  60. token = random_string(24)
  61. ts_valid_until_ms = self.clock.time_msec() + self.EXPIRES_MS
  62. yield self.store.insert_open_id_token(token, ts_valid_until_ms, user_id)
  63. return (
  64. 200,
  65. {
  66. "access_token": token,
  67. "token_type": "Bearer",
  68. "matrix_server_name": self.server_name,
  69. "expires_in": self.EXPIRES_MS / 1000,
  70. },
  71. )
  72. def register_servlets(hs, http_server):
  73. IdTokenServlet(hs).register(http_server)