本文整理汇总了Python中gratipay.security.user.User类的典型用法代码示例。如果您正苦于以下问题:Python User类的具体用法?Python User怎么用?Python User使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了User类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_auth_from_request
def get_auth_from_request(request):
"""Authenticate from a cookie or an API key in basic auth.
"""
user = None
if request.line.uri.startswith('/assets/'):
pass
elif 'Authorization' in request.headers:
header = request.headers['authorization']
if header.startswith('Basic '):
creds = header[len('Basic '):].decode('base64')
token, ignored = creds.split(':')
user = User.from_api_key(token)
# We don't require CSRF if they basically authenticated.
csrf_token = csrf._get_new_csrf_key()
request.headers.cookie['csrf_token'] = csrf_token
request.headers['X-CSRF-TOKEN'] = csrf_token
if 'Referer' not in request.headers:
request.headers['Referer'] = \
'https://%s/' % csrf._get_host(request)
elif SESSION in request.headers.cookie:
token = request.headers.cookie[SESSION].value
user = User.from_session_token(token)
request.context['user'] = user or User()
示例2: test_user_can_be_loaded_from_session_token
def test_user_can_be_loaded_from_session_token(self):
self.make_participant('alice')
user = User.from_username('alice')
user.sign_in(SimpleCookie())
token = user.participant.session_token
actual = User.from_session_token(token).participant.username
assert actual == 'alice'
示例3: test_user_from_expired_session_is_anonymous
def test_user_from_expired_session_is_anonymous(self):
self.make_participant('alice')
user = User.from_username('alice')
user.sign_in(SimpleCookie())
token = user.participant.session_token
user.participant.set_session_expires(utcnow())
user = User.from_session_token(token)
assert user.ANON
示例4: _get_user_via_api_key
def _get_user_via_api_key(api_key):
"""Given an api_key, return a User. This auth method is deprecated.
"""
user = User()
user.participant = Participant._from_thing('api_key', api_key)
if user.participant:
p = user.participant
today = date.today()
if p.old_auth_usage != today:
Participant.db.run("""
UPDATE participants
SET old_auth_usage = %s
WHERE id = %s
""", (today, p.id))
return user
示例5: test_session_cookie_is_secure_if_it_should_be
def test_session_cookie_is_secure_if_it_should_be(self):
canonical_scheme = gratipay.canonical_scheme
gratipay.canonical_scheme = 'https'
try:
cookies = SimpleCookie()
self.make_participant('alice')
user = User.from_username('alice')
user.sign_in(cookies)
assert '; secure' in cookies[SESSION].output()
finally:
gratipay.canonical_scheme = canonical_scheme
示例6: test_session_is_regularly_refreshed
def test_session_is_regularly_refreshed(self):
self.make_participant('alice')
user = User.from_username('alice')
user.sign_in(SimpleCookie())
cookies = SimpleCookie()
user.keep_signed_in(cookies)
assert SESSION not in cookies
cookies = SimpleCookie()
expires = user.participant.session_expires
user.participant.set_session_expires(expires - SESSION_REFRESH)
user.keep_signed_in(cookies)
assert SESSION in cookies
示例7: authenticate_user_if_possible
def authenticate_user_if_possible(request, user):
"""This signs the user in.
"""
if request.line.uri.startswith('/assets/'):
pass
elif 'Authorization' in request.headers:
header = request.headers['authorization']
if header.startswith('Basic '):
user = _get_user_via_basic_auth(header)
if not user.ANON:
_turn_off_csrf(request)
elif SESSION in request.headers.cookie:
token = request.headers.cookie[SESSION].value
user = User.from_session_token(token)
return {'user': user}
示例8: opt_in
def opt_in(self, desired_username):
"""Given a desired username, return a User object.
"""
from gratipay.security.user import User
user = User.from_username(self.participant.username)
assert not user.ANON, self.participant # sanity check
if self.participant.is_claimed:
newly_claimed = False
else:
newly_claimed = True
user.participant.set_as_claimed()
try:
user.participant.change_username(desired_username)
except ProblemChangingUsername:
pass
if user.participant.is_closed:
user.participant.update_is_closed(False)
return user, newly_claimed
示例9: set_request_context_user
def set_request_context_user(request):
"""Set request.context['user']. This signs the user in.
"""
request.context['user'] = user = ANON # Make sure we always have a user object, even if
# there's an exception in the rest of this function.
if request.line.uri.startswith('/assets/'):
pass
elif 'Authorization' in request.headers:
header = request.headers['authorization']
if header.startswith('Basic '):
user = _get_user_via_basic_auth(header)
if not user.ANON:
_turn_off_csrf(request)
elif SESSION in request.headers.cookie:
token = request.headers.cookie[SESSION].value
user = User.from_session_token(token)
request.context['user'] = user
示例10: build_wsgi_environ
def build_wsgi_environ(self, *a, **kw):
"""Extend base class to support authenticating as a certain user.
"""
# csrf - for both anon and authenticated
csrf_token = kw.get('csrf_token', b'sotokeny')
if csrf_token:
self.cookie[b'csrf_token'] = csrf_token
kw[b'HTTP_X-CSRF-TOKEN'] = csrf_token
# user authentication
auth_as = kw.pop('auth_as', None)
if auth_as is None:
if SESSION in self.cookie:
del self.cookie[SESSION]
else:
user = User.from_username(auth_as)
user.sign_in(self.cookie)
return Client.build_wsgi_environ(self, *a, **kw)
示例11: _get_user_via_basic_auth
def _get_user_via_basic_auth(auth_header):
"""Given a basic auth header, return a User object.
"""
try:
creds = binascii.a2b_base64(auth_header[len('Basic '):]).split(':', 1)
except binascii.Error:
raise Response(400, 'Malformed "Authorization" header')
if len(creds) != 2:
raise Response(401)
userid, api_key = creds
if len(userid) == 36 and '-' in userid:
user = _get_user_via_api_key(userid) # For backward-compatibility
else:
try:
userid = int(userid)
except ValueError:
raise Response(401)
user = User.from_id(userid)
if user.ANON or not constant_time_compare(user.participant.api_key, api_key):
raise Response(401)
return user
示例12: build_wsgi_environ
def build_wsgi_environ(self, *a, **kw):
"""Extend base class to support authenticating as a certain user.
"""
self.cookie.clear()
# csrf - for both anon and authenticated
csrf_token = kw.get('csrf_token', b'ThisIsATokenThatIsThirtyTwoBytes')
if csrf_token:
self.cookie[b'csrf_token'] = csrf_token
kw[b'HTTP_X-CSRF-TOKEN'] = csrf_token
# user authentication
auth_as = kw.pop('auth_as', None)
if auth_as:
user = User.from_username(auth_as)
user.sign_in(self.cookie)
for k, v in kw.pop('cookies', {}).items():
self.cookie[k] = v
return Client.build_wsgi_environ(self, *a, **kw)
示例13: test_show_as_team_to_admin
def test_show_as_team_to_admin(self):
self.make_participant('alice', is_admin=True)
user = User.from_username('alice')
assert self.team.show_as_team(user)
示例14: test_user_from_None_session_token_is_anonymous
def test_user_from_None_session_token_is_anonymous(self):
self.make_participant('alice')
self.make_participant('bob')
user = User.from_session_token(None)
assert user.ANON
示例15: test_user_from_None_id_is_anonymous
def test_user_from_None_id_is_anonymous(self):
user = User.from_id(None)
assert user.ANON