25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

246 satır
7.9 KiB

  1. """passlib.apps"""
  2. #=============================================================================
  3. # imports
  4. #=============================================================================
  5. # core
  6. import logging; log = logging.getLogger(__name__)
  7. from itertools import chain
  8. # site
  9. # pkg
  10. from passlib import hash
  11. from passlib.context import LazyCryptContext
  12. from passlib.utils import sys_bits
  13. # local
  14. __all__ = [
  15. 'custom_app_context',
  16. 'django_context',
  17. 'ldap_context', 'ldap_nocrypt_context',
  18. 'mysql_context', 'mysql4_context', 'mysql3_context',
  19. 'phpass_context',
  20. 'phpbb3_context',
  21. 'postgres_context',
  22. ]
  23. #=============================================================================
  24. # master containing all identifiable hashes
  25. #=============================================================================
  26. def _load_master_config():
  27. from passlib.registry import list_crypt_handlers
  28. # get master list
  29. schemes = list_crypt_handlers()
  30. # exclude the ones we know have ambiguous or greedy identify() methods.
  31. excluded = [
  32. # frequently confused for eachother
  33. 'bigcrypt',
  34. 'crypt16',
  35. # no good identifiers
  36. 'cisco_pix',
  37. 'cisco_type7',
  38. 'htdigest',
  39. 'mysql323',
  40. 'oracle10',
  41. # all have same size
  42. 'lmhash',
  43. 'msdcc',
  44. 'msdcc2',
  45. 'nthash',
  46. # plaintext handlers
  47. 'plaintext',
  48. 'ldap_plaintext',
  49. # disabled handlers
  50. 'django_disabled',
  51. 'unix_disabled',
  52. 'unix_fallback',
  53. ]
  54. for name in excluded:
  55. schemes.remove(name)
  56. # return config
  57. return dict(schemes=schemes, default="sha256_crypt")
  58. master_context = LazyCryptContext(onload=_load_master_config)
  59. #=============================================================================
  60. # for quickly bootstrapping new custom applications
  61. #=============================================================================
  62. custom_app_context = LazyCryptContext(
  63. # choose some reasonbly strong schemes
  64. schemes=["sha512_crypt", "sha256_crypt"],
  65. # set some useful global options
  66. default="sha256_crypt" if sys_bits < 64 else "sha512_crypt",
  67. # set a good starting point for rounds selection
  68. sha512_crypt__min_rounds = 535000,
  69. sha256_crypt__min_rounds = 535000,
  70. # if the admin user category is selected, make a much stronger hash,
  71. admin__sha512_crypt__min_rounds = 1024000,
  72. admin__sha256_crypt__min_rounds = 1024000,
  73. )
  74. #=============================================================================
  75. # django
  76. #=============================================================================
  77. #-----------------------------------------------------------------------
  78. # 1.0
  79. #-----------------------------------------------------------------------
  80. _django10_schemes = [
  81. "django_salted_sha1",
  82. "django_salted_md5",
  83. "django_des_crypt",
  84. "hex_md5",
  85. "django_disabled",
  86. ]
  87. django10_context = LazyCryptContext(
  88. schemes=_django10_schemes,
  89. default="django_salted_sha1",
  90. deprecated=["hex_md5"],
  91. )
  92. #-----------------------------------------------------------------------
  93. # 1.4
  94. #-----------------------------------------------------------------------
  95. _django14_schemes = [
  96. "django_pbkdf2_sha256",
  97. "django_pbkdf2_sha1",
  98. "django_bcrypt"
  99. ] + _django10_schemes
  100. django14_context = LazyCryptContext(
  101. schemes=_django14_schemes,
  102. deprecated=_django10_schemes,
  103. )
  104. #-----------------------------------------------------------------------
  105. # 1.6
  106. #-----------------------------------------------------------------------
  107. _django16_schemes = list(_django14_schemes)
  108. _django16_schemes.insert(1, "django_bcrypt_sha256")
  109. django16_context = LazyCryptContext(
  110. schemes=_django16_schemes,
  111. deprecated=_django10_schemes,
  112. )
  113. #-----------------------------------------------------------------------
  114. # 1.10
  115. #-----------------------------------------------------------------------
  116. _django_110_schemes = [
  117. "django_pbkdf2_sha256",
  118. "django_pbkdf2_sha1",
  119. "django_argon2",
  120. "django_bcrypt",
  121. "django_bcrypt_sha256",
  122. "django_disabled",
  123. ]
  124. django110_context = LazyCryptContext(schemes=_django_110_schemes)
  125. #-----------------------------------------------------------------------
  126. # 2.1
  127. #-----------------------------------------------------------------------
  128. _django21_schemes = list(_django_110_schemes)
  129. _django21_schemes.remove("django_bcrypt")
  130. django21_context = LazyCryptContext(schemes=_django21_schemes)
  131. #-----------------------------------------------------------------------
  132. # latest
  133. #-----------------------------------------------------------------------
  134. # this will always point to latest version in passlib
  135. django_context = django21_context
  136. #=============================================================================
  137. # ldap
  138. #=============================================================================
  139. #: standard ldap schemes
  140. std_ldap_schemes = [
  141. "ldap_salted_sha512",
  142. "ldap_salted_sha256",
  143. "ldap_salted_sha1",
  144. "ldap_salted_md5",
  145. "ldap_sha1",
  146. "ldap_md5",
  147. "ldap_plaintext",
  148. ]
  149. # create context with all std ldap schemes EXCEPT crypt
  150. ldap_nocrypt_context = LazyCryptContext(std_ldap_schemes)
  151. # create context with all possible std ldap + ldap crypt schemes
  152. def _iter_ldap_crypt_schemes():
  153. from passlib.utils import unix_crypt_schemes
  154. return ('ldap_' + name for name in unix_crypt_schemes)
  155. def _iter_ldap_schemes():
  156. """helper which iterates over supported std ldap schemes"""
  157. return chain(std_ldap_schemes, _iter_ldap_crypt_schemes())
  158. ldap_context = LazyCryptContext(_iter_ldap_schemes())
  159. ### create context with all std ldap schemes + crypt schemes for localhost
  160. ##def _iter_host_ldap_schemes():
  161. ## "helper which iterates over supported std ldap schemes"
  162. ## from passlib.handlers.ldap_digests import get_host_ldap_crypt_schemes
  163. ## return chain(std_ldap_schemes, get_host_ldap_crypt_schemes())
  164. ##ldap_host_context = LazyCryptContext(_iter_host_ldap_schemes())
  165. #=============================================================================
  166. # mysql
  167. #=============================================================================
  168. mysql3_context = LazyCryptContext(["mysql323"])
  169. mysql4_context = LazyCryptContext(["mysql41", "mysql323"], deprecated="mysql323")
  170. mysql_context = mysql4_context # tracks latest mysql version supported
  171. #=============================================================================
  172. # postgres
  173. #=============================================================================
  174. postgres_context = LazyCryptContext(["postgres_md5"])
  175. #=============================================================================
  176. # phpass & variants
  177. #=============================================================================
  178. def _create_phpass_policy(**kwds):
  179. """helper to choose default alg based on bcrypt availability"""
  180. kwds['default'] = 'bcrypt' if hash.bcrypt.has_backend() else 'phpass'
  181. return kwds
  182. phpass_context = LazyCryptContext(
  183. schemes=["bcrypt", "phpass", "bsdi_crypt"],
  184. onload=_create_phpass_policy,
  185. )
  186. phpbb3_context = LazyCryptContext(["phpass"], phpass__ident="H")
  187. # TODO: support the drupal phpass variants (see phpass homepage)
  188. #=============================================================================
  189. # roundup
  190. #=============================================================================
  191. _std_roundup_schemes = [ "ldap_hex_sha1", "ldap_hex_md5", "ldap_des_crypt", "roundup_plaintext" ]
  192. roundup10_context = LazyCryptContext(_std_roundup_schemes)
  193. # NOTE: 'roundup15' really applies to roundup 1.4.17+
  194. roundup_context = roundup15_context = LazyCryptContext(
  195. schemes=_std_roundup_schemes + [ "ldap_pbkdf2_sha1" ],
  196. deprecated=_std_roundup_schemes,
  197. default = "ldap_pbkdf2_sha1",
  198. ldap_pbkdf2_sha1__default_rounds = 10000,
  199. )
  200. #=============================================================================
  201. # eof
  202. #=============================================================================