blob: c132bb465a73dfa48dc185906cc22eec5b1a4f8d [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 Nikolaus78e3a052018-09-05 12:18:35 +020011FP_STAFF_GROUP_NAME = "FairphoneSoftwareTeam"
12
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020013
Dirk Vogt57a615d2017-05-04 22:29:54 +020014class FairphoneAccountAdapter(DefaultSocialAccountAdapter):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020015 """Account adapter for existing Google accounts."""
16
Dirk Vogt57a615d2017-05-04 22:29:54 +020017 def is_open_for_signup(self, request, sociallogin):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020018 """Allow signup."""
Dirk Vogt57a615d2017-05-04 22:29:54 +020019 return True
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020020
Mitja Nikolaus6a679132018-08-30 14:35:29 +020021 def save_user(
22 self,
23 request: HttpRequest,
24 sociallogin: SocialLogin,
25 form: SignupForm = None,
26 ):
27 """Save a user to the database.
28
29 Additionally add the user to the FairphoneSoftwareTeam group if his
30 or her account was issued by Fairphone, i.e. ends with "@fairphone.com".
31
32 Args:
33 request: The HTTP request.
34 sociallogin:
35 SocialLogin instance representing a Google user that is in
36 the process of being logged in.
37 form: Request form (not used).
38
39 Returns: The newly created user from the local database.
40
41 """
Mitja Nikolause7d3c762018-08-30 17:29:27 +020042 user = DefaultSocialAccountAdapter.save_user(
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020043 self, request, sociallogin, form=None
44 )
Mitja Nikolause7d3c762018-08-30 17:29:27 +020045 if user.email.split("@")[1] == "fairphone.com":
Mitja Nikolaus78e3a052018-09-05 12:18:35 +020046 group = Group.objects.get(name=FP_STAFF_GROUP_NAME)
Mitja Nikolause7d3c762018-08-30 17:29:27 +020047 group.user_set.add(user)
48 return user
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020049
Mitja Nikolaus6a679132018-08-30 14:35:29 +020050 def populate_user(
51 self, request: HttpRequest, sociallogin: SocialLogin, data: dict
52 ):
53 """Populate an already existing user instance.
54
55 The permission is denied if the Google account was not issued by
56 Fairphone, i.e. does not end with "@fairphone.com".
57
58 Args:
59 request: The HTTP request.
60 sociallogin:
61 SocialLogin instance representing a Google user that is in
62 the process of being logged in.
63 data: Common user data fields.
64
65 Returns: The user from the database.
66
67 """
Mitja Nikolause7d3c762018-08-30 17:29:27 +020068 user = DefaultSocialAccountAdapter.populate_user(
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020069 self, request, sociallogin, data
70 )
Mitja Nikolause7d3c762018-08-30 17:29:27 +020071 if not user.email.split("@")[1] == "fairphone.com":
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020072 raise PermissionDenied()
Mitja Nikolause7d3c762018-08-30 17:29:27 +020073 return user
Dirk Vogt57a615d2017-05-04 22:29:54 +020074
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020075
Dirk Vogt57a615d2017-05-04 22:29:54 +020076class FormAccountAdapter(DefaultAccountAdapter):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020077 """Account adapter for signing up using a form.
78
79 Signup is not allowed using Hiccup, only existing Fairphone Google accounts
80 can be used.
81 """
82
Dirk Vogt57a615d2017-05-04 22:29:54 +020083 def is_open_for_signup(self, request):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020084 """Do not allow signup."""
Dirk Vogt57a615d2017-05-04 22:29:54 +020085 return False