def create_event_producer(producer_url): if frappe.db.exists("Event Producer", producer_url): event_producer = frappe.get_doc("Event Producer", producer_url) for entry in event_producer.producer_doctypes: entry.unsubscribe = 0 event_producer.save() return generate_keys("Administrator") producer_site = connect() response = producer_site.post_api( "frappe.core.doctype.user.user.generate_keys", params={"user": "******"} ) api_secret = response.get("api_secret") response = producer_site.get_value("User", "api_key", {"name": "Administrator"}) api_key = response.get("api_key") event_producer = frappe.new_doc("Event Producer") event_producer.producer_doctypes = [] event_producer.producer_url = producer_url event_producer.append("producer_doctypes", {"ref_doctype": "ToDo", "use_same_name": 1}) event_producer.append("producer_doctypes", {"ref_doctype": "Note", "use_same_name": 1}) event_producer.user = "******" event_producer.api_key = api_key event_producer.api_secret = api_secret event_producer.save()
def _add_user_api_key(username): from frappe.core.doctype.user.user import generate_keys if frappe.db.exists("User", {"username": username}): generate_keys(frappe.get_value('User', {'username': username}, 'name')) frappe.db.commit() print("API key generated for user {}".format(username)) return 0
def test_auth_via_api_key_secret(self): # generate API key and API secret for administrator keys = generate_keys("Administrator") frappe.db.commit() generated_secret = frappe.utils.password.get_decrypted_password( "User", "Administrator", fieldname='api_secret' ) api_key = frappe.db.get_value("User", "Administrator", "api_key") header = {"Authorization": "token {}:{}".format(api_key, generated_secret)} res = requests.post(get_url() + "/api/method/frappe.auth.get_logged_user", headers=header) self.assertEqual(res.status_code, 200) self.assertEqual("Administrator", res.json()["message"]) self.assertEqual(keys['api_secret'], generated_secret) header = {"Authorization": "Basic {}".format(base64.b64encode(frappe.safe_encode("{}:{}".format(api_key, generated_secret))).decode())} res = requests.post(get_url() + "/api/method/frappe.auth.get_logged_user", headers=header) self.assertEqual(res.status_code, 200) self.assertEqual("Administrator", res.json()["message"]) # Valid api key, invalid api secret api_secret = "ksk&93nxoe3os" header = {"Authorization": "token {}:{}".format(api_key, api_secret)} res = requests.post(get_url() + "/api/method/frappe.auth.get_logged_user", headers=header) self.assertEqual(res.status_code, 403) # random api key and api secret api_key = "@3djdk3kld" api_secret = "ksk&93nxoe3os" header = {"Authorization": "token {}:{}".format(api_key, api_secret)} res = requests.post(get_url() + "/api/method/frappe.auth.get_logged_user", headers=header) self.assertEqual(res.status_code, 401)
def create_event_producer(producer_url): if frappe.db.exists('Event Producer', producer_url): event_producer = frappe.get_doc('Event Producer', producer_url) for entry in event_producer.producer_doctypes: entry.unsubscribe = 0 event_producer.save() return generate_keys('Administrator') producer_site = connect() response = producer_site.post_api( 'frappe.core.doctype.user.user.generate_keys', params={'user': '******'} ) api_secret = response.get('api_secret') response = producer_site.get_value('User', 'api_key', {'name': 'Administrator'}) api_key = response.get('api_key') event_producer = frappe.new_doc('Event Producer') event_producer.producer_doctypes = [] event_producer.producer_url = producer_url event_producer.append('producer_doctypes', { 'ref_doctype': 'ToDo', 'use_same_name': 1 }) event_producer.append('producer_doctypes', { 'ref_doctype': 'Note', 'use_same_name': 1 }) event_producer.user = '******' event_producer.api_key = api_key event_producer.api_secret = api_secret event_producer.save()
def verify_otp(otp_auth_attempt_name, incoming_otp, action): settings = frappe.get_single('CD User Management Settings') frappe.set_user(settings.default_user) otp_auth_attempt_doc = frappe.get_doc("CD OTP Auth Attempt", { "name": otp_auth_attempt_name, "login_status": "OTP Generated" }) verification_time = now_datetime().__str__()[:-7] otp_expiry_limit = settings.otp_expiry_limit_in_mins max_otp_attempts = settings.max_otp_attempts if not (time_diff(verification_time, otp_auth_attempt_doc.generated_time).total_seconds() / 60) <= otp_expiry_limit: otp_auth_attempt_doc.login_status = 'Expired' otp_auth_attempt_doc.save() frappe.local.response.http_status_code = 429 frappe.local.response["message"] = "OTP Expired" if otp_auth_attempt_doc.generated_otp == incoming_otp: otp_auth_attempt_doc.login_status = 'Success' otp_auth_attempt_doc.verify_action = action.replace('_', ' ').title() otp_auth_attempt_doc.save() user = frappe.get_doc( "User", {'mobile_no': otp_auth_attempt_doc.mobile_number}) if action == 'get_reset_password_key': user.reset_password() frappe.local.response[ "reset_password_key"] = user.reset_password_key if action == 'get_api_credentials': frappe.local.response["api_key"] = user.api_key frappe.local.response["api_secret"] = generate_keys( user.name)['api_secret'] else: if len(otp_auth_attempt_doc.failed_attempts) == max_otp_attempts: otp_auth_attempt_doc.login_status = 'Blocked' otp_auth_attempt_doc.save() frappe.local.response.http_status_code = 429 frappe.local.response["message"] = "Maximum Limit Reached" else: otp_auth_attempt_doc.append('failed_attempts', {'failed_incoming_otp': incoming_otp}) otp_auth_attempt_doc.save() frappe.local.response.http_status_code = 401 frappe.local.response["message"] = "Incorrect OTP"
def before_save(self): # creating a random email-id for customer by using first name and last name email = self.customer.lower() + "_" + self.last_name.lower( ) + "@" + "gmail.com" if not self.user_id: user_doc = frappe.get_doc( { # a user is created is created for that customer using the email 'doctype': 'User', 'email': email, 'first_name': self.customer, 'send_welcome_email': 0, 'role_profile_name': 'FirstU Customer' }) user_doc.insert() # generate user_id,api_secret and api key for user self.user_id = user_doc.email self.api_secret = generate_keys(self.user_id)['api_secret'] self.api_key = frappe.db.get_value('User', self.user_id, 'api_key') frappe.db.commit() self.owner = self.user_id
def before_save(self): if not self.user_id: rand_no = random.randrange(10000, 99999, 1) rand_string = ''.join(random.choices(string.ascii_lowercase, k=10)) service_provider = ['gmail', 'ymail', 'yahoo', 'hotmail'] provider = random.choices(service_provider)[0] rand_email = rand_string + str(rand_no) + '@' + provider + '.com' email = rand_email user_doc = frappe.get_doc({ 'doctype': 'User', 'email': email, 'first_name': rand_string, 'send_welcome_email': 0, 'role_profile_name': 'FirstU Customer' }) user_doc.insert() self.user_id = user_doc.email self.api_secret = generate_keys(self.user_id)['api_secret'] self.api_key = frappe.db.get_value('User', self.user_id, 'api_key') frappe.db.commit() self.owner = self.user_id
def setUp(self): # generate keys for current user to send requests for the following tests generate_keys(frappe.session.user) frappe.db.commit() generated_secret = frappe.utils.password.get_decrypted_password( "User", frappe.session.user, fieldname='api_secret') api_key = frappe.db.get_value("User", "Administrator", "api_key") self.header = { "Authorization": "token {}:{}".format(api_key, generated_secret) } self.test_html_template = """ <!DOCTYPE html> <html> <head> <style> table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; } td, th { border: 1px solid #dddddd; text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #dddddd; } </style> </head> <body> <h2>HTML Table</h2> <table> <tr> <th>Company</th> <th>Contact</th> <th>Country</th> </tr> <tr> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Germany</td> </tr> <tr> <td>Centro comercial Moctezuma</td> <td>Francisco Chang</td> <td>Mexico</td> </tr> <tr> <td>Ernst Handel</td> <td>Roland Mendel</td> <td>Austria</td> </tr> <tr> <td>Island Trading</td> <td>Helen Bennett</td> <td>UK</td> </tr> <tr> <td>Laughing Bacchus Winecellars</td> <td>Yoshi Tannamuri</td> <td>Canada</td> </tr> <tr> <td>Magazzini Alimentari Riuniti</td> <td>Giovanni Rovelli</td> <td>Italy</td> </tr> </table> </body> </html> """ self.test_filters = { "from_date": "2019-06-30", "to_date": "2019-07-31", "party": [], "group_by": "Group by Voucher (Consolidated)", "cost_center": [], "project": [] } self.test_doctype = 'File' self.test_document = 'Test Document' self.test_report_name = 'General Ledger' self.test_file_type = 'CSV' self.test_method = 'Test Method' self.file_name = frappe.utils.random_string(10) + '.txt' self.test_content = frappe.utils.random_string(1024)