Example #1
0
    def setUp(self):
        def set_user(request):
            request.user = mock.MagicMock()
            request.client = mock.MagicMock()
            request.client.client_id = 'mocked_client_id'
            return True

        self.mock_validator = mock.MagicMock()
        self.mock_validator.authenticate_client.side_effect = set_user
        self.addCleanup(setattr, self, 'mock_validator', mock.MagicMock())
        auth_code = AuthorizationCodeGrant(
            request_validator=self.mock_validator)
        password = ResourceOwnerPasswordCredentialsGrant(
            request_validator=self.mock_validator)
        client = ClientCredentialsGrant(request_validator=self.mock_validator)
        supported_types = {
            'authorization_code': auth_code,
            'password': password,
            'client_credentials': client,
        }
        self.expires_in = 1800
        token = tokens.BearerToken(self.mock_validator,
                                   expires_in=self.expires_in)
        self.endpoint = TokenEndpoint('authorization_code',
                                      default_token_type=token,
                                      grant_types=supported_types)
 def __init__(self, request_validator, token_expires_in=None,
              token_generator=None, refresh_token_generator=None,
              *args, **kwargs):
     auth_grant = OrgAuthorizationCodeGrant(request_validator)
     implicit_grant = OrgImplicitGrant(request_validator)
     password_grant = OrgResourceOwnerPasswordCredentialsGrant(
         request_validator)
     credentials_grant = ClientCredentialsGrant(request_validator)
     refresh_grant = RefreshTokenGrant(request_validator)
     bearer = BearerToken(request_validator, token_generator,
                          token_expires_in, refresh_token_generator)
     AuthorizationEndpoint.__init__(self, default_response_type='code',
                                    response_types={
                                        'code': auth_grant,
                                        'token': implicit_grant,
                                    },
                                    default_token_type=bearer)
     TokenEndpoint.__init__(self, default_grant_type='authorization_code',
                            grant_types={
                                'authorization_code': auth_grant,
                                'password': password_grant,
                                'client_credentials': credentials_grant,
                                'refresh_token': refresh_grant,
                            },
                            default_token_type=bearer)
     ResourceEndpoint.__init__(self, default_token='Bearer',
                               token_types={'Bearer': bearer})
     RevocationEndpoint.__init__(self, request_validator)
 def __init__(self,
              request_validator,
              token_expires_in=None,
              token_generator=None,
              refresh_token_generator=None,
              *args,
              **kwargs):
     auth_grant = OrgAuthorizationCodeGrant(request_validator)
     implicit_grant = OrgImplicitGrant(request_validator)
     password_grant = OrgResourceOwnerPasswordCredentialsGrant(
         request_validator)
     credentials_grant = ClientCredentialsGrant(request_validator)
     refresh_grant = RefreshTokenGrant(request_validator)
     bearer = BearerToken(request_validator, token_generator,
                          token_expires_in, refresh_token_generator)
     AuthorizationEndpoint.__init__(self,
                                    default_response_type='code',
                                    response_types={
                                        'code': auth_grant,
                                        'token': implicit_grant,
                                    },
                                    default_token_type=bearer)
     TokenEndpoint.__init__(self,
                            default_grant_type='authorization_code',
                            grant_types={
                                'authorization_code': auth_grant,
                                'password': password_grant,
                                'client_credentials': credentials_grant,
                                'refresh_token': refresh_grant,
                            },
                            default_token_type=bearer)
     ResourceEndpoint.__init__(self,
                               default_token='Bearer',
                               token_types={'Bearer': bearer})
     RevocationEndpoint.__init__(self, request_validator)
 def __init__(
     self,
     request_validator,
     token_generator=None,
     token_expires_in=None,
     refresh_token_generator=None,
     **kwargs
 ):
     """Construct a client credentials grant server.
     :param request_validator: An implementation of
                               oauthlib.oauth2.RequestValidator.
     :param token_expires_in: An int or a function to generate a token
                              expiration offset (in seconds) given a
                              oauthlib.common.Request object.
     :param token_generator: A function to generate a token from a request.
     :param refresh_token_generator: A function to generate a token from a
                                     request for the refresh token.
     :param kwargs: Extra parameters to pass to authorization-,
                    token-, resource-, and revocation-endpoint constructors.
     """
     self._params = {}
     self.request_validator = request_validator
     refresh_grant = SocialTokenGrant(request_validator)
     bearer = BearerToken(
         request_validator,
         token_generator,
         token_expires_in,
         refresh_token_generator,
     )
     TokenEndpoint.__init__(
         self,
         default_grant_type='convert_token',
         grant_types={'convert_token': refresh_grant},
         default_token_type=bearer,
     )
