def set_password(self, raw_password=None, hashed_password=None, cached_password=None): """Set a password with the raw password string, or the pre-hashed password. If using the raw string, """ assert hashed_password is None or settings.DEBUG, "Only use hashed_password in debug mode." assert raw_password is not None or hashed_password is not None, "Must be passing in raw or hashed password" assert not (raw_password is not None and hashed_password is not None), "Must be specifying only one--not both." if raw_password: verify_raw_password(raw_password) if hashed_password: self.password = hashed_password # Can't save a cached password from a hash, so just make sure there is none. # Note: Need to do this, even if they're not enabled--we don't want to risk # being out of sync (if people turn on/off/on the feature CachedPassword.invalidate_cached_password(user=self) else: n_iters = settings.PASSWORD_ITERATIONS_TEACHER_SYNCED if self.is_teacher else settings.PASSWORD_ITERATIONS_STUDENT_SYNCED self.password = crypt(raw_password, iterations=n_iters) if self.id: CachedPassword.set_cached_password(self, raw_password)
def handle(self, *args, **options): if len(args) != 1: raise CommandError("need exactly one argument (username)") if args: username, = args try: u = FacilityUser.objects.using( options.get('database')).get(username=username) except FacilityUser.DoesNotExist: raise CommandError("user '%s' does not exist" % username) self.stdout.write("Changing password for user '%s'\n" % u.username) if options['noinput']: p1 = generate_random_password() self.stdout.write("Generated new password for user '%s': '%s'\n" % (username, p1)) else: MAX_TRIES = 3 count = 0 p1, p2 = 1, 2 # To make them initially mismatch. while p1 != p2 and count < MAX_TRIES: p1 = self._get_pass() try: verify_raw_password(p1) except ValidationError as e: self.stderr.write(str(e) + "\n") count += 1 continue p2 = self._get_pass("Password (again): ") if p1 != p2: self.stdout.write( "Passwords do not match. Please try again.\n") count = count + 1 if count == MAX_TRIES: raise CommandError( "Aborting password change for user '%s' after %s attempts" % (username, count)) u.set_password(p1) u.save() return "Password changed successfully for user '%s'\n" % u.username
def handle(self, *args, **options): if len(args) != 1: raise CommandError("need exactly one argument (username)") if args: username, = args try: u = FacilityUser.objects.using(options.get('database')).get(username=username) except FacilityUser.DoesNotExist: raise CommandError("user '%s' does not exist" % username) self.stdout.write("Changing password for user '%s'\n" % u.username) if options['noinput']: p1 = generate_random_password() self.stdout.write("Generated new password for user '%s': '%s'\n" % (username, p1)) else: MAX_TRIES = 3 count = 0 p1, p2 = 1, 2 # To make them initially mismatch. while p1 != p2 and count < MAX_TRIES: p1 = self._get_pass() try: verify_raw_password(p1) except ValidationError as e: self.stderr.write(unicode(e) + "\n") count += 1 continue p2 = self._get_pass("Password (again): ") if p1 != p2: self.stdout.write("Passwords do not match. Please try again.\n") count = count + 1 if count == MAX_TRIES: raise CommandError("Aborting password change for user '%s' after %s attempts" % (username, count)) u.set_password(p1) u.save() return "Password changed successfully for user '%s'\n" % u.username
def clean_password_first(self): password = self.cleaned_data.get('password_first', "") verify_raw_password(password) return password