1 from django
.contrib
.auth
.backends
import ModelBackend
2 from django
.core
.validators
import validate_email
3 from django
.core
.exceptions
import ValidationError
4 from django
.conf
import settings
5 from django
.contrib
.sites
.requests
import RequestSite
6 from django
.contrib
.auth
import get_user_model
7 from django
.urls
import reverse
10 class EmailAuthenticationBackend(ModelBackend
):
11 """Auth backend to enable login with email address as username"""
13 def authenticate(self
, request
, username
=None, password
=None):
15 validate_email(username
)
17 User
= get_user_model()
19 user
= User
.objects
.get(email
=username
)
20 except User
.DoesNotExist
:
23 return user
if user
.check_password(password
) else None
25 except ValidationError
:
29 def get_google_oauth_flow(request
):
30 """Prepare an OAuth 2.0 flow
32 https://developers.google.com/api-client-library/python/guide/aaa_oauth"""
34 from oauth2client
.client
import OAuth2WebServerFlow
36 site
= RequestSite(request
)
38 callback
= "http{s}://{domain}{callback}".format(
39 s
="s" if request
.is_secure() else "",
41 callback
=reverse("login-google-callback"),
44 flow
= OAuth2WebServerFlow(
45 client_id
=settings
.GOOGLE_CLIENT_ID
,
46 client_secret
=settings
.GOOGLE_CLIENT_SECRET
,
47 scope
="https://www.googleapis.com/auth/userinfo.email",
48 redirect_uri
=callback
,