Example #5
0
    def setUp(self):
        def set_user(request):
            request.user = mock.MagicMock()
            request.client = mock.MagicMock()
            request.client.client_id = 'mocked_client_id'
            return True

        self.mock_validator = mock.MagicMock()
        self.mock_validator.authenticate_client.side_effect = set_user
        self.mock_validator.get_code_challenge.return_value = None
        self.addCleanup(setattr, self, 'mock_validator', mock.MagicMock())
        auth_code = AuthorizationCodeGrant(
            request_validator=self.mock_validator)
        supported_types = {
            'authorization_code': auth_code,
        }
        self.expires_in = 1800
        token = BearerToken(
            self.mock_validator,
            expires_in=self.expires_in
        )
        self.endpoint = TokenEndpoint(
            'authorization_code',
            default_token_type=token,
            grant_types=supported_types
        )
Example #6
0
class TokenEndpointTest(TestCase):
    def setUp(self):
        def set_user(request):
            request.user = mock.MagicMock()
            request.client = mock.MagicMock()
            request.client.client_id = 'mocked_client_id'
            return True

        self.mock_validator = mock.MagicMock()
        self.mock_validator.authenticate_client.side_effect = set_user
        self.mock_validator.get_code_challenge.return_value = None
        self.addCleanup(setattr, self, 'mock_validator', mock.MagicMock())
        auth_code = AuthorizationCodeGrant(
            request_validator=self.mock_validator)
        supported_types = {
            'authorization_code': auth_code,
        }
        self.expires_in = 1800
        token = BearerToken(self.mock_validator, expires_in=self.expires_in)
        self.endpoint = TokenEndpoint('authorization_code',
                                      default_token_type=token,
                                      grant_types=supported_types)

    @mock.patch('oauthlib.common.generate_token', new=lambda: 'abc')
    def test_authorization_grant(self):
        body = 'grant_type=authorization_code&code=abc&scope=all+of+them&state=xyz'
        headers, body, status_code = self.endpoint.create_token_response(
            '', body=body)
        token = {
            'token_type': 'Bearer',
            'expires_in': self.expires_in,
            'access_token': 'abc',
            'refresh_token': 'abc',
            'scope': 'all of them',
            'state': 'xyz'
        }
        self.assertEqual(json.loads(body), token)

        body = 'grant_type=authorization_code&code=abc&state=xyz'
        headers, body, status_code = self.endpoint.create_token_response(
            '', body=body)
        token = {
            'token_type': 'Bearer',
            'expires_in': self.expires_in,
            'access_token': 'abc',
            'refresh_token': 'abc',
            'state': 'xyz'
        }
        self.assertEqual(json.loads(body), token)

    def test_missing_type(self):
        _, body, _ = self.endpoint.create_token_response('', body='')
        token = {'error': 'unsupported_grant_type'}
        self.assertEqual(json.loads(body), token)

    def test_invalid_type(self):
        body = 'grant_type=invalid'
        _, body, _ = self.endpoint.create_token_response('', body=body)
        token = {'error': 'unsupported_grant_type'}
        self.assertEqual(json.loads(body), token)
	def __init__(self, request_validator, token_generator=None,
				 token_expires_in=None, refresh_token_generator=None, **kwargs):
		"""Construct a new web application server.

		:param request_validator: An implementation of
								  oauthlib.oauth2.RequestValidator.
		:param token_expires_in: An int or a function to generate a token
								 expiration offset (in seconds) given a
								 oauthlib.common.Request object.
		:param token_generator: A function to generate a token from a request.
		:param refresh_token_generator: A function to generate a token from a
										request for the refresh token.
		:param kwargs: Extra parameters to pass to authorization-,
					   token-, resource-, and revocation-endpoint constructors.
		"""
		auth_grant = AuthorizationCodeGrant(request_validator)
		refresh_grant = RefreshTokenGrant(request_validator)
		bearer = BearerToken(request_validator, token_generator,
							 token_expires_in, refresh_token_generator)
		AuthorizationEndpoint.__init__(self, default_response_type='code',
									   response_types={'code': auth_grant},
									   default_token_type=bearer)
		TokenEndpoint.__init__(self, default_grant_type='authorization_code',
							   grant_types={
								   'authorization_code': auth_grant,
								   'refresh_token': refresh_grant,
							   },
							   default_token_type=bearer)
		ResourceEndpoint.__init__(self, default_token='Bearer',
								  token_types={'Bearer': bearer})
		RevocationEndpoint.__init__(self, request_validator)
