本文整理汇总了Python中django.contrib.auth.tokens.PasswordResetTokenGenerator.make_token方法的典型用法代码示例。如果您正苦于以下问题:Python PasswordResetTokenGenerator.make_token方法的具体用法?Python PasswordResetTokenGenerator.make_token怎么用?Python PasswordResetTokenGenerator.make_token使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.auth.tokens.PasswordResetTokenGenerator
的用法示例。
在下文中一共展示了PasswordResetTokenGenerator.make_token方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_10265
# 需要导入模块: from django.contrib.auth.tokens import PasswordResetTokenGenerator [as 别名]
# 或者: from django.contrib.auth.tokens.PasswordResetTokenGenerator import make_token [as 别名]
def test_10265(self):
"""
The token generated for a user created in the same request
will work correctly.
"""
# See ticket #10265
user = User.objects.create_user('comebackkid', '[email protected]', 'testpw')
p0 = PasswordResetTokenGenerator()
tk1 = p0.make_token(user)
reload = User.objects.get(username='comebackkid')
tk2 = p0.make_token(reload)
self.assertEqual(tk1, tk2)
示例2: send_token_message
# 需要导入模块: from django.contrib.auth.tokens import PasswordResetTokenGenerator [as 别名]
# 或者: from django.contrib.auth.tokens.PasswordResetTokenGenerator import make_token [as 别名]
def send_token_message(host, user, template, subject, new_user=False, extra_context=None):
"""
Send an email to the the user with a new token
"""
token_generator = PasswordResetTokenGenerator()
t = loader.get_template(template)
c = {
'email': user.email,
'host': host,
'user_token': int_to_base36(user.id),
'user': user,
'key_token': token_generator.make_token(user),
'new_user' : new_user,
}
if extra_context:
# If we have extra_content we need for the template
for key in extra_context:
c[key] = extra_context[key]
# send the user an email with the template filled out
# the actual link can be created using:
# {% url signup_login_by_email user_token key_token %}
send_mail(subject, t.render(Context(c)), settings.EMAIL_HOST_USER, [user.email])
示例3: create_or_login
# 需要导入模块: from django.contrib.auth.tokens import PasswordResetTokenGenerator [as 别名]
# 或者: from django.contrib.auth.tokens.PasswordResetTokenGenerator import make_token [as 别名]
def create_or_login(request):
if not request.method == "POST":
return HttpResponseRedirect(reverse(list))
form = EmailForm(request.POST)
if not form.is_valid():
return render(request, "members/login.html", {"form": form})
data = form.cleaned_data
member = get_object_or_None(Member, email=data["email"])
if not member:
member = Member(email=data["email"])
member.save()
member_url = reverse(edit, kwargs={"key": member.key})
token_gen = PasswordResetTokenGenerator()
token = token_gen.make_token(member)
try:
send_mail(
"Your Nick Reid Directory Profile",
"Follow this link to update your profile: %s?token=%s" % (member_url, token),
"[email protected]",
[member.email],
fail_silently=False,
)
except:
messages.error(request, "There was an error sending email to %s", member.email)
messages.success(request, "An email has been sent to %s." % (member.email))
return HttpResponseRedirect(reverse(list))
示例4: test_reset_password
# 需要导入模块: from django.contrib.auth.tokens import PasswordResetTokenGenerator [as 别名]
# 或者: from django.contrib.auth.tokens.PasswordResetTokenGenerator import make_token [as 别名]
def test_reset_password(self):
response = self.client.get(reverse('auth_password_reset'))
self.assertEquals(response.status_code, 200)
self.assertIn('Reset your password', response.content)
response = self.client.post(reverse('auth_password_reset'),
{'email': self.user.email}, follow=True)
self.assertEquals(response.status_code, 200)
self.assertIn('email with a link to reset your password', response.content)
pt = PasswordResetTokenGenerator()
token = pt.make_token(self.user)
uid = base64.b64encode(str(self.user.id)).strip('=')
response = self.client.get(reverse('auth_password_reset_confirm',
args=(uid, token)),
follow=True)
self.assertEquals(response.status_code, 200)
self.assertIn('Enter your new password below', response.content)
data = {
'new_password1': 'newpassword',
'new_password2': 'newpassword'
}
response = self.client.post(reverse('auth_password_reset_confirm', args=(uid, token)),
data, follow=True)
self.assertEquals(response.status_code, 200)
self.assertIn('Your password has been reset!', response.content)
result = self.client.login(username=self.user.username, password='newpassword')
self.assertTrue(result)
#reset password
self.user.set_password('supersecret')
示例5: generate_password_reset_url
# 需要导入模块: from django.contrib.auth.tokens import PasswordResetTokenGenerator [as 别名]
# 或者: from django.contrib.auth.tokens.PasswordResetTokenGenerator import make_token [as 别名]
def generate_password_reset_url(user_profile: UserProfile,
token_generator: PasswordResetTokenGenerator) -> str:
token = token_generator.make_token(user_profile)
uid = urlsafe_base64_encode(force_bytes(user_profile.id)).decode('ascii')
endpoint = reverse('django.contrib.auth.views.password_reset_confirm',
kwargs=dict(uidb64=uid, token=token))
return "{}{}".format(user_profile.realm.uri, endpoint)
示例6: test_PasswordResetConfirmView_valid_token
# 需要导入模块: from django.contrib.auth.tokens import PasswordResetTokenGenerator [as 别名]
# 或者: from django.contrib.auth.tokens.PasswordResetTokenGenerator import make_token [as 别名]
def test_PasswordResetConfirmView_valid_token(self):
# PasswordResetConfirmView valid token
default_token_generator = PasswordResetTokenGenerator()
token = default_token_generator.make_token(self.user)
uidb64 = force_text(urlsafe_base64_encode(force_bytes(self.user.pk)))
response = PasswordResetConfirmView.as_view(success_url='dummy/')(self.request, uidb64=uidb64, token=token)
self.assertContains(response, '<title>Enter new password</title>')
self.assertContains(response, '<h1>Enter new password</h1>')
示例7: _get_confirm_url
# 需要导入模块: from django.contrib.auth.tokens import PasswordResetTokenGenerator [as 别名]
# 或者: from django.contrib.auth.tokens.PasswordResetTokenGenerator import make_token [as 别名]
def _get_confirm_url(self, user):
token_maker = PasswordResetTokenGenerator()
token = token_maker.make_token(user)
uid = urlsafe_base64_encode(force_bytes(user.pk)) #same thing django does to generate uid
confirm_url = reverse('auth_password_reset_confirm',
kwargs={'token':token,
'uidb64':uid})
return confirm_url
示例8: test_make_token
# 需要导入模块: from django.contrib.auth.tokens import PasswordResetTokenGenerator [as 别名]
# 或者: from django.contrib.auth.tokens.PasswordResetTokenGenerator import make_token [as 别名]
def test_make_token(self):
"""
Ensure that we can make a token and that it is valid
"""
user = User.objects.create_user('tokentestuser', '[email protected]', 'testpw')
p0 = PasswordResetTokenGenerator()
tk1 = p0.make_token(user)
self.assertTrue(p0.check_token(user, tk1))
示例9: form_valid
# 需要导入模块: from django.contrib.auth.tokens import PasswordResetTokenGenerator [as 别名]
# 或者: from django.contrib.auth.tokens.PasswordResetTokenGenerator import make_token [as 别名]
def form_valid(self, form):
"""
Register a new user.
"""
# Do not accept any valid form when registration is closed.
if not settings.REGISTRATION_POSSIBLE:
messages.error(self.request, _('I\'m sorry, but I can\'t let anyone register at the moment.'))
return redirect(reverse_lazy('login'))
# Create and save user
user = LilyUser.objects.create_user(
email=form.cleaned_data['email'],
password=form.cleaned_data['password'],
first_name=form.cleaned_data['first_name'],
preposition=form.cleaned_data['preposition'],
last_name=form.cleaned_data['last_name'],
)
user.is_active = False
user.save()
# Add to admin group
account_admin = Group.objects.get_or_create(name='account_admin')[0]
user.groups.add(account_admin)
# Get the current site
try:
current_site = Site.objects.get_current()
except Site.DoesNotExist:
current_site = ''
# Generate uidb36 and token for the activation link
uidb36 = int_to_base36(user.pk)
token_generator = PasswordResetTokenGenerator()
token = token_generator.make_token(user)
# Send an activation mail
# TODO: only create/save contact when e-mail sent successfully
send_templated_mail(
template_name='activation',
from_email=settings.DEFAULT_FROM_EMAIL,
recipient_list=[form.cleaned_data['email']],
context={
'current_site': current_site,
'protocol': self.request.is_secure() and 'https' or 'http',
'user': user,
'uidb36': uidb36,
'token': token,
}
)
# Show registration message
messages.success(
self.request,
_('Registration completed. I\'ve sent you an email, please check it to activate your account.')
)
return self.get_success_url()
示例10: get_token
# 需要导入模块: from django.contrib.auth.tokens import PasswordResetTokenGenerator [as 别名]
# 或者: from django.contrib.auth.tokens.PasswordResetTokenGenerator import make_token [as 别名]
def get_token(user):
#assert request.user.is_authenticated()
token_generator = PasswordResetTokenGenerator()
token = token_generator.make_token(user)
return token
示例11: test_PasswordResetConfirmView_valid_token
# 需要导入模块: from django.contrib.auth.tokens import PasswordResetTokenGenerator [as 别名]
# 或者: from django.contrib.auth.tokens.PasswordResetTokenGenerator import make_token [as 别名]
def test_PasswordResetConfirmView_valid_token(self):
# PasswordResetConfirmView valid token
client = PasswordResetConfirmClient()
default_token_generator = PasswordResetTokenGenerator()
token = default_token_generator.make_token(self.user)
uidb64 = force_text(urlsafe_base64_encode(force_bytes(self.user.pk)))
url = reverse('password_reset_confirm', kwargs={'uidb64': uidb64, 'token': token})
response = client.get(url)
self.assertContains(response, '<title>Enter new password</title>')
self.assertContains(response, '<h1>Enter new password</h1>')
示例12: ActivationResendView
# 需要导入模块: from django.contrib.auth.tokens import PasswordResetTokenGenerator [as 别名]
# 或者: from django.contrib.auth.tokens.PasswordResetTokenGenerator import make_token [as 别名]
class ActivationResendView(FormView):
"""
This view is used by an user to request a new activation e-mail.
"""
template_name = 'users/activation_resend_form.html'
form_class = ResendActivationForm
def form_valid(self, form):
"""
If ResendActivationForm passed the validation, generate new token and send an e-mail.
"""
self.TGen = PasswordResetTokenGenerator()
self.users = CustomUser.objects.filter(
contact__email_addresses__email_address__iexact=form.cleaned_data['email'],
contact__email_addresses__is_primary=True
)
# Get the current site or empty string
try:
self.current_site = Site.objects.get_current()
except Site.DoesNotExist:
self.current_site = ''
for user in self.users:
# Generate uidb36 and token for the activation link
self.uidb36 = int_to_base36(user.pk)
self.token = self.TGen.make_token(user)
# E-mail to the user
send_templated_mail(
template_name='activation',
from_email=settings.DEFAULT_FROM_EMAIL,
recipient_list=[form.cleaned_data['email']],
context={
'current_site': self.current_site,
'protocol': self.request.is_secure() and 'https' or 'http',
'full_name': " ".join([user.contact.first_name, user.contact.preposition, user.contact.last_name]),
'user': user,
'uidb36': self.uidb36,
'token': self.token,
}
)
# Show registration message
messages.success(self.request, _('Reactivation success. Check your <nobr>e-mail</nobr> to activate your account.'))
# Redirect to success url
return self.get_success_url()
def get_success_url(self):
"""
Redirect to the success url.
"""
return redirect(reverse_lazy('login'))
示例13: test_token_with_different_secret
# 需要导入模块: from django.contrib.auth.tokens import PasswordResetTokenGenerator [as 别名]
# 或者: from django.contrib.auth.tokens.PasswordResetTokenGenerator import make_token [as 别名]
def test_token_with_different_secret(self):
"""
A valid token can be created with a secret other than SECRET_KEY by
using the PasswordResetTokenGenerator.secret attribute.
"""
user = User.objects.create_user('tokentestuser', '[email protected]', 'testpw')
new_secret = 'abcdefghijkl'
# Create and check a token with a different secret.
p0 = PasswordResetTokenGenerator()
p0.secret = new_secret
tk0 = p0.make_token(user)
self.assertTrue(p0.check_token(user, tk0))
# Create and check a token with the default secret.
p1 = PasswordResetTokenGenerator()
self.assertEqual(p1.secret, settings.SECRET_KEY)
self.assertNotEqual(p1.secret, new_secret)
tk1 = p1.make_token(user)
# Tokens created with a different secret don't validate.
self.assertFalse(p0.check_token(user, tk1))
self.assertFalse(p1.check_token(user, tk0))
示例14: send_reset_password_email
# 需要导入模块: from django.contrib.auth.tokens import PasswordResetTokenGenerator [as 别名]
# 或者: from django.contrib.auth.tokens.PasswordResetTokenGenerator import make_token [as 别名]
def send_reset_password_email(user, request):
token_generator = PasswordResetTokenGenerator()
temp_key = token_generator.make_token(user)
# send the password reset email
path = reverse('account_reset_password_from_key',
kwargs=dict(uidb36=int_to_base36(user.id),
key=temp_key))
url = request.build_absolute_uri(path)
context = {'password_reset_url': url}
subject = 'Reset your password'
send_mail('account/email/forget_password.html', subject, user.email, context)
示例15: send_email_auth_token
# 需要导入模块: from django.contrib.auth.tokens import PasswordResetTokenGenerator [as 别名]
# 或者: from django.contrib.auth.tokens.PasswordResetTokenGenerator import make_token [as 别名]
def send_email_auth_token(request, user, new_user=False):
token_generator = PasswordResetTokenGenerator()
t = loader.get_template('signup/email_auth_form.html')
c = {
'email': user.email,
'host': request.get_host(),
'user_token': int_to_base36(user.id),
'user': user,
'key_token': token_generator.make_token(user),
'new_user' : new_user,
}
send_mail(_("New Login token for %s") % request.get_host(), t.render(Context(c)), settings.EMAIL_HOST_USER, [user.email])