本文整理汇总了Python中django.core.signing.SignatureExpired方法的典型用法代码示例。如果您正苦于以下问题:Python signing.SignatureExpired方法的具体用法?Python signing.SignatureExpired怎么用?Python signing.SignatureExpired使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.core.signing
的用法示例。
在下文中一共展示了signing.SignatureExpired方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: verify
# 需要导入模块: from django.core import signing [as 别名]
# 或者: from django.core.signing import SignatureExpired [as 别名]
def verify(self):
data = self._data
signature = data.get(self.SIGNATURE_FIELD, None)
if signature is None:
raise BadSignature()
expected_signature = self.calculate_signature()
if not constant_time_compare(signature, expected_signature):
raise BadSignature()
valid_period = self.get_valid_period()
if self.USE_TIMESTAMP and valid_period is not None:
timestamp = data[self.TIMESTAMP_FIELD]
timestamp = int(timestamp)
current_timestamp = get_current_timestamp()
valid_period_secs = valid_period.total_seconds()
if current_timestamp - timestamp > valid_period_secs:
raise SignatureExpired()
示例2: get
# 需要导入模块: from django.core import signing [as 别名]
# 或者: from django.core.signing import SignatureExpired [as 别名]
def get(self, request, **kwargs):
token = kwargs.get('token')
try:
new_email = loads(token, max_age=self.timeout_seconds)
# 期限切れ
except SignatureExpired:
return HttpResponseBadRequest()
# tokenが間違っている
except BadSignature:
return HttpResponseBadRequest()
# tokenは問題なし
else:
User.objects.filter(email=new_email, is_active=False).delete()
request.user.email = new_email
request.user.save()
return super().get(request, **kwargs)
示例3: validate_token
# 需要导入模块: from django.core import signing [as 别名]
# 或者: from django.core.signing import SignatureExpired [as 别名]
def validate_token(token):
"""
Validate token.
returns MeetingInvitation or None
"""
try:
# Valid in 7 days
valid_in = 60 * 60 * 24 * 7
data = signing.loads(TimestampSigner().unsign(token, max_age=valid_in))
return MeetingInvitation.objects.filter(
user=int(data["user_id"]), meeting=int(data["meeting_id"])
)[0]
except (BadSignature, SignatureExpired):
return None
示例4: get_object
# 需要导入模块: from django.core import signing [as 别名]
# 或者: from django.core.signing import SignatureExpired [as 别名]
def get_object(self):
# Decode the timestamped data:
# - the PK of the AlumSurveyTracker
#
# If the timestamp is older than 1 month, display an error message.
#
# Figure out which model is not null (alumni_info or intern_info) to use.
# See if we already have an AlumSurvey that points to this survey tracker.
# If not, create it.
signer = TimestampSigner()
try:
this_pk = signer.unsign(self.kwargs['survey_slug'], max_age=timedelta(days=30))
except SignatureExpired:
raise PermissionDenied("The survey link has expired.")
except BadSignature:
raise PermissionDenied("Bad survey link.")
try:
return AlumSurvey.objects.get(survey_tracker__pk=this_pk)
except AlumSurvey.DoesNotExist:
tracker = get_object_or_404(AlumSurveyTracker, pk=this_pk)
return AlumSurvey(survey_tracker=tracker, survey_date=datetime.now())
# No need to override get_context because we can get everything from
# form.instance.survey_tracker
示例5: change_email
# 需要导入模块: from django.core import signing [as 别名]
# 或者: from django.core.signing import SignatureExpired [as 别名]
def change_email(request, token):
try:
data = signing.loads(token, max_age=TOKEN_MAX_AGE)
except signing.SignatureExpired:
return TemplateResponse(request, 'registration/token_expired.html')
except signing.BadSignature:
return TemplateResponse(request, 'registration/token_invalid.html')
if request.user.username != data.get('username'):
return TemplateResponse(request, 'registration/token_invalid.html')
email = data.get('email')
try:
validate_email(email)
except ValidationError:
return TemplateResponse(request, 'registration/token_invalid.html')
request.user.email = email
request.user.save()
messages.success(request, _('Your email address has been changed.'))
return redirect('registration_account')
示例6: verify_signer_or_bad_request
# 需要导入模块: from django.core import signing [as 别名]
# 或者: from django.core.signing import SignatureExpired [as 别名]
def verify_signer_or_bad_request(signer):
try:
signer.verify()
except SignatureExpired:
raise BadRequest(_("Signature expired"))
except BadSignature:
raise BadRequest(_("Invalid signature"))
示例7: test_verify_expired
# 需要导入模块: from django.core import signing [as 别名]
# 或者: from django.core.signing import SignatureExpired [as 别名]
def test_verify_expired(self):
timestamp = int(time.time())
with patch('time.time',
side_effect=lambda: timestamp):
signer1 = self.create_signer({
'email': self.test_email,
})
signed_data = signer1.get_signed_data()
signer2 = self.create_signer(signed_data)
with patch('time.time',
side_effect=lambda: timestamp + 3600 * 24 * 2):
self.assertRaises(SignatureExpired, signer2.verify)
示例8: _check_csrf
# 需要导入模块: from django.core import signing [as 别名]
# 或者: from django.core.signing import SignatureExpired [as 别名]
def _check_csrf(self, state):
signer = TimestampSigner()
if state is None:
state = ""
try:
state = signer.unsign(state, max_age=300)
except BadSignature: # pragma: no branch
logger.debug("state has been tempered with")
state = ""
except SignatureExpired: # pragma: no cover
logger.debug("state has expired")
state = ""
checks = (
re.search("[a-zA-Z0-9]", state),
len(state) == CSRF_TOKEN_LENGTH,
)
# validate state parameter
if not all(checks):
logger.debug("State validation failed:")
logger.debug("state: {}".format(state))
logger.debug("checks: {}".format(checks))
self.context["message"] = {"error": "bad_state"}
示例9: validate_reset_token
# 需要导入模块: from django.core import signing [as 别名]
# 或者: from django.core.signing import SignatureExpired [as 别名]
def validate_reset_token(token):
try:
return signing.loads(
TimestampSigner().unsign(token, max_age=settings.PASSWORD_RESET_TIMEOUT)
)["email"]
except (BadSignature, SignatureExpired):
return None
示例10: validate_registration_token
# 需要导入模块: from django.core import signing [as 别名]
# 或者: from django.core.signing import SignatureExpired [as 别名]
def validate_registration_token(token):
try:
return signing.loads(
TimestampSigner().unsign(
token, max_age=settings.REGISTRATION_CONFIRMATION_TIMEOUT
)
)["email"]
except (BadSignature, SignatureExpired):
return None
示例11: validate_student_confirmation_token
# 需要导入模块: from django.core import signing [as 别名]
# 或者: from django.core.signing import SignatureExpired [as 别名]
def validate_student_confirmation_token(token):
try:
return signing.loads(
TimestampSigner().unsign(
token, max_age=settings.STUDENT_CONFIRMATION_TIMEOUT
)
)
except (BadSignature, SignatureExpired):
return None
示例12: unsubscribe_reports
# 需要导入模块: from django.core import signing [as 别名]
# 或者: from django.core.signing import SignatureExpired [as 别名]
def unsubscribe_reports(request, signed_username):
# Some email servers open links in emails to check for malicious content.
# To work around this, for GET requests we serve a confirmation form.
# If the signature is more than 5 minutes old, we also include JS code to
# auto-submit the form.
ctx = {}
signer = signing.TimestampSigner(salt="reports")
# First, check the signature without looking at the timestamp:
try:
username = signer.unsign(signed_username)
except signing.BadSignature:
return render(request, "bad_link.html")
# Check if timestamp is older than 5 minutes:
try:
username = signer.unsign(signed_username, max_age=300)
except signing.SignatureExpired:
ctx["autosubmit"] = True
if request.method != "POST":
return render(request, "accounts/unsubscribe_submit.html", ctx)
user = User.objects.get(username=username)
profile = Profile.objects.for_user(user)
profile.reports_allowed = False
profile.next_report_date = None
profile.nag_period = td()
profile.next_nag_date = None
profile.save()
return render(request, "accounts/unsubscribed.html")
示例13: unsubscribe_email
# 需要导入模块: from django.core import signing [as 别名]
# 或者: from django.core.signing import SignatureExpired [as 别名]
def unsubscribe_email(request, code, signed_token):
# Some email servers open links in emails to check for malicious content.
# To work around this, on GET requests we serve a confirmation form.
# If the signature is at least 5 minutes old, we also include JS code to
# auto-submit the form.
ctx = {}
if ":" in signed_token:
signer = signing.TimestampSigner(salt="alerts")
# First, check the signature without looking at the timestamp:
try:
token = signer.unsign(signed_token)
except signing.BadSignature:
return render(request, "bad_link.html")
# Check if timestamp is older than 5 minutes:
try:
signer.unsign(signed_token, max_age=300)
except signing.SignatureExpired:
ctx["autosubmit"] = True
else:
token = signed_token
channel = get_object_or_404(Channel, code=code, kind="email")
if channel.make_token() != token:
return render(request, "bad_link.html")
if request.method != "POST":
return render(request, "accounts/unsubscribe_submit.html", ctx)
channel.delete()
return render(request, "front/unsubscribe_success.html")
示例14: test_max_age_argument
# 需要导入模块: from django.core import signing [as 别名]
# 或者: from django.core.signing import SignatureExpired [as 别名]
def test_max_age_argument(self):
value = 'hello'
with freeze_time(123456789):
response = HttpResponse()
response.set_signed_cookie('c', value)
request = HttpRequest()
request.COOKIES['c'] = response.cookies['c'].value
self.assertEqual(request.get_signed_cookie('c'), value)
with freeze_time(123456800):
self.assertEqual(request.get_signed_cookie('c', max_age=12), value)
self.assertEqual(request.get_signed_cookie('c', max_age=11), value)
with self.assertRaises(signing.SignatureExpired):
request.get_signed_cookie('c', max_age=10)
示例15: test_timestamp_signer
# 需要导入模块: from django.core import signing [as 别名]
# 或者: from django.core.signing import SignatureExpired [as 别名]
def test_timestamp_signer(self):
value = 'hello'
with freeze_time(123456789):
signer = signing.TimestampSigner('predictable-key')
ts = signer.sign(value)
self.assertNotEqual(ts, signing.Signer('predictable-key').sign(value))
self.assertEqual(signer.unsign(ts), value)
with freeze_time(123456800):
self.assertEqual(signer.unsign(ts, max_age=12), value)
# max_age parameter can also accept a datetime.timedelta object
self.assertEqual(signer.unsign(ts, max_age=datetime.timedelta(seconds=11)), value)
with self.assertRaises(signing.SignatureExpired):
signer.unsign(ts, max_age=10)