You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

198 lines
6.7 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. _django10_schemes = [
  78. "django_salted_sha1", "django_salted_md5", "django_des_crypt",
  79. "hex_md5", "django_disabled",
  80. ]
  81. django10_context = LazyCryptContext(
  82. schemes=_django10_schemes,
  83. default="django_salted_sha1",
  84. deprecated=["hex_md5"],
  85. )
  86. _django14_schemes = ["django_pbkdf2_sha256", "django_pbkdf2_sha1",
  87. "django_bcrypt"] + _django10_schemes
  88. django14_context = LazyCryptContext(
  89. schemes=_django14_schemes,
  90. deprecated=_django10_schemes,
  91. )
  92. _django16_schemes = _django14_schemes[:]
  93. _django16_schemes.insert(1, "django_bcrypt_sha256")
  94. django16_context = LazyCryptContext(
  95. schemes=_django16_schemes,
  96. deprecated=_django10_schemes,
  97. )
  98. django110_context = LazyCryptContext(
  99. schemes=["django_pbkdf2_sha256", "django_pbkdf2_sha1",
  100. "django_argon2", "django_bcrypt", "django_bcrypt_sha256",
  101. "django_disabled"],
  102. )
  103. # this will always point to latest version
  104. django_context = django110_context
  105. #=============================================================================
  106. # ldap
  107. #=============================================================================
  108. std_ldap_schemes = ["ldap_salted_sha1", "ldap_salted_md5",
  109. "ldap_sha1", "ldap_md5",
  110. "ldap_plaintext" ]
  111. # create context with all std ldap schemes EXCEPT crypt
  112. ldap_nocrypt_context = LazyCryptContext(std_ldap_schemes)
  113. # create context with all possible std ldap + ldap crypt schemes
  114. def _iter_ldap_crypt_schemes():
  115. from passlib.utils import unix_crypt_schemes
  116. return ('ldap_' + name for name in unix_crypt_schemes)
  117. def _iter_ldap_schemes():
  118. """helper which iterates over supported std ldap schemes"""
  119. return chain(std_ldap_schemes, _iter_ldap_crypt_schemes())
  120. ldap_context = LazyCryptContext(_iter_ldap_schemes())
  121. ### create context with all std ldap schemes + crypt schemes for localhost
  122. ##def _iter_host_ldap_schemes():
  123. ## "helper which iterates over supported std ldap schemes"
  124. ## from passlib.handlers.ldap_digests import get_host_ldap_crypt_schemes
  125. ## return chain(std_ldap_schemes, get_host_ldap_crypt_schemes())
  126. ##ldap_host_context = LazyCryptContext(_iter_host_ldap_schemes())
  127. #=============================================================================
  128. # mysql
  129. #=============================================================================
  130. mysql3_context = LazyCryptContext(["mysql323"])
  131. mysql4_context = LazyCryptContext(["mysql41", "mysql323"], deprecated="mysql323")
  132. mysql_context = mysql4_context # tracks latest mysql version supported
  133. #=============================================================================
  134. # postgres
  135. #=============================================================================
  136. postgres_context = LazyCryptContext(["postgres_md5"])
  137. #=============================================================================
  138. # phpass & variants
  139. #=============================================================================
  140. def _create_phpass_policy(**kwds):
  141. """helper to choose default alg based on bcrypt availability"""
  142. kwds['default'] = 'bcrypt' if hash.bcrypt.has_backend() else 'phpass'
  143. return kwds
  144. phpass_context = LazyCryptContext(
  145. schemes=["bcrypt", "phpass", "bsdi_crypt"],
  146. onload=_create_phpass_policy,
  147. )
  148. phpbb3_context = LazyCryptContext(["phpass"], phpass__ident="H")
  149. # TODO: support the drupal phpass variants (see phpass homepage)
  150. #=============================================================================
  151. # roundup
  152. #=============================================================================
  153. _std_roundup_schemes = [ "ldap_hex_sha1", "ldap_hex_md5", "ldap_des_crypt", "roundup_plaintext" ]
  154. roundup10_context = LazyCryptContext(_std_roundup_schemes)
  155. # NOTE: 'roundup15' really applies to roundup 1.4.17+
  156. roundup_context = roundup15_context = LazyCryptContext(
  157. schemes=_std_roundup_schemes + [ "ldap_pbkdf2_sha1" ],
  158. deprecated=_std_roundup_schemes,
  159. default = "ldap_pbkdf2_sha1",
  160. ldap_pbkdf2_sha1__default_rounds = 10000,
  161. )
  162. #=============================================================================
  163. # eof
  164. #=============================================================================