当前位置: 首页>>代码示例>>Python>>正文


Python xsrfutil.generate_token方法代码示例

本文整理汇总了Python中oauth2client.contrib.xsrfutil.generate_token方法的典型用法代码示例。如果您正苦于以下问题:Python xsrfutil.generate_token方法的具体用法?Python xsrfutil.generate_token怎么用?Python xsrfutil.generate_token使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在oauth2client.contrib.xsrfutil的用法示例。


在下文中一共展示了xsrfutil.generate_token方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _build_state_value

# 需要导入模块: from oauth2client.contrib import xsrfutil [as 别名]
# 或者: from oauth2client.contrib.xsrfutil import generate_token [as 别名]
def _build_state_value(request_handler, user):
    """Composes the value for the 'state' parameter.

    Packs the current request URI and an XSRF token into an opaque string that
    can be passed to the authentication server via the 'state' parameter.

    Args:
        request_handler: webapp.RequestHandler, The request.
        user: google.appengine.api.users.User, The current user.

    Returns:
        The state value as a string.
    """
    uri = request_handler.request.url
    token = xsrfutil.generate_token(xsrf_secret_key(), user.user_id(),
                                    action_id=str(uri))
    return uri + ':' + token 
开发者ID:0x0ece,项目名称:oscars2016,代码行数:19,代码来源:appengine.py

示例2: test_token_length_differs_from_generated

# 需要导入模块: from oauth2client.contrib import xsrfutil [as 别名]
# 或者: from oauth2client.contrib.xsrfutil import generate_token [as 别名]
def test_token_length_differs_from_generated(self):
        token_time = 123456789
        # Make sure it isn't too old.
        curr_time = token_time + xsrfutil.DEFAULT_TIMEOUT_SECS - 1

        key = object()
        user_id = object()
        action_id = object()
        token = base64.b64encode(_helpers._to_bytes(str(token_time)))
        generated_token = b'a'
        # Make sure the token length comparison will fail.
        self.assertNotEqual(len(token), len(generated_token))

        with mock.patch('oauth2client.contrib.xsrfutil.generate_token',
                        return_value=generated_token) as gen_tok:
            self.assertFalse(xsrfutil.validate_token(key, token, user_id,
                                                     current_time=curr_time,
                                                     action_id=action_id))
            gen_tok.assert_called_once_with(key, user_id, action_id=action_id,
                                            when=token_time) 
开发者ID:openstack,项目名称:deb-python-oauth2client,代码行数:22,代码来源:test_xsrfutil.py

示例3: test_token_differs_from_generated_but_same_length

# 需要导入模块: from oauth2client.contrib import xsrfutil [as 别名]
# 或者: from oauth2client.contrib.xsrfutil import generate_token [as 别名]
def test_token_differs_from_generated_but_same_length(self):
        token_time = 123456789
        # Make sure it isn't too old.
        curr_time = token_time + xsrfutil.DEFAULT_TIMEOUT_SECS - 1

        key = object()
        user_id = object()
        action_id = object()
        token = base64.b64encode(_helpers._to_bytes(str(token_time)))
        # It is encoded as b'MTIzNDU2Nzg5', which has length 12.
        generated_token = b'M' * 12
        # Make sure the token length comparison will succeed, but the token
        # comparison will fail.
        self.assertEqual(len(token), len(generated_token))
        self.assertNotEqual(token, generated_token)

        with mock.patch('oauth2client.contrib.xsrfutil.generate_token',
                        return_value=generated_token) as gen_tok:
            self.assertFalse(xsrfutil.validate_token(key, token, user_id,
                                                     current_time=curr_time,
                                                     action_id=action_id))
            gen_tok.assert_called_once_with(key, user_id, action_id=action_id,
                                            when=token_time) 
开发者ID:openstack,项目名称:deb-python-oauth2client,代码行数:25,代码来源:test_xsrfutil.py

示例4: test_bad_positional

# 需要导入模块: from oauth2client.contrib import xsrfutil [as 别名]
# 或者: from oauth2client.contrib.xsrfutil import generate_token [as 别名]
def test_bad_positional(self):
        # Need 2 positional arguments.
        with self.assertRaises(TypeError):
            xsrfutil.generate_token(None)
        # At most 2 positional arguments.
        with self.assertRaises(TypeError):
            xsrfutil.generate_token(None, None, None) 
开发者ID:openstack,项目名称:deb-python-oauth2client,代码行数:9,代码来源:test_xsrfutil.py

示例5: test_it

# 需要导入模块: from oauth2client.contrib import xsrfutil [as 别名]
# 或者: from oauth2client.contrib.xsrfutil import generate_token [as 别名]
def test_it(self):
        digest = b'foobar'
        digester = mock.Mock()
        digester.digest = mock.Mock(name='digest', return_value=digest)
        with mock.patch('oauth2client.contrib.xsrfutil.hmac') as hmac:
            hmac.new = mock.Mock(name='new', return_value=digester)
            token = xsrfutil.generate_token(TEST_KEY,
                                            TEST_USER_ID_1,
                                            action_id=TEST_ACTION_ID_1,
                                            when=TEST_TIME)
            hmac.new.assert_called_once_with(TEST_KEY)
            digester.digest.assert_called_once_with()

            expected_digest_calls = [
                mock.call.update(_helpers._to_bytes(str(TEST_USER_ID_1))),
                mock.call.update(xsrfutil.DELIMITER),
                mock.call.update(TEST_ACTION_ID_1),
                mock.call.update(xsrfutil.DELIMITER),
                mock.call.update(_helpers._to_bytes(str(TEST_TIME))),
            ]
            self.assertEqual(digester.method_calls, expected_digest_calls)

            expected_token_as_bytes = (digest + xsrfutil.DELIMITER +
                                       _helpers._to_bytes(str(TEST_TIME)))
            expected_token = base64.urlsafe_b64encode(
                expected_token_as_bytes)
            self.assertEqual(token, expected_token) 
开发者ID:openstack,项目名称:deb-python-oauth2client,代码行数:29,代码来源:test_xsrfutil.py

示例6: test_with_system_time

# 需要导入模块: from oauth2client.contrib import xsrfutil [as 别名]
# 或者: from oauth2client.contrib.xsrfutil import generate_token [as 别名]
def test_with_system_time(self):
        digest = b'foobar'
        curr_time = 1440449755.74
        digester = mock.Mock()
        digester.digest = mock.Mock(name='digest', return_value=digest)
        with mock.patch('oauth2client.contrib.xsrfutil.hmac') as hmac:
            hmac.new = mock.Mock(name='new', return_value=digester)

            with mock.patch('oauth2client.contrib.xsrfutil.time') as time:
                time.time = mock.Mock(name='time', return_value=curr_time)
                # when= is omitted
                token = xsrfutil.generate_token(TEST_KEY,
                                                TEST_USER_ID_1,
                                                action_id=TEST_ACTION_ID_1)

                hmac.new.assert_called_once_with(TEST_KEY)
                time.time.assert_called_once_with()
                digester.digest.assert_called_once_with()

                expected_digest_calls = [
                    mock.call.update(_helpers._to_bytes(str(TEST_USER_ID_1))),
                    mock.call.update(xsrfutil.DELIMITER),
                    mock.call.update(TEST_ACTION_ID_1),
                    mock.call.update(xsrfutil.DELIMITER),
                    mock.call.update(_helpers._to_bytes(str(int(curr_time)))),
                ]
                self.assertEqual(digester.method_calls, expected_digest_calls)

                expected_token_as_bytes = (
                    digest + xsrfutil.DELIMITER +
                    _helpers._to_bytes(str(int(curr_time))))
                expected_token = base64.urlsafe_b64encode(
                    expected_token_as_bytes)
                self.assertEqual(token, expected_token) 
开发者ID:openstack,项目名称:deb-python-oauth2client,代码行数:36,代码来源:test_xsrfutil.py

示例7: setup

# 需要导入模块: from oauth2client.contrib import xsrfutil [as 别名]
# 或者: from oauth2client.contrib.xsrfutil import generate_token [as 别名]
def setup(request):
  FLOW.params['state'] = xsrfutil.generate_token(settings.SECRET_KEY,
                                                 request.user.username)
  authorize_url = FLOW.step1_get_authorize_url()
  return HttpResponseRedirect(authorize_url) 
开发者ID:google,项目名称:mirandum,代码行数:7,代码来源:views.py

示例8: setup

# 需要导入模块: from oauth2client.contrib import xsrfutil [as 别名]
# 或者: from oauth2client.contrib.xsrfutil import generate_token [as 别名]
def setup(request):
  state_token = xsrfutil.generate_token(settings.SECRET_KEY,
                                                 request.user.username)
  if 'redir' in request.GET and request.GET['redir'] in ['ffsetup', 'subs', 'sponsors']:
    state_token = "%s|%s" % (state_token, request.GET['redir'])
  FLOW.params['state'] = state_token
  authorize_url = FLOW.step1_get_authorize_url()
  return HttpResponseRedirect(authorize_url) 
开发者ID:google,项目名称:mirandum,代码行数:10,代码来源:views.py


注:本文中的oauth2client.contrib.xsrfutil.generate_token方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。