|
| 1 | +""" |
| 2 | +oauthlib.openid.connect.core.endpoints.userinfo |
| 3 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 | +
|
| 5 | +This module is an implementation of userinfo endpoint. |
| 6 | +""" |
| 7 | +from __future__ import absolute_import, unicode_literals |
| 8 | + |
| 9 | +import json |
| 10 | +import logging |
| 11 | + |
| 12 | +from oauthlib.common import Request |
| 13 | +from oauthlib.common import unicode_type |
| 14 | +from oauthlib.oauth2.rfc6749.endpoints.base import BaseEndpoint |
| 15 | +from oauthlib.oauth2.rfc6749.endpoints.base import catch_errors_and_unavailability |
| 16 | +from oauthlib.oauth2.rfc6749.tokens import BearerToken |
| 17 | +from oauthlib.oauth2.rfc6749 import errors |
| 18 | + |
| 19 | + |
| 20 | +log = logging.getLogger(__name__) |
| 21 | + |
| 22 | + |
| 23 | +class UserInfoEndpoint(BaseEndpoint): |
| 24 | + """Authorizes access to userinfo resource. |
| 25 | + """ |
| 26 | + def __init__(self, request_validator): |
| 27 | + self.bearer = BearerToken(request_validator, None, None, None) |
| 28 | + self.request_validator = request_validator |
| 29 | + BaseEndpoint.__init__(self) |
| 30 | + |
| 31 | + @catch_errors_and_unavailability |
| 32 | + def create_userinfo_response(self, uri, http_method='GET', body=None, headers=None): |
| 33 | + """Validate BearerToken and return userinfo from RequestValidator |
| 34 | +
|
| 35 | + The UserInfo Endpoint MUST return a |
| 36 | + content-type header to indicate which format is being returned. The |
| 37 | + content-type of the HTTP response MUST be application/json if the |
| 38 | + response body is a text JSON object; the response body SHOULD be encoded |
| 39 | + using UTF-8. |
| 40 | + """ |
| 41 | + request = Request(uri, http_method, body, headers) |
| 42 | + request.scopes = ["openid"] |
| 43 | + self.validate_userinfo_request(request) |
| 44 | + |
| 45 | + claims = self.request_validator.get_userinfo_claims(request) |
| 46 | + if claims is None: |
| 47 | + log.error('Userinfo MUST have claims for %r.', request) |
| 48 | + raise errors.ServerError(status_code=500) |
| 49 | + |
| 50 | + if isinstance(claims, dict): |
| 51 | + resp_headers = { |
| 52 | + 'Content-Type': 'application/json' |
| 53 | + } |
| 54 | + if "sub" not in claims: |
| 55 | + log.error('Userinfo MUST have "sub" for %r.', request) |
| 56 | + raise errors.ServerError(status_code=500) |
| 57 | + body = json.dumps(claims) |
| 58 | + elif isinstance(claims, unicode_type): |
| 59 | + resp_headers = { |
| 60 | + 'Content-Type': 'application/jwt' |
| 61 | + } |
| 62 | + body = claims |
| 63 | + else: |
| 64 | + log.error('Userinfo return unknown response for %r.', request) |
| 65 | + raise errors.ServerError(status_code=500) |
| 66 | + log.debug('Userinfo access valid for %r.', request) |
| 67 | + return resp_headers, body, 200 |
| 68 | + |
| 69 | + def validate_userinfo_request(self, request): |
| 70 | + """Ensure the request is valid. |
| 71 | +
|
| 72 | + 5.3.1. UserInfo Request |
| 73 | + The Client sends the UserInfo Request using either HTTP GET or HTTP |
| 74 | + POST. The Access Token obtained from an OpenID Connect Authentication |
| 75 | + Request MUST be sent as a Bearer Token, per Section 2 of OAuth 2.0 |
| 76 | + Bearer Token Usage [RFC6750]. |
| 77 | +
|
| 78 | + It is RECOMMENDED that the request use the HTTP GET method and the |
| 79 | + Access Token be sent using the Authorization header field. |
| 80 | +
|
| 81 | + The following is a non-normative example of a UserInfo Request: |
| 82 | +
|
| 83 | + GET /userinfo HTTP/1.1 |
| 84 | + Host: server.example.com |
| 85 | + Authorization: Bearer SlAV32hkKG |
| 86 | +
|
| 87 | + 5.3.3. UserInfo Error Response |
| 88 | + When an error condition occurs, the UserInfo Endpoint returns an Error |
| 89 | + Response as defined in Section 3 of OAuth 2.0 Bearer Token Usage |
| 90 | + [RFC6750]. (HTTP errors unrelated to RFC 6750 are returned to the User |
| 91 | + Agent using the appropriate HTTP status code.) |
| 92 | +
|
| 93 | + The following is a non-normative example of a UserInfo Error Response: |
| 94 | +
|
| 95 | + HTTP/1.1 401 Unauthorized |
| 96 | + WWW-Authenticate: Bearer error="invalid_token", |
| 97 | + error_description="The Access Token expired" |
| 98 | + """ |
| 99 | + if not self.bearer.validate_request(request): |
| 100 | + raise errors.InvalidTokenError() |
| 101 | + if "openid" not in request.scopes: |
| 102 | + raise errors.InsufficientScopeError() |
0 commit comments