forked from City-of-Turku/tunnistamo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scopes.py
242 lines (198 loc) · 8.24 KB
/
scopes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import re
from django.utils.translation import ugettext_lazy as _
from oidc_provider.lib.claims import ScopeClaims, StandardScopeClaims
from oidc_provider.lib.errors import BearerTokenError
from social_django.models import UserSocialAuth
from auth_backends.models import SuomiFiAccessLevel
from .models import ApiScope
class ApiScopeClaims(ScopeClaims):
@classmethod
def get_scopes_info(cls, scopes=[]):
scopes_by_identifier = {
api_scope.identifier: api_scope
for api_scope in ApiScope.objects.by_identifiers(scopes)
}
api_scopes = (scopes_by_identifier.get(scope) for scope in scopes)
return [
{
'scope': api_scope.identifier,
'name': api_scope.name,
'description': api_scope.description,
}
for api_scope in api_scopes if api_scope
]
class GithubUsernameScopeClaims(ScopeClaims):
info_github_username = (
_("GitHub username"), _("Access to your GitHub username."))
def scope_github_username(self):
social_accounts = self.user.social_auth
github_account = social_accounts.filter(provider='github').first()
if not github_account:
return {}
github_data = github_account.extra_data
return {
'github_username': github_data.get('login'),
}
class DevicesScopeClaims(ScopeClaims):
info_devices = (
_('Devices'), _('Permission to link devices to your user account identities.'))
class IdentitiesScopeClaims(ScopeClaims):
info_identities = (
_('Identities'), _('Access to cards and other identity information.'))
class LoginEntriesScopeClaims(ScopeClaims):
info_login_entries = (
_('Login history'), _('Access to your login history.'))
class UserConsentsScopeClaims(ScopeClaims):
info_user_consents = (
_('Consents'), _('Permission to view and delete your consents for services.'))
class AdGroupsScopeClaims(ScopeClaims):
info_ad_groups = (_("AD Groups"), _("Access to your AD Group memberships."))
def scope_ad_groups(self):
return {
'ad_groups': list(self.user.ad_groups.all().values_list('name', flat=True)),
}
class ReducedStandardScopeClaims(StandardScopeClaims):
info_profile = (
_('Basic profile'),
_('Access to your basic information, which includes your first and last names.'),
)
info_email = (
_('Email address'),
_('Access to your email address.'),
)
info_phone = (
_('Phone number'),
_('Access to your phone number.'),
)
info_address = (
_('Address information'),
_('Access to your address. Includes country, locality, street and other information.'),
)
info_birthdate = (
_('Birthdate'),
_('Access to your birthdate.')
)
def scope_profile(self):
dic = {
'name': self.userinfo.get('name'),
'given_name': (self.userinfo.get('given_name') or
getattr(self.user, 'first_name', None)),
'family_name': (self.userinfo.get('family_name') or
getattr(self.user, 'last_name', None)),
'middle_name': self.userinfo.get('middle_name'),
'nickname': self.userinfo.get('nickname'),
'preferred_username': self.userinfo.get('preferred_username'),
'picture': self.userinfo.get('picture'),
'updated_at': self.userinfo.get('updated_at'),
}
self.__insert_student_role_if_applicable(dic)
self.__insert_oid(dic)
return dic
def scope_email(self):
return super().scope_email()
def scope_phone(self):
return super().scope_phone()
def scope_address(self):
return super().scope_address()
def scope_birthdate(self):
return {'birthdate': self.userinfo.get('birthdate')}
def __insert_student_role_if_applicable(self, attributes):
# opas_adfs provider inserts "school_role" in the extra data
try:
social_user = UserSocialAuth.objects.get(user=self.user, provider='opas_adfs')
extra_data = social_user.extra_data
key = 'school_role'
attributes[key] = extra_data[key] if key in extra_data else None
except UserSocialAuth.DoesNotExist:
pass
def __insert_oid(self, attributes):
try:
social_user = UserSocialAuth.objects.get(user=self.user)
attributes['oid'] = social_user.uid
except UserSocialAuth.DoesNotExist:
attributes['oid'] = None
class TurkuSuomiFiUserAttributeScopeClaims(ScopeClaims):
def scope_address(self):
address = {}
try:
social_user = UserSocialAuth.objects.get(user=self.user, provider='turku_suomifi')
extra_data = social_user.extra_data
address['address'] = {}
address['address']['municipality_code'] = extra_data['municipality_code']
address['address']['municipality_name'] = extra_data['municipality_name']
address['address']['postal_code'] = extra_data['postal_code']
address['non_disclosure'] = extra_data['non_disclosure']
except UserSocialAuth.DoesNotExist:
pass
return address
class SuomiFiUserAttributeScopeClaimsMeta(type):
def __dir__(cls):
names = super().__dir__()
for level in SuomiFiAccessLevel.objects.all():
names.append('info_suomifi_' + level.shorthand)
return names
def __getattr__(cls, name):
match = re.match(r'^info_suomifi_(.*)', name)
if match:
try:
level = SuomiFiAccessLevel.objects.get(shorthand=match.group(1))
return (level.name, level.description)
except SuomiFiAccessLevel.DoesNotExist:
raise AttributeError()
return super().__getattr__(name)
class SuomiFiUserAttributeScopeClaims(ScopeClaims, metaclass=SuomiFiUserAttributeScopeClaimsMeta):
def create_response_dic(self):
dic = {}
try:
social_user = UserSocialAuth.objects.get(user=self.user, provider='suomifi')
except UserSocialAuth.DoesNotExist:
return dic
for level in SuomiFiAccessLevel.objects.all():
scope = 'suomifi_' + level.shorthand
if scope in self.scopes:
if scope not in self.client.scope:
raise BearerTokenError('insufficient_scope')
dic[scope] = {}
for attribute in level.attributes.all():
if attribute.friendly_name in social_user.extra_data['suomifi_attributes']:
dic[scope][attribute.friendly_name] = \
social_user.extra_data['suomifi_attributes'][attribute.friendly_name]
dic = self._clean_dic(dic)
return dic
class OptionalOpenIDScopeClaims(ScopeClaims):
def scope_openid(self):
return {'amr': self.user.last_login_backend} if self.user.last_login_backend else {}
class CombinedScopeClaims(ScopeClaims):
combined_scope_claims = [
ReducedStandardScopeClaims,
GithubUsernameScopeClaims,
ApiScopeClaims,
DevicesScopeClaims,
IdentitiesScopeClaims,
LoginEntriesScopeClaims,
AdGroupsScopeClaims,
SuomiFiUserAttributeScopeClaims,
TurkuSuomiFiUserAttributeScopeClaims,
OptionalOpenIDScopeClaims,
]
@classmethod
def get_scopes_info(cls, scopes=[]):
extended_scopes = ApiScope.extend_scope(scopes)
scopes_info_map = {}
for claim_cls in cls.combined_scope_claims:
for info in claim_cls.get_scopes_info(extended_scopes):
scopes_info_map[info['scope']] = info
return [
scopes_info_map[scope]
for scope in extended_scopes
if scope in scopes_info_map
]
def __init__(self, token, *args, **kwargs):
self._token = token
super().__init__(token, *args, **kwargs)
def create_response_dic(self):
result = super(CombinedScopeClaims, self).create_response_dic()
for claim_cls in self.combined_scope_claims:
claim = claim_cls(self._token)
result.update(claim.create_response_dic())
return result