blob: 584fe54bbd3da0f632313725d6f6b6d8ff00d818 [file] [log] [blame]
Mitja Nikolaus6a679132018-08-30 14:35:29 +02001"""Allauth adapter for authenticating requests using Google OAuth."""
2
Dirk Vogt57a615d2017-05-04 22:29:54 +02003from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
4from allauth.account.adapter import DefaultAccountAdapter
Mitja Nikolaus6a679132018-08-30 14:35:29 +02005from allauth.socialaccount.forms import SignupForm
6from allauth.socialaccount.models import SocialLogin
Dirk Vogt57a615d2017-05-04 22:29:54 +02007from django.core.exceptions import PermissionDenied
8from django.contrib.auth.models import Group
Mitja Nikolaus6a679132018-08-30 14:35:29 +02009from django.http import HttpRequest
Dirk Vogt57a615d2017-05-04 22:29:54 +020010
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020011
Dirk Vogt57a615d2017-05-04 22:29:54 +020012class FairphoneAccountAdapter(DefaultSocialAccountAdapter):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020013 """Account adapter for existing Google accounts."""
14
Dirk Vogt57a615d2017-05-04 22:29:54 +020015 def is_open_for_signup(self, request, sociallogin):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020016 """Allow signup."""
Dirk Vogt57a615d2017-05-04 22:29:54 +020017 return True
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020018
Mitja Nikolaus6a679132018-08-30 14:35:29 +020019 def save_user(
20 self,
21 request: HttpRequest,
22 sociallogin: SocialLogin,
23 form: SignupForm = None,
24 ):
25 """Save a user to the database.
26
27 Additionally add the user to the FairphoneSoftwareTeam group if his
28 or her account was issued by Fairphone, i.e. ends with "@fairphone.com".
29
30 Args:
31 request: The HTTP request.
32 sociallogin:
33 SocialLogin instance representing a Google user that is in
34 the process of being logged in.
35 form: Request form (not used).
36
37 Returns: The newly created user from the local database.
38
39 """
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020040 u = DefaultSocialAccountAdapter.save_user(
41 self, request, sociallogin, form=None
42 )
43 if u.email.split("@")[1] == "fairphone.com":
44 g = Group.objects.get(name="FairphoneSoftwareTeam")
Dirk Vogt57a615d2017-05-04 22:29:54 +020045 g.user_set.add(u)
46 return u
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020047
Mitja Nikolaus6a679132018-08-30 14:35:29 +020048 def populate_user(
49 self, request: HttpRequest, sociallogin: SocialLogin, data: dict
50 ):
51 """Populate an already existing user instance.
52
53 The permission is denied if the Google account was not issued by
54 Fairphone, i.e. does not end with "@fairphone.com".
55
56 Args:
57 request: The HTTP request.
58 sociallogin:
59 SocialLogin instance representing a Google user that is in
60 the process of being logged in.
61 data: Common user data fields.
62
63 Returns: The user from the database.
64
65 """
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020066 u = DefaultSocialAccountAdapter.populate_user(
67 self, request, sociallogin, data
68 )
69 if not u.email.split("@")[1] == "fairphone.com":
70 raise PermissionDenied()
Dirk Vogt57a615d2017-05-04 22:29:54 +020071 return u
72
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020073
Dirk Vogt57a615d2017-05-04 22:29:54 +020074class FormAccountAdapter(DefaultAccountAdapter):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020075 """Account adapter for signing up using a form.
76
77 Signup is not allowed using Hiccup, only existing Fairphone Google accounts
78 can be used.
79 """
80
Dirk Vogt57a615d2017-05-04 22:29:54 +020081 def is_open_for_signup(self, request):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020082 """Do not allow signup."""
Dirk Vogt57a615d2017-05-04 22:29:54 +020083 return False