140 |
140 |
logger.warning('auth_oidc: id_token nonce %r != expected nonce %r', id_token_nonce, nonce)
|
141 |
141 |
return None
|
142 |
142 |
|
143 |
|
User = get_user_model()
|
144 |
|
user = None
|
145 |
|
if provider.strategy == models.OIDCProvider.STRATEGY_FIND_UUID:
|
146 |
|
# use the OP sub to find an user by UUUID
|
147 |
|
# it means OP and RP share the same account base and OP is passing its UUID as sub
|
148 |
|
try:
|
149 |
|
user = User.objects.get(uuid=id_token.sub, is_active=True)
|
150 |
|
except User.DoesNotExist:
|
151 |
|
pass
|
152 |
|
else:
|
153 |
|
logger.info('auth_oidc: found user using UUID (=sub) "%s": %s', id_token.sub, user)
|
154 |
|
elif provider.strategy == models.OIDCProvider.STRATEGY_FIND_USERNAME:
|
155 |
|
users = User.objects.filter(username=id_token.sub, is_active=True).order_by('pk')
|
156 |
|
if not users:
|
157 |
|
logger.warning('auth_oidc: user with username (=sub) "%s" not found', id_token.sub)
|
158 |
|
else:
|
159 |
|
user = users[0]
|
160 |
|
logger.info('auth_oidc: found user using username (=sub) "%s": %s', id_token.sub, user)
|
161 |
|
else:
|
162 |
|
try:
|
163 |
|
user = User.objects.get(
|
164 |
|
oidc_account__provider=provider, oidc_account__sub=id_token.sub, is_active=True
|
165 |
|
)
|
166 |
|
except User.DoesNotExist:
|
167 |
|
pass
|
168 |
|
else:
|
169 |
|
logger.info('auth_oidc: found user using with sub "%s": %s', id_token.sub, user)
|
|
143 |
# map claims to attributes or user fields
|
|
144 |
# mapping is done before eventual creation of user as EMAIL_IS_UNIQUE needs to know if the
|
|
145 |
# mapping will provide some mail to us
|
|
146 |
ou_map = {ou.slug: ou for ou in OrganizationalUnit.cached()}
|
|
147 |
user_ou = provider.ou
|
|
148 |
user_info = None
|
|
149 |
save_user = False
|
|
150 |
mappings = []
|
|
151 |
context = id_token_content.copy()
|
170 |
152 |
need_user_info = False
|
171 |
153 |
for claim_mapping in provider.claim_mappings.all():
|
172 |
154 |
need_user_info = need_user_info or not claim_mapping.idtoken_claim
|
173 |
155 |
|
174 |
|
user_info = None
|
175 |
156 |
if need_user_info:
|
176 |
157 |
if not access_token:
|
177 |
158 |
logger.warning('auth_oidc: need user info for some claims, but no access token was returned')
|
... | ... | |
193 |
174 |
logger.warning('auth_oidc: bad JSON in user info response, %s (%r)', e, response.content)
|
194 |
175 |
else:
|
195 |
176 |
logger.debug('auth_oidc: user_info content %s', user_info)
|
196 |
|
|
197 |
|
# check for required claims
|
198 |
|
for claim_mapping in provider.claim_mappings.all():
|
199 |
|
claim = claim_mapping.claim
|
200 |
|
if claim_mapping.required:
|
201 |
|
if '{{' in claim or '{%' in claim:
|
202 |
|
logger.warning('claim \'%r\' is templated, it cannot be set as required')
|
203 |
|
elif claim_mapping.idtoken_claim and claim not in id_token:
|
204 |
|
logger.warning(
|
205 |
|
'auth_oidc: cannot create user missing required claim %r in id_token (%r)',
|
206 |
|
claim,
|
207 |
|
id_token,
|
208 |
|
)
|
209 |
|
return None
|
210 |
|
elif not user_info or claim not in user_info:
|
211 |
|
logger.warning(
|
212 |
|
'auth_oidc: cannot create user missing required claim %r in user_info (%r)',
|
213 |
|
claim,
|
214 |
|
user_info,
|
215 |
|
)
|
216 |
|
return None
|
217 |
|
|
218 |
|
# map claims to attributes or user fields
|
219 |
|
# mapping is done before eventual creation of user as EMAIL_IS_UNIQUE needs to know if the
|
220 |
|
# mapping will provide some mail to us
|
221 |
|
ou_map = {ou.slug: ou for ou in OrganizationalUnit.cached()}
|
222 |
|
user_ou = provider.ou
|
223 |
|
save_user = False
|
224 |
|
mappings = []
|
225 |
|
context = id_token_content.copy()
|
226 |
|
if need_user_info:
|
227 |
|
context.update(user_info or {})
|
|
177 |
context.update(user_info or {})
|
228 |
178 |
|
229 |
179 |
for claim_mapping in provider.claim_mappings.all():
|
230 |
180 |
claim = claim_mapping.claim
|
... | ... | |
251 |
201 |
verified = True
|
252 |
202 |
mappings.append((attribute, value, verified))
|
253 |
203 |
|
|
204 |
# check for required claims
|
|
205 |
for claim_mapping in provider.claim_mappings.all():
|
|
206 |
claim = claim_mapping.claim
|
|
207 |
if claim_mapping.required:
|
|
208 |
if '{{' in claim or '{%' in claim:
|
|
209 |
logger.warning('claim \'%r\' is templated, it cannot be set as required')
|
|
210 |
elif claim_mapping.idtoken_claim and claim not in id_token:
|
|
211 |
logger.warning(
|
|
212 |
'auth_oidc: cannot create user missing required claim %r in id_token (%r)',
|
|
213 |
claim,
|
|
214 |
id_token,
|
|
215 |
)
|
|
216 |
return None
|
|
217 |
elif not user_info or claim not in user_info:
|
|
218 |
logger.warning(
|
|
219 |
'auth_oidc: cannot create user missing required claim %r in user_info (%r)',
|
|
220 |
claim,
|
|
221 |
user_info,
|
|
222 |
)
|
|
223 |
return None
|
|
224 |
|
254 |
225 |
# find en email in mappings
|
255 |
226 |
email = None
|
256 |
227 |
for attribute, value, verified in mappings:
|
257 |
228 |
if attribute == 'email':
|
258 |
229 |
email = value
|
259 |
230 |
|
|
231 |
User = get_user_model()
|
|
232 |
user = None
|
|
233 |
if provider.strategy == models.OIDCProvider.STRATEGY_FIND_UUID:
|
|
234 |
# use the OP sub to find an user by UUUID
|
|
235 |
# it means OP and RP share the same account base and OP is passing its UUID as sub
|
|
236 |
try:
|
|
237 |
user = User.objects.get(uuid=id_token.sub, is_active=True)
|
|
238 |
except User.DoesNotExist:
|
|
239 |
pass
|
|
240 |
else:
|
|
241 |
logger.info('auth_oidc: found user using UUID (=sub) "%s": %s', id_token.sub, user)
|
|
242 |
elif provider.strategy == models.OIDCProvider.STRATEGY_FIND_USERNAME:
|
|
243 |
users = User.objects.filter(username=id_token.sub, is_active=True).order_by('pk')
|
|
244 |
if not users:
|
|
245 |
logger.warning('auth_oidc: user with username (=sub) "%s" not found', id_token.sub)
|
|
246 |
else:
|
|
247 |
user = users[0]
|
|
248 |
logger.info('auth_oidc: found user using username (=sub) "%s": %s', id_token.sub, user)
|
|
249 |
elif provider.strategy == models.OIDCProvider.STRATEGY_FIND_EMAIL:
|
|
250 |
if email and id_token.sub != email:
|
|
251 |
logger.warning(
|
|
252 |
'auth_oidc: email claim (%s) does not match subject identifier (%s) yet STRATEGY_FIND_EMAIL, fallback on email claim.',
|
|
253 |
email,
|
|
254 |
id_token.sub,
|
|
255 |
)
|
|
256 |
elif not email:
|
|
257 |
logger.warning(
|
|
258 |
'auth_oidc: email claim absent yet STRATEGY_FIND_EMAIL is set, using subject identifier (%s) instead',
|
|
259 |
id_token.sub,
|
|
260 |
)
|
|
261 |
email = id_token.sub
|
|
262 |
|
|
263 |
if not email:
|
|
264 |
logger.error(
|
|
265 |
'auth_oidc: email lookup activated for provider "%s" yet no email received', provider
|
|
266 |
)
|
|
267 |
users = User.objects.filter(email__iexact=email, is_active=True)
|
|
268 |
if not app_settings.A2_EMAIL_IS_UNIQUE and provider.ou:
|
|
269 |
users = users.filter(ou=provider.ou)
|
|
270 |
Lock.lock_email(email)
|
|
271 |
try:
|
|
272 |
user = users.get()
|
|
273 |
except User.DoesNotExist:
|
|
274 |
logger.warning('auth_oidc: user with email "%s" not found', email)
|
|
275 |
else:
|
|
276 |
logger.info('auth_oidc: found user using email "%s": %s', email, user)
|
|
277 |
else:
|
|
278 |
try:
|
|
279 |
user = User.objects.get(
|
|
280 |
oidc_account__provider=provider, oidc_account__sub=id_token.sub, is_active=True
|
|
281 |
)
|
|
282 |
except User.DoesNotExist:
|
|
283 |
pass
|
|
284 |
else:
|
|
285 |
logger.info('auth_oidc: found user using with sub "%s": %s', id_token.sub, user)
|
|
286 |
|
260 |
287 |
# eventually create a new user or link to an existing one based on email
|
261 |
288 |
created_user = False
|
262 |
289 |
linked = False
|