本文整理匯總了Python中django.contrib.auth.tokens.default_token_generator.check_token方法的典型用法代碼示例。如果您正苦於以下問題:Python default_token_generator.check_token方法的具體用法?Python default_token_generator.check_token怎麽用?Python default_token_generator.check_token使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.contrib.auth.tokens.default_token_generator
的用法示例。
在下文中一共展示了default_token_generator.check_token方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: clean
# 需要導入模塊: from django.contrib.auth.tokens import default_token_generator [as 別名]
# 或者: from django.contrib.auth.tokens.default_token_generator import check_token [as 別名]
def clean(self):
cleaned_data = super().clean()
token = cleaned_data["token"]
userid = urlsafe_b64decode(cleaned_data["encoded_user_id"])
user = User.objects.filter(id=userid).first()
if not user:
raise ValidationError({"encoded_user_id": _("Invalid user")})
if not default_token_generator.check_token(user, token):
raise ValidationError({"token": _("Invalid token")})
cleaned_data["user"] = user
return cleaned_data
示例2: validate
# 需要導入模塊: from django.contrib.auth.tokens import default_token_generator [as 別名]
# 或者: from django.contrib.auth.tokens.default_token_generator import check_token [as 別名]
def validate(self, attrs):
self._errors = {}
# Decode the uidb64 to uid to get User object
try:
uid = force_text(uid_decoder(attrs['uid']))
self.user = UserModel._default_manager.get(pk=uid)
except (TypeError, ValueError, OverflowError, UserModel.DoesNotExist):
raise ValidationError({'uid': ['Invalid value']})
self.custom_validation(attrs)
# Construct SetPasswordForm instance
self.set_password_form = self.set_password_form_class(
user=self.user, data=attrs
)
if not self.set_password_form.is_valid():
raise serializers.ValidationError(self.set_password_form.errors)
if not default_token_generator.check_token(self.user, attrs['token']):
raise ValidationError({'token': ['Invalid value']})
return attrs
示例3: initial
# 需要導入模塊: from django.contrib.auth.tokens import default_token_generator [as 別名]
# 或者: from django.contrib.auth.tokens.default_token_generator import check_token [as 別名]
def initial(self, request, *args, **kwargs):
uidb64 = kwargs['uidb64']
uid = urlsafe_base64_decode(force_text(uidb64))
try:
self.user = User.objects.get(pk=uid)
except User.DoesNotExist:
raise exceptions.InvalidExpiredToken()
token = kwargs['token']
if not default_token_generator.check_token(self.user, token):
raise exceptions.InvalidExpiredToken()
return super(OneTimeUseAPIMixin, self).initial(
request,
*args,
**kwargs
)
示例4: validate
# 需要導入模塊: from django.contrib.auth.tokens import default_token_generator [as 別名]
# 或者: from django.contrib.auth.tokens.default_token_generator import check_token [as 別名]
def validate(self, attrs):
user = get_user_from_uid(attrs.get('uid'))
value = attrs['token']
if not default_token_generator.check_token(user, value):
raise ValidationError(_("Invalid token: %s") % value)
return attrs
示例5: post
# 需要導入模塊: from django.contrib.auth.tokens import default_token_generator [as 別名]
# 或者: from django.contrib.auth.tokens.default_token_generator import check_token [as 別名]
def post(self, request, uidb64=None, token=None, *args, **kwargs):
userModel = get_user_model()
form = self.form_class(request.POST)
if uidb64 is None or token is None:
form.add_error(
field=None, error=u"O link usado para a troca de senha não é válido ou expirou, por favor tente enviar novamente.")
return self.form_invalid(form)
try:
uid = urlsafe_base64_decode(uidb64)
user = userModel._default_manager.get(pk=uid)
except (TypeError, ValueError, OverflowError, userModel.DoesNotExist):
user = None
if user is not None and default_token_generator.check_token(user, token):
if form.is_valid():
new_password = form.cleaned_data['new_password']
new_password_confirm = form.cleaned_data[
'new_password_confirm']
if new_password == new_password_confirm:
user.set_password(new_password)
user.save()
messages.success(request, u"Senha trocada com sucesso")
return self.form_valid(form)
else:
form.add_error(field=None, error=u"Senhas diferentes.")
return self.form_invalid(form)
else:
form.add_error(
field=None, error=u"Não foi possivel trocar a senha. Formulário inválido.")
return self.form_invalid(form)
else:
form.add_error(
field=None, error=u"O link usado para a troca de senha não é válido ou expirou, por favor tente enviar novamente.")
return self.form_invalid(form)
示例6: password_reset_confirm
# 需要導入模塊: from django.contrib.auth.tokens import default_token_generator [as 別名]
# 或者: from django.contrib.auth.tokens.default_token_generator import check_token [as 別名]
def password_reset_confirm(request, uidb64, token):
"""REST API reset password confirm"""
serializer = PasswordSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
uid = force_text(urlsafe_base64_decode(uidb64))
try:
user = User.objects.get(pk=uid)
except User.DoesNotExist:
user = None
if user is not None and default_token_generator.check_token(user, token):
user.set_password(serializer.validated_data.get('password'))
user.save()
return Response({}, status=status.HTTP_201_CREATED)
示例7: recupera_password_conferma
# 需要導入模塊: from django.contrib.auth.tokens import default_token_generator [as 別名]
# 或者: from django.contrib.auth.tokens.default_token_generator import check_token [as 別名]
def recupera_password_conferma(request, uidb64=None, token=None,
template='base_recupero_password_conferma.html',
contesto_extra=None):
assert uidb64 is not None and token is not None # checked by URLconf
try:
# urlsafe_base64_decode() decodes to bytestring on Python 3
uid = force_text(urlsafe_base64_decode(uidb64))
utente = Utenza.objects.get(pk=uid)
except (TypeError, ValueError, OverflowError, Utenza.DoesNotExist):
utente = None
if utente is not None and default_token_generator.check_token(utente, token):
link_valido = True
titolo = 'Inserisci una nuova password'
if request.method == 'POST':
modulo = ModuloImpostaPassword(utente, request.POST)
if modulo.is_valid():
modulo.save()
return HttpResponseRedirect(reverse('recupero_password_completo'))
else:
modulo = ModuloImpostaPassword(utente)
else:
link_valido = False
modulo = None
titolo = 'Errore nell\'impostazione della nuova password'
contesto = {
'modulo': modulo,
'titolo': titolo,
'link_valido': link_valido,
"scadenza_token": django_settings.PASSWORD_RESET_TIMEOUT_DAYS * 24
}
if contesto_extra is not None:
contesto.update(contesto_extra)
return TemplateResponse(request, template, contesto)