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
.models
import RequestSite
23 from django
.contrib
.auth
import get_user_model
24 from django
.core
.urlresolvers
import reverse
27 class EmailAuthenticationBackend(ModelBackend
):
28 def authenticate(self
, username
=None, password
=None):
30 validate_email(username
)
32 User
= get_user_model()
34 user
= User
.objects
.get(email
=username
)
35 except User
.DoesNotExist
:
38 return user
if user
.check_password(password
) else None
40 except ValidationError
:
43 def get_user(self
, username
):
44 User
= get_user_model()
46 return User
.objects
.get(username
=username
)
47 except User
.DoesNotExist
:
51 def get_google_oauth_flow(request
):
52 """ Prepare an OAuth 2.0 flow
54 https://developers.google.com/api-client-library/python/guide/aaa_oauth """
56 from oauth2client
.client
import OAuth2WebServerFlow
58 site
= RequestSite(request
)
60 callback
= 'http{s}://{domain}{callback}'.format(
61 s
='s' if request
.is_secure() else '',
63 callback
=reverse('login-google-callback'))
65 flow
= OAuth2WebServerFlow(
66 client_id
=settings
.GOOGLE_CLIENT_ID
,
67 client_secret
=settings
.GOOGLE_CLIENT_SECRET
,
68 scope
='https://www.googleapis.com/auth/userinfo.email',
69 redirect_uri
=callback
)