[Clients] handle Unicode decode error when uploading OPML
[mygpo.git] / mygpo / web / auth.py
blob9b01e52fb5f5d599f241e5b882d01a8aaa8c9647
2 # This file is part of my.gpodder.org.
4 # my.gpodder.org is free software: you can redistribute it and/or modify it
5 # under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or (at your
7 # option) any later version.
9 # my.gpodder.org is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
12 # License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with my.gpodder.org. If not, see <http://www.gnu.org/licenses/>.
18 from django.contrib.auth.backends import ModelBackend
19 from django.core.validators import validate_email
20 from django.core.exceptions import ValidationError
21 from django.conf import settings
22 from django.contrib.sites.requests import RequestSite
23 from django.contrib.auth import get_user_model
24 from django.core.urlresolvers import reverse
27 class EmailAuthenticationBackend(ModelBackend):
28 """ Auth backend to enable login with email address as username """
30 def authenticate(self, username=None, password=None):
31 try:
32 validate_email(username)
34 User = get_user_model()
35 try:
36 user = User.objects.get(email=username)
37 except User.DoesNotExist:
38 return None
40 return user if user.check_password(password) else None
42 except ValidationError:
43 return None
46 def get_google_oauth_flow(request):
47 """ Prepare an OAuth 2.0 flow
49 https://developers.google.com/api-client-library/python/guide/aaa_oauth """
51 from oauth2client.client import OAuth2WebServerFlow
53 site = RequestSite(request)
55 callback = 'http{s}://{domain}{callback}'.format(
56 s='s' if request.is_secure() else '',
57 domain=site.domain,
58 callback=reverse('login-google-callback'))
60 flow = OAuth2WebServerFlow(
61 client_id=settings.GOOGLE_CLIENT_ID,
62 client_secret=settings.GOOGLE_CLIENT_SECRET,
63 scope='https://www.googleapis.com/auth/userinfo.email',
64 redirect_uri=callback)
66 return flow