Remove unused license preamble
[mygpo.git] / mygpo / web / auth.py
blob79f1e1dfaace24a9a95817bba98b414c69209f5a
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.core.urlresolvers import reverse
10 class EmailAuthenticationBackend(ModelBackend):
11 """ Auth backend to enable login with email address as username """
13 def authenticate(self, username=None, password=None):
14 try:
15 validate_email(username)
17 User = get_user_model()
18 try:
19 user = User.objects.get(email=username)
20 except User.DoesNotExist:
21 return None
23 return user if user.check_password(password) else None
25 except ValidationError:
26 return None
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 '',
40 domain=site.domain,
41 callback=reverse('login-google-callback'))
43 flow = OAuth2WebServerFlow(
44 client_id=settings.GOOGLE_CLIENT_ID,
45 client_secret=settings.GOOGLE_CLIENT_SECRET,
46 scope='https://www.googleapis.com/auth/userinfo.email',
47 redirect_uri=callback)
49 return flow