60 lines
1.5 KiB
Python
60 lines
1.5 KiB
Python
"""
|
|
Utility functions for third_party_auth
|
|
"""
|
|
|
|
from uuid import UUID
|
|
from django.contrib.auth.models import User
|
|
|
|
|
|
def user_exists(details):
|
|
"""
|
|
Return True if user with given details exist in the system.
|
|
|
|
Arguments:
|
|
details (dict): dictionary containing user infor like email, username etc.
|
|
|
|
Returns:
|
|
(bool): True if user with given details exists, `False` otherwise.
|
|
"""
|
|
user_queryset_filter = {}
|
|
email = details.get('email')
|
|
username = details.get('username')
|
|
if email:
|
|
user_queryset_filter['email'] = email
|
|
elif username:
|
|
user_queryset_filter['username__iexact'] = username
|
|
|
|
if user_queryset_filter:
|
|
return User.objects.filter(**user_queryset_filter).exists()
|
|
|
|
return False
|
|
|
|
|
|
def convert_saml_slug_provider_id(provider):
|
|
"""
|
|
Provider id is stored with the backend type prefixed to it (ie "saml-")
|
|
Slug is stored without this prefix.
|
|
This just converts between them whenever you expect the opposite of what you currently have.
|
|
|
|
Arguments:
|
|
provider (string): provider_id or slug
|
|
|
|
Returns:
|
|
(string): Opposite of what you inputted (slug -> provider_id; provider_id -> slug)
|
|
"""
|
|
if provider.startswith('saml-'):
|
|
return provider[5:]
|
|
else:
|
|
return 'saml-' + provider
|
|
|
|
|
|
def validate_uuid4_string(uuid_string):
|
|
"""
|
|
Returns True if valid uuid4 string, or False
|
|
"""
|
|
try:
|
|
UUID(uuid_string, version=4)
|
|
except ValueError:
|
|
return False
|
|
return True
|