Debian Codegraph @

django_mailman3/lib/auth/fedora/provider.py

raw
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2012-2023 by the Free Software Foundation, Inc.
3 #
4 # This file is part of Django-Mailman.
5 #
6 # HyperKitty is free software: you can redistribute it and/or modify it under
7 # the terms of the GNU General Public License as published by the Free
8 # Software Foundation, either version 3 of the License, or (at your option)
9 # any later version.
10 #
11 # HyperKitty is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 # more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Django-Mailman. If not, see <http://www.gnu.org/licenses/>.
18 #
19 # Author: Aurelien Bompard <[email protected]>
20 #
21
22 from urllib.parse import urlparse
23
24 from django.urls import reverse
25 from django.utils.http import urlencode
26
27 from allauth.account.models import EmailAddress
28 from allauth.socialaccount import providers
29 from allauth.socialaccount.providers.openid.provider import (
30 OpenIDAccount, OpenIDProvider)
31 from allauth.socialaccount.providers.openid.utils import (
32 get_email_from_response)
33
34
35 def extract_username(url):
36 return urlparse(url).netloc.split('.')[0]
37
38
39 class FedoraAccount(OpenIDAccount):
40
41 def get_brand(self):
42 return dict(id='fedora', name='Fedora')
43
44 def to_str(self):
45 return extract_username(self.account.uid)
46
47
48 class FedoraProvider(OpenIDProvider):
49 id = 'fedora'
50 name = 'Fedora'
51 account_class = FedoraAccount
52 endpoint = 'https://id.fedoraproject.org'
53 login_view = 'fedora_login'
54
55 def get_login_url(self, request, **kwargs):
56 url = reverse(self.login_view)
57 if kwargs:
58 url += '?' + urlencode(kwargs)
59 return url
60
61 def extract_username(self, data):
62 """
63 https://fedoraproject.org/wiki/OpenID
64 For fedoraproject.org, the identity_url looks like:
65
66 https://username.id.fedoraproject.org
67 """
68 return extract_username(data.identity_url)
69
70 def extract_common_fields(self, data):
71 fields = super(FedoraProvider, self).extract_common_fields(data)
72 fields['username'] = self.extract_username(data)
73 return fields
74
75 def extract_email_addresses(self, data):
76 """
77 For example:
78
79 [EmailAddress(email='[email protected]',
80 verified=True,
81 primary=True)]
82 """
83 ret = []
84 primary_email = get_email_from_response(data)
85 if primary_email:
86 # It would be added by cleanup_email_addresses(), but we add it
87 # here to mark it as verified.
88 ret.append(EmailAddress(
89 email=primary_email, verified=True, primary=True))
90 # Add the email alias provided by the Fedora project.
91 ret.append(EmailAddress(
92 email='%[email protected]' % self.extract_username(data),
93 verified=True, primary=False))
94 return ret
95
96
97 providers.registry.register(FedoraProvider)