Example #8
0
	def __init__(self, request_validator, token_generator=None,
				 token_expires_in=None, refresh_token_generator=None, **kwargs):
		"""Construct a new web application server.

		:param request_validator: An implementation of
								  oauthlib.oauth2.RequestValidator.
		:param token_expires_in: An int or a function to generate a token
								 expiration offset (in seconds) given a
								 oauthlib.common.Request object.
		:param token_generator: A function to generate a token from a request.
		:param refresh_token_generator: A function to generate a token from a
										request for the refresh token.
		:param kwargs: Extra parameters to pass to authorization-,
					   token-, resource-, and revocation-endpoint constructors.
		"""
		auth_grant = AuthorizationCodeGrant(request_validator)
		refresh_grant = RefreshTokenGrant(request_validator)
		bearer = BearerToken(request_validator, token_generator,
							 token_expires_in, refresh_token_generator)
		AuthorizationEndpoint.__init__(self, default_response_type='code',
									   response_types={'code': auth_grant},
									   default_token_type=bearer)
		TokenEndpoint.__init__(self, default_grant_type='authorization_code',
							   grant_types={
								   'authorization_code': auth_grant,
								   'refresh_token': refresh_grant,
							   },
							   default_token_type=bearer)
		ResourceEndpoint.__init__(self, default_token='Bearer',
								  token_types={'Bearer': bearer})
		RevocationEndpoint.__init__(self, request_validator)
Example #9
0
    def setUp(self):
        def set_user(request):
            request.user = mock.MagicMock()
            request.client = mock.MagicMock()
            request.client.client_id = 'mocked_client_id'
            return True

        self.mock_validator = mock.MagicMock()
        self.mock_validator.authenticate_client.side_effect = set_user
        self.mock_validator.get_code_challenge.return_value = None
        self.addCleanup(setattr, self, 'mock_validator', mock.MagicMock())
        auth_code = AuthorizationCodeGrant(
            request_validator=self.mock_validator)
        password = ResourceOwnerPasswordCredentialsGrant(
            request_validator=self.mock_validator)
        client = ClientCredentialsGrant(
            request_validator=self.mock_validator)
        supported_types = {
            'authorization_code': auth_code,
            'password': password,
            'client_credentials': client,
        }
        self.expires_in = 1800
        token = tokens.BearerToken(
            self.mock_validator,
            expires_in=self.expires_in
        )
        self.endpoint = TokenEndpoint(
            'authorization_code',
            default_token_type=token,
            grant_types=supported_types
        )
 def __init__(self, request_validator, token_generator=None,
              token_expires_in=None, refresh_token_generator=None, **kwargs):
     """Construct a client credentials grant server.
     :param request_validator: An implementation of
                               oauthlib.oauth2.RequestValidator.
     :param token_expires_in: An int or a function to generate a token
                              expiration offset (in seconds) given a
                              oauthlib.common.Request object.
     :param token_generator: A function to generate a token from a request.
     :param refresh_token_generator: A function to generate a token from a
                                     request for the refresh token.
     :param kwargs: Extra parameters to pass to authorization-,
                    token-, resource-, and revocation-endpoint constructors.
     """
     refresh_grant = SocialTokenGrant(request_validator)
     bearer = BearerToken(request_validator, token_generator,
                          token_expires_in, refresh_token_generator)
     TokenEndpoint.__init__(self, default_grant_type='convert_token',
                            grant_types={
                                'convert_token': refresh_grant,
                            },
                            default_token_type=bearer)
Example #11
0
    def setUp(self):
        def set_user(request):
            request.user = mock.MagicMock()
            request.client = mock.MagicMock()
            request.client.client_id = "mocked_client_id"
            return True

        self.mock_validator = mock.MagicMock()
        self.mock_validator.authenticate_client.side_effect = set_user
        self.addCleanup(setattr, self, "mock_validator", mock.MagicMock())
        auth_code = AuthorizationCodeGrant(request_validator=self.mock_validator)
        password = ResourceOwnerPasswordCredentialsGrant(request_validator=self.mock_validator)
        client = ClientCredentialsGrant(request_validator=self.mock_validator)
        supported_types = {"authorization_code": auth_code, "password": password, "client_credentials": client}
        self.expires_in = 1800
        token = tokens.BearerToken(self.mock_validator, expires_in=self.expires_in)
        self.endpoint = TokenEndpoint("authorization_code", default_token_type=token, grant_types=supported_types)
Example #12
0
class TokenEndpointTest(TestCase):
    def setUp(self):
        def set_user(request):
            request.user = mock.MagicMock()
            request.client = mock.MagicMock()
            request.client.client_id = "mocked_client_id"
            return True

        self.mock_validator = mock.MagicMock()
        self.mock_validator.authenticate_client.side_effect = set_user
        self.addCleanup(setattr, self, "mock_validator", mock.MagicMock())
        auth_code = AuthorizationCodeGrant(request_validator=self.mock_validator)
        password = ResourceOwnerPasswordCredentialsGrant(request_validator=self.mock_validator)
        client = ClientCredentialsGrant(request_validator=self.mock_validator)
        supported_types = {"authorization_code": auth_code, "password": password, "client_credentials": client}
        self.expires_in = 1800
        token = tokens.BearerToken(self.mock_validator, expires_in=self.expires_in)
        self.endpoint = TokenEndpoint("authorization_code", default_token_type=token, grant_types=supported_types)

    @mock.patch("oauthlib.common.generate_token", new=lambda: "abc")
    def test_authorization_grant(self):
        body = "grant_type=authorization_code&code=abc&scope=all+of+them&state=xyz"
        headers, body, status_code = self.endpoint.create_token_response("", body=body)
        token = {
            "token_type": "Bearer",
            "expires_in": self.expires_in,
            "access_token": "abc",
            "refresh_token": "abc",
            "state": "xyz",
        }
        self.assertEqual(json.loads(body), token)

    @mock.patch("oauthlib.common.generate_token", new=lambda: "abc")
    def test_password_grant(self):
        body = "grant_type=password&username=a&password=hello&scope=all+of+them"
        headers, body, status_code = self.endpoint.create_token_response("", body=body)
        token = {
            "token_type": "Bearer",
            "expires_in": self.expires_in,
            "access_token": "abc",
            "refresh_token": "abc",
            "scope": "all of them",
        }
        self.assertEqual(json.loads(body), token)

    @mock.patch("oauthlib.common.generate_token", new=lambda: "abc")
    def test_client_grant(self):
        body = "grant_type=client_credentials&scope=all+of+them"
        headers, body, status_code = self.endpoint.create_token_response("", body=body)
        token = {"token_type": "Bearer", "expires_in": self.expires_in, "access_token": "abc", "scope": "all of them"}
        self.assertEqual(json.loads(body), token)

    def test_missing_type(self):
        _, body, _ = self.endpoint.create_token_response("", body="")
        token = {"error": "unsupported_grant_type"}
        self.assertEqual(json.loads(body), token)

    def test_invalid_type(self):
        body = "grant_type=invalid"
        _, body, _ = self.endpoint.create_token_response("", body=body)
        token = {"error": "unsupported_grant_type"}
        self.assertEqual(json.loads(body), token)
class TokenEndpointTest(TestCase):

    def setUp(self):
        def set_user(request):
            request.user = mock.MagicMock()
            request.client = mock.MagicMock()
            request.client.client_id = 'mocked_client_id'
            return True

        self.mock_validator = mock.MagicMock()
        self.mock_validator.authenticate_client.side_effect = set_user
        self.mock_validator.get_code_challenge.return_value = None
        self.addCleanup(setattr, self, 'mock_validator', mock.MagicMock())
        auth_code = AuthorizationCodeGrant(
            request_validator=self.mock_validator)
        password = ResourceOwnerPasswordCredentialsGrant(
            request_validator=self.mock_validator)
        client = ClientCredentialsGrant(
            request_validator=self.mock_validator)
        supported_types = {
            'authorization_code': auth_code,
            'password': password,
            'client_credentials': client,
        }
        self.expires_in = 1800
        token = tokens.BearerToken(
            self.mock_validator,
            expires_in=self.expires_in
        )
        self.endpoint = TokenEndpoint(
            'authorization_code',
            default_token_type=token,
            grant_types=supported_types
        )

    @mock.patch('oauthlib.common.generate_token', new=lambda: 'abc')
    def test_authorization_grant(self):
        body = 'grant_type=authorization_code&code=abc&scope=all+of+them'
        headers, body, status_code = self.endpoint.create_token_response(
            '', body=body)
        token = {
            'token_type': 'Bearer',
            'expires_in': self.expires_in,
            'access_token': 'abc',
            'refresh_token': 'abc',
            'scope': 'all of them'
        }
        self.assertEqual(json.loads(body), token)

        body = 'grant_type=authorization_code&code=abc'
        headers, body, status_code = self.endpoint.create_token_response(
            '', body=body)
        token = {
            'token_type': 'Bearer',
            'expires_in': self.expires_in,
            'access_token': 'abc',
            'refresh_token': 'abc'
        }
        self.assertEqual(json.loads(body), token)

        # try with additional custom variables
        body = 'grant_type=authorization_code&code=abc&state=foobar'
        headers, body, status_code = self.endpoint.create_token_response(
            '', body=body)
        self.assertEqual(json.loads(body), token)

    @mock.patch('oauthlib.common.generate_token', new=lambda: 'abc')
    def test_password_grant(self):
        body = 'grant_type=password&username=a&password=hello&scope=all+of+them'
        headers, body, status_code = self.endpoint.create_token_response(
            '', body=body)
        token = {
            'token_type': 'Bearer',
            'expires_in': self.expires_in,
            'access_token': 'abc',
            'refresh_token': 'abc',
            'scope': 'all of them',
        }
        self.assertEqual(json.loads(body), token)

    @mock.patch('oauthlib.common.generate_token', new=lambda: 'abc')
    def test_client_grant(self):
        body = 'grant_type=client_credentials&scope=all+of+them'
        headers, body, status_code = self.endpoint.create_token_response(
            '', body=body)
        token = {
            'token_type': 'Bearer',
            'expires_in': self.expires_in,
            'access_token': 'abc',
            'scope': 'all of them',
        }
        self.assertEqual(json.loads(body), token)

    def test_missing_type(self):
        _, body, _ = self.endpoint.create_token_response('', body='')
        token = {'error': 'unsupported_grant_type'}
        self.assertEqual(json.loads(body), token)

    def test_invalid_type(self):
        body = 'grant_type=invalid'
        _, body, _ = self.endpoint.create_token_response('', body=body)
        token = {'error': 'unsupported_grant_type'}
        self.assertEqual(json.loads(body), token)