Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Except and raise statement python 3 compatible style (#3216) * changes exception and raise statements to python 3 style * changes except statement to python 3 style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * changes except and raise statement to python 3 compatible style * adds six.reraise to fix python 3 style raise statements with traceback * fixes indentation
pirms 8 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import frappe
  5. from frappe import _
  6. from frappe.utils import cstr, encode
  7. from cryptography.fernet import Fernet
  8. def get_decrypted_password(doctype, name, fieldname='password', raise_exception=True):
  9. auth = frappe.db.sql('''select `password` from `__Auth`
  10. where doctype=%(doctype)s and name=%(name)s and fieldname=%(fieldname)s and encrypted=1''',
  11. { 'doctype': doctype, 'name': name, 'fieldname': fieldname })
  12. if auth and auth[0][0]:
  13. return decrypt(auth[0][0])
  14. elif raise_exception:
  15. frappe.throw(_('Password not found'), frappe.AuthenticationError)
  16. def set_encrypted_password(doctype, name, pwd, fieldname='password'):
  17. frappe.db.sql("""insert into __Auth (doctype, name, fieldname, `password`, encrypted)
  18. values (%(doctype)s, %(name)s, %(fieldname)s, %(pwd)s, 1)
  19. on duplicate key update `password`=%(pwd)s, encrypted=1""",
  20. { 'doctype': doctype, 'name': name, 'fieldname': fieldname, 'pwd': encrypt(pwd) })
  21. def check_password(user, pwd, doctype='User', fieldname='password'):
  22. '''Checks if user and password are correct, else raises frappe.AuthenticationError'''
  23. auth = frappe.db.sql("""select name, `password`, salt from `__Auth`
  24. where doctype=%(doctype)s and name=%(name)s and fieldname=%(fieldname)s and encrypted=0
  25. and (
  26. (salt is null and `password`=password(%(pwd)s))
  27. or `password`=password(concat(%(pwd)s, salt))
  28. )""",{ 'doctype': doctype, 'name': user, 'fieldname': fieldname, 'pwd': pwd }, as_dict=True)
  29. if not auth:
  30. raise frappe.AuthenticationError('Incorrect User or Password')
  31. salt = auth[0].salt
  32. if not salt:
  33. # sets salt and updates password
  34. update_password(user, pwd, doctype, fieldname)
  35. # lettercase agnostic
  36. user = auth[0].name
  37. return user
  38. def update_password(user, pwd, doctype='User', fieldname='password'):
  39. salt = frappe.generate_hash()
  40. frappe.db.sql("""insert into __Auth (doctype, name, fieldname, `password`, salt, encrypted)
  41. values (%(doctype)s, %(name)s, %(fieldname)s, password(concat(%(pwd)s, %(salt)s)), %(salt)s, 0)
  42. on duplicate key update
  43. `password`=password(concat(%(pwd)s, %(salt)s)), salt=%(salt)s, encrypted=0""",
  44. { 'doctype': doctype, 'name': user, 'fieldname': fieldname, 'pwd': pwd, 'salt': salt })
  45. def delete_all_passwords_for(doctype, name):
  46. try:
  47. frappe.db.sql("""delete from __Auth where doctype=%(doctype)s and name=%(name)s""",
  48. { 'doctype': doctype, 'name': name })
  49. except Exception as e:
  50. if e.args[0]!=1054:
  51. raise
  52. def rename_password(doctype, old_name, new_name):
  53. # NOTE: fieldname is not considered, since the document is renamed
  54. frappe.db.sql("""update __Auth set name=%(new_name)s
  55. where doctype=%(doctype)s and name=%(old_name)s""",
  56. { 'doctype': doctype, 'new_name': new_name, 'old_name': old_name })
  57. def rename_password_field(doctype, old_fieldname, new_fieldname):
  58. frappe.db.sql('''update `__Auth` set fieldname=%(new_fieldname)s
  59. where doctype=%(doctype)s and fieldname=%(old_fieldname)s''',
  60. { 'doctype': doctype, 'old_fieldname': old_fieldname, 'new_fieldname': new_fieldname })
  61. def create_auth_table():
  62. # same as Framework.sql
  63. frappe.db.sql_ddl("""create table if not exists __Auth (
  64. `doctype` VARCHAR(140) NOT NULL,
  65. `name` VARCHAR(255) NOT NULL,
  66. `fieldname` VARCHAR(140) NOT NULL,
  67. `password` VARCHAR(255) NOT NULL,
  68. `salt` VARCHAR(140),
  69. `encrypted` INT(1) NOT NULL DEFAULT 0,
  70. PRIMARY KEY (`doctype`, `name`, `fieldname`)
  71. ) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci""")
  72. def encrypt(pwd):
  73. if len(pwd) > 100:
  74. # encrypting > 100 chars will lead to truncation
  75. frappe.throw(_('Password cannot be more than 100 characters long'))
  76. cipher_suite = Fernet(encode(get_encryption_key()))
  77. cipher_text = cstr(cipher_suite.encrypt(encode(pwd)))
  78. return cipher_text
  79. def decrypt(pwd):
  80. cipher_suite = Fernet(encode(get_encryption_key()))
  81. plain_text = cstr(cipher_suite.decrypt(encode(pwd)))
  82. return plain_text
  83. def get_encryption_key():
  84. from frappe.installer import update_site_config
  85. if 'encryption_key' not in frappe.local.conf:
  86. encryption_key = Fernet.generate_key()
  87. update_site_config('encryption_key', encryption_key)
  88. frappe.local.conf.encryption_key = encryption_key
  89. return frappe.local.conf.encryption_key