utils.py 820 B

1234567891011121314151617181920212223242526
  1. from django.conf import settings
  2. from django.core.exceptions import ImproperlyConfigured
  3. from social_core.storage import NO_ASCII_REGEX, NO_SPECIAL_REGEX
  4. __all__ = (
  5. 'clean_username',
  6. 'get_current_pepper',
  7. )
  8. def clean_username(value):
  9. """Clean username removing any unsupported character"""
  10. value = NO_ASCII_REGEX.sub('', value)
  11. value = NO_SPECIAL_REGEX.sub('', value)
  12. value = value.replace(':', '')
  13. return value
  14. def get_current_pepper():
  15. """
  16. Return the ID and value of the newest (highest ID) cryptographic pepper.
  17. """
  18. if len(settings.API_TOKEN_PEPPERS) < 1:
  19. raise ImproperlyConfigured("Must define API_TOKEN_PEPPERS to use v2 API tokens")
  20. newest_id = sorted(settings.API_TOKEN_PEPPERS.keys())[-1]
  21. return newest_id, settings.API_TOKEN_PEPPERS[newest_id]