Exemplo n.º 1
0
def includeme(config):
    registry = config.registry
    settings = registry.settings

    config.include('pyramid_oauthlib')
    config.add_oauth_param('assertion')

    # Use session credentials as a client credentials authorization grant
    config.add_grant_type('oauthlib.oauth2.ClientCredentialsGrant',
                          request_validator=validator)

    # Use web tokens as an authorization grant
    config.add_grant_type('h.oauth.JWTBearerGrant',
                          JWT_BEARER,
                          request_validator=validator)

    # Use web tokens for resource authorization
    config.add_token_type('oauthlib.oauth2.BearerToken',
                          request_validator=validator,
                          token_generator=generate_signed_token)

    # Configure a default client factory
    client_class = settings.get('auth.client_factory', 'h.models.Client')
    config.add_directive('set_client_factory', set_client_factory)
    config.set_client_factory(client_class)

    # Set default client credentials
    settings.setdefault('h.client_id', generate_client_id())
    settings.setdefault('h.client_secret', generate_client_id())
Exemplo n.º 2
0
Arquivo: auth.py Projeto: chrber/h
def includeme(config):
    registry = config.registry
    settings = registry.settings

    config.include('pyramid_oauthlib')
    config.add_oauth_param('assertion')

    # Use session credentials as a client credentials authorization grant
    config.add_grant_type('oauthlib.oauth2.ClientCredentialsGrant',
                          request_validator=validator)

    # Use web tokens as an authorization grant
    config.add_grant_type('h.oauth.JWTBearerGrant', JWT_BEARER,
                          request_validator=validator)

    # Use web tokens for resource authorization
    config.add_token_type('oauthlib.oauth2.BearerToken',
                          request_validator=validator,
                          token_generator=generate_signed_token)

    # Configure a default client factory
    client_class = settings.get('auth.client_factory', 'h.models.Client')
    config.add_directive('set_client_factory', set_client_factory)
    config.set_client_factory(client_class)

    # Set default client credentials
    settings.setdefault('h.client_id', generate_client_id())
    settings.setdefault('h.client_secret', generate_client_id())
Exemplo n.º 3
0
    def test_generate_client_id(self):
        client_id = generate_client_id()
        self.assertEqual(len(client_id), 30)

        client_id = generate_client_id(length=44)
        self.assertEqual(len(client_id), 44)

        client_id = generate_client_id(length=6, chars="python")
        self.assertEqual(len(client_id), 6)
        for c in client_id:
            self.assertIn(c, "python")
Exemplo n.º 4
0
    def test_generate_client_id(self):
        client_id = generate_client_id()
        self.assertEqual(len(client_id), 30)

        client_id = generate_client_id(length=44)
        self.assertEqual(len(client_id), 44)

        client_id = generate_client_id(length=6, chars="python")
        self.assertEqual(len(client_id), 6)
        for c in client_id:
            self.assertIn(c, "python")
Exemplo n.º 5
0
    def test_custom_launch_params(self):
        key = generate_client_id()
        secret = generate_token()
        lp = {
            'lti_version': 'foo',
            'lti_message_type': 'bar',
            'resource_link_id': 123,
            'launch_url': 'more_foo',
            'basiclti_submit': 'more_bar'
        }
        launch_url = 'http://example.edu/foo/bar'
        launch_headers = {'Content-Type': 'baz'}
        tp = create_tp(key,
                       secret,
                       lp,
                       launch_url,
                       launch_headers,
                       tp_class=CustomToolProvider)

        with patch.object(SignatureOnlyEndpoint, 'validate_request') as mv:
            mv.return_value = True, None  # Tuple of valid, request
            self.assertTrue(tp.is_valid_request(Mock()))
            call_url, call_method, call_params, call_headers = mv.call_args[0]
            self.assertEqual(call_url, launch_url)
            self.assertEqual(call_method, 'POST')
            self.assertEqual(call_params, lp)
            self.assertEqual(call_headers, launch_headers)
Exemplo n.º 6
0
    def test_constructor(self):
        client_id = generate_client_id()
        client_secret = generate_token()
        tc = ToolConsumer(client_id, client_secret,
                          launch_url='http://example.edu')
        self.assertIsInstance(tc.launch_params, LaunchParams)

        lp = LaunchParams()
        tc = ToolConsumer(client_id, client_secret,
                          launch_url='http://example.edu', params=lp)
        self.assertEqual(tc.launch_params, lp)

        lp_dict = {'resource_link_id': 1}
        tc = ToolConsumer(client_id, client_secret,
                          launch_url='http://example.edu',
                          params=lp_dict)
        self.assertIsInstance(tc.launch_params, LaunchParams)
        self.assertEqual(tc.launch_params._params.get('resource_link_id'), 1)

        # no launch_url should raise exception
        self.failUnlessRaises(InvalidLTIConfigError, ToolConsumer,
                              client_id, client_secret,
                              params=lp_dict)

        # but confirm that 'launch_url' can still be passed in params
        # (backwards compatibility)
        lp_dict['launch_url'] = 'http://example.edu'
        tc = ToolConsumer(client_id, client_secret, params=lp_dict)
        self.assertEqual(tc.launch_url, 'http://example.edu')
Exemplo n.º 7
0
    def test_constructor(self):
        client_id = generate_client_id()
        client_secret = generate_token()
        tc = ContentItemResponse(client_id, client_secret,
                          launch_url='http://example.edu')
        self.assertIsInstance(tc.launch_params, LaunchParams)

        lp = LaunchParams()
        tc = ContentItemResponse(client_id, client_secret,
                          launch_url='http://example.edu', params=lp)
        self.assertEqual(tc.launch_params, lp)

        lp_dict = {'resource_link_id': 1}
        tc = ContentItemResponse(client_id, client_secret,
                          launch_url='http://example.edu',
                          params=lp_dict)
        self.assertIsInstance(tc.launch_params, LaunchParams)
        self.assertEqual(tc.launch_params._params.get('resource_link_id'), 1)

        # no launch_url should raise exception
        self.failUnlessRaises(InvalidLTIConfigError, ContentItemResponse,
                              client_id, client_secret,
                              params=lp_dict)

        # but confirm that 'launch_url' can still be passed in params
        # (backwards compatibility)
        lp_dict['launch_url'] = 'http://example.edu'
        tc = ContentItemResponse(client_id, client_secret, params=lp_dict)
        self.assertEqual(tc.launch_url, 'http://example.edu')
Exemplo n.º 8
0
    def test_new_request(self):
        key = generate_client_id()
        secret = generate_token()
        lp = {
            'lti_version': 'foo',
            'lti_message_type': 'bar',
            'resource_link_id': 123
        }
        tp = create_tp(key, secret, lp)
        req = tp.new_request({})
        self.assertIsInstance(req, OutcomeRequest)
        self.assertEqual(req, tp._last_outcome_request)
        self.assertEqual(req.consumer_key, key)
        self.assertEqual(len(tp.outcome_requests), 1)

        # outcome request should get assigned attr
        req = tp.new_request({'score': 1.0})
        self.assertEqual(req.score, 1.0)
        self.assertEqual(len(tp.outcome_requests), 2)

        # but can't override some fields
        req = tp.new_request({'consumer_key': 'foo'})
        self.assertEqual(req.consumer_key, key)
        self.assertEqual(len(tp.outcome_requests), 3)

        # should fail if we use an invalid opt
        self.assertRaises(InvalidLTIConfigError, tp.new_request, {'foo': 1})
        self.assertEqual(len(tp.outcome_requests), 3)
Exemplo n.º 9
0
    def test_new_request(self):
        key = generate_client_id()
        secret = generate_token()
        lp = {
            'lti_version': 'foo',
            'lti_message_type': 'bar',
            'resource_link_id': 123
        }
        tp = create_tp(key, secret, lp)
        req = tp.new_request({})
        self.assertIsInstance(req, OutcomeRequest)
        self.assertEqual(req, tp._last_outcome_request)
        self.assertEqual(req.consumer_key, key)
        self.assertEqual(len(tp.outcome_requests), 1)

        # outcome request should get assigned attr
        req = tp.new_request({'score': 1.0})
        self.assertEqual(req.score, 1.0)
        self.assertEqual(len(tp.outcome_requests), 2)

        # but can't override some fields
        req = tp.new_request({'consumer_key': 'foo'})
        self.assertEqual(req.consumer_key, key)
        self.assertEqual(len(tp.outcome_requests), 3)

        # should fail if we use an invalid opt
        self.assertRaises(InvalidLTIConfigError, tp.new_request, {'foo': 1})
        self.assertEqual(len(tp.outcome_requests), 3)
Exemplo n.º 10
0
def create_tp(key=None, secret=None, lp=None, launch_url=None,
              launch_headers=None, tp_class=ToolProvider):
    key = key or generate_client_id()
    secret = secret or generate_token()
    launch_params = LaunchParams()
    if lp is not None:
        launch_params.update(lp)
    launch_url = launch_url or "http://example.edu"
    launch_headers = launch_headers or {}
    return tp_class(key, secret, launch_params, launch_url, launch_headers)
Exemplo n.º 11
0
    def test_has_required_params(self):

        client_id = generate_client_id()
        client_secret = generate_token()
        tc = ToolConsumer(client_id, client_secret, launch_url="http://example.edu")

        self.assertFalse(tc.has_required_params())

        tc.launch_params["resource_link_id"] = generate_token()
        self.assertTrue(tc.has_required_params())
Exemplo n.º 12
0
    def test_has_required_params(self):

        client_id = generate_client_id()
        client_secret = generate_token()
        tc = ToolConsumer(client_id,
                          client_secret,
                          launch_url='http://example.edu')

        self.assertFalse(tc.has_required_params())

        tc.launch_params['resource_link_id'] = generate_token()
        self.assertTrue(tc.has_required_params())
Exemplo n.º 13
0
    def test_has_required_params(self):

        client_id = generate_client_id()
        client_secret = generate_token()
        tc = ContentItemResponse(client_id, client_secret,
                          launch_url='http://example.edu')

        #Can't assert false for has_required_params as the only required params are lti_version and lti_message_type
        #However should consider checking the message type in the future

        tc.launch_params['lti_version'] = 'LTI-1p0'
        tc.launch_params['lti_message_type'] = 'ContentItemSelection'
        self.assertTrue(tc.has_required_params())
Exemplo n.º 14
0
    def __init__(self, **kwargs):

        if 'token' not in kwargs or kwargs['token'] is None:
            token = generate_client_id(length=32, chars=UNICODE_ASCII_CHARACTER_SET)
            kwargs['token'] = token
            kwargs['uid'] = token

        expires_in = 720
        if 'expires_in' in kwargs:
            expires_in = int(kwargs['expires_in'])
            del kwargs['expires_in']

        if 'expires' not in kwargs or kwargs['expires'] is None:
            kwargs['expires'] = datetime.datetime.utcnow() + datetime.timedelta(minutes=expires_in)

        super(VerificationToken, self).__init__(**kwargs)
Exemplo n.º 15
0
    def test_custom_launch_params(self):
        key = generate_client_id()
        secret = generate_token()
        lp = {
            'lti_version': 'foo',
            'lti_message_type': 'bar',
            'resource_link_id': 123,
            'launch_url': 'more_foo',
            'basiclti_submit': 'more_bar'
        }
        launch_url = 'http://example.edu/foo/bar'
        launch_headers = {'Content-Type': 'baz'}
        tp = create_tp(key, secret, lp, launch_url, launch_headers, tp_class=CustomToolProvider)

        with patch.object(SignatureOnlyEndpoint, 'validate_request') as mv:
            mv.return_value = True, None  # Tuple of valid, request
            self.assertTrue(tp.is_valid_request(Mock()))
            call_url, call_method, call_params, call_headers = mv.call_args[0]
            self.assertEqual(call_url, launch_url)
            self.assertEqual(call_method, 'POST')
            self.assertEqual(call_params, lp)
            self.assertEqual(call_headers, launch_headers)
Exemplo n.º 16
0
    def test_is_valid_request(self):
        """
        just checks that the TP sends the correct args to the endpoint
        """
        key = generate_client_id()
        secret = generate_token()
        lp = {
            'lti_version': 'foo',
            'lti_message_type': 'bar',
            'resource_link_id': 123
        }
        launch_url = 'http://example.edu/foo/bar'
        launch_headers = {'Content-Type': 'baz'}
        tp = create_tp(key, secret, lp, launch_url, launch_headers)

        with patch.object(SignatureOnlyEndpoint, 'validate_request') as mv:
            mv.return_value = True
            self.assertTrue(tp.is_valid_request(Mock()))
            call_url, call_method, call_params, call_headers = mv.call_args[0]
            self.assertEqual(call_url, launch_url)
            self.assertEqual(call_method, 'POST')
            self.assertEqual(call_params, lp)
            self.assertEqual(call_headers, launch_headers)
Exemplo n.º 17
0
    def test_is_valid_request(self):
        """
        just checks that the TP sends the correct args to the endpoint
        """
        key = generate_client_id()
        secret = generate_token()
        lp = {
            'lti_version': 'foo',
            'lti_message_type': 'bar',
            'resource_link_id': 123
        }
        launch_url = 'http://example.edu/foo/bar'
        launch_headers = {'Content-Type': 'baz'}
        tp = create_tp(key, secret, lp, launch_url, launch_headers)

        with patch.object(SignatureOnlyEndpoint, 'validate_request') as mv:
            mv.return_value = True
            self.assertTrue(tp.is_valid_request(Mock()))
            call_url, call_method, call_params, call_headers = mv.call_args[0]
            self.assertEqual(call_url, launch_url)
            self.assertEqual(call_method, 'POST')
            self.assertEqual(call_params, lp)
            self.assertEqual(call_headers, launch_headers)
Exemplo n.º 18
0
def create_tb(key=None, secret=None, lp=None):
    key = key or generate_client_id()
    secret = secret or generate_token()
    lp = lp or LaunchParams()
    return ToolBase(key, secret, lp)
Exemplo n.º 19
0
 def hash(self):
     return generate_client_id(length=24, chars=UNICODE_ASCII_CHARACTER_SET)
Exemplo n.º 20
0
def create_tb(key=None, secret=None, lp=None):
    key = key or generate_client_id()
    secret = secret or generate_token()
    lp = lp or LaunchParams()
    return ToolBase(key, secret, lp)
Exemplo n.º 21
0
 def hash(self):
     """
     生成一个客户端ID(只包含数字/小写/大写)
     :return:
     """
     return generate_client_id(length=40, chars=CLIENT_ID_CHARACTER_SET)
Exemplo n.º 22
0
 def hash(self):
     datestamp = datetime.date.today().isoformat().replace("-", "")
     return datestamp + generate_client_id(
         length=24, chars=UNICODE_ASCII_CHARACTER_SET)