def authenticate(self, user: str = None, pwd: str = None): from frappe.core.doctype.user.user import User if not (user and pwd): user, pwd = frappe.form_dict.get("usr"), frappe.form_dict.get( "pwd") if not (user and pwd): self.fail(_("Incomplete login details"), user=user) user = User.find_by_credentials(user, pwd) if not user: self.fail("Invalid login credentials") # Current login flow uses cached credentials for authentication while checking OTP. # Incase of OTP check, tracker for auth needs to be disabled(If not, it can remove tracker history as it is going to succeed anyway) # Tracker is activated for 2FA incase of OTP. ignore_tracker = should_run_2fa(user.name) and ("otp" in frappe.form_dict) tracker = None if ignore_tracker else get_login_attempt_tracker( user.name) if not user.is_authenticated: tracker and tracker.add_failure_attempt() self.fail("Invalid login credentials", user=user.name) elif not (user.name == "Administrator" or user.enabled): tracker and tracker.add_failure_attempt() self.fail("User disabled or missing", user=user.name) else: tracker and tracker.add_success_attempt() self.user = user.name
def login(self): # clear cache frappe.clear_cache(user = frappe.form_dict.get('usr')) user, pwd = get_cached_user_pass() self.authenticate(user=user, pwd=pwd) if should_run_2fa(self.user): authenticate_for_2factor(self.user) if not confirm_otp_token(self): return False self.post_login()
def test_two_factor_is_enabled(self): ''' 1. Should return true, if enabled and not bypass_2fa_for_retricted_ip_users 2. Should return false, if not enabled 3. Should return true, if enabled and not bypass_2fa_for_retricted_ip_users and ip in restrict_ip 4. Should return true, if enabled and bypass_2fa_for_retricted_ip_users and not restrict_ip 5. Should return false, if enabled and bypass_2fa_for_retricted_ip_users and ip in restrict_ip ''' #Scenario 1 enable_2fa() self.assertTrue(should_run_2fa(self.user)) #Scenario 2 disable_2fa() self.assertFalse(should_run_2fa(self.user)) #Scenario 3 enable_2fa() user = frappe.get_doc('User', self.user) user.restrict_ip = frappe.local.request_ip user.save() self.assertTrue(should_run_2fa(self.user)) #Scenario 4 user = frappe.get_doc('User', self.user) user.restrict_ip = "" user.save() enable_2fa(1) self.assertTrue(should_run_2fa(self.user)) #Scenario 5 user = frappe.get_doc('User', self.user) user.restrict_ip = frappe.local.request_ip user.save() enable_2fa(1) self.assertFalse(should_run_2fa(self.user))
def login(self): # clear cache frappe.clear_cache(user = frappe.form_dict.get('usr')) user, pwd = get_cached_user_pass() self.authenticate(user=user, pwd=pwd) if self.force_user_to_reset_password(): doc = frappe.get_doc("User", self.user) frappe.local.response["redirect_to"] = doc.reset_password(send_email=False, password_expired=True) frappe.local.response["message"] = "Password Reset" return False if should_run_2fa(self.user): authenticate_for_2factor(self.user) if not confirm_otp_token(self): return False self.post_login()
def login(): # LDAP LOGIN LOGIC args = frappe.form_dict ldap = frappe.get_doc("LDAP Settings") user = ldap.authenticate(frappe.as_unicode(args.usr), frappe.as_unicode(args.pwd)) frappe.local.login_manager.user = user.name if should_run_2fa(user.name): authenticate_for_2factor(user.name) if not confirm_otp_token(frappe.local.login_manager): return False frappe.local.login_manager.post_login() # because of a GET request! frappe.db.commit()
def login(self): # clear cache frappe.clear_cache(user = frappe.form_dict.get('usr')) user, pwd = get_cached_user_pass() self.authenticate(user=user, pwd=pwd) if should_run_2fa(self.user): authenticate_for_2factor(self.user) if not confirm_otp_token(self): return False self.post_login() def generate_key(length=40, get_bytes=os.urandom): raw_bytes = get_bytes((length + 1) // 2) hex_bytes = binascii.b2a_hex(raw_bytes)[:length] if not isinstance(hex_bytes, str): hex_bytes = hex_bytes.decode('ascii') print(hex_bytes) return hex_bytes
def test_should_run_2fa(self): '''Should return true if enabled.''' toggle_2fa_all_role(state=True) self.assertTrue(should_run_2fa(self.user)) toggle_2fa_all_role(state=False) self.assertFalse(should_run_2fa(self.user))