Debian Codegraph @

allauth/socialaccount/providers/orcid/provider.py

raw
1 from __future__ import annotations
2
3 from allauth.socialaccount.providers.base import ProviderAccount
4 from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider
5 from allauth.socialaccount.providers.orcid.views import OrcidOAuth2Adapter
6
7
8 class Scope:
9 USERINFO_PROFILE = "/authenticate"
10
11
12 class OrcidAccount(ProviderAccount):
13 def get_profile_url(self):
14 return extract_from_dict(self.account.extra_data, ["orcid-identifier", "uri"])
15
16
17 class OrcidProvider(OAuth2Provider):
18 id = "orcid"
19 name = "Orcid.org"
20 account_class = OrcidAccount
21 oauth2_adapter_class = OrcidOAuth2Adapter
22
23 def get_default_scope(self):
24 return [Scope.USERINFO_PROFILE]
25
26 def extract_uid(self, data):
27 return extract_from_dict(data, ["orcid-identifier", "path"])
28
29 def extract_common_fields(self, data):
30 common_fields = dict(
31 email=extract_from_dict(data, ["person", "emails", "email", 0, "email"]),
32 last_name=extract_from_dict(
33 data, ["person", "name", "family-name", "value"]
34 ),
35 first_name=extract_from_dict(
36 data, ["person", "name", "given-names", "value"]
37 ),
38 )
39 return {key: value for (key, value) in common_fields.items() if value}
40
41
42 provider_classes = [OrcidProvider]
43
44
45 def extract_from_dict(data, path):
46 """
47 Navigate `data`, a multidimensional array (list or dictionary), and returns
48 the object at `path`.
49 """
50 value = data
51 try:
52 for key in path:
53 value = value[key]
54 return value
55 except (KeyError, IndexError, TypeError):
56 return ""