auth.py 966 Bytes
Newer Older
Jon Yu's avatar
Jon Yu committed
1
2
import unicodedata

3
from django.conf import settings
Jon Yu's avatar
Jon Yu committed
4
5
6
from mozilla_django_oidc.auth import OIDCAuthenticationBackend


7
8
9
10
11
12
13
14
15
16
def using_oidc_auth():
    return (
        settings.OIDC_RP_CLIENT_ID and
        settings.OIDC_RP_CLIENT_SECRET and
        settings.OIDC_OP_AUTHORIZATION_ENDPOINT and
        settings.OIDC_OP_TOKEN_ENDPOINT and
        settings.OIDC_OP_USER_ENDPOINT
    )


Jon Yu's avatar
Jon Yu committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def generate_username(email):
    return unicodedata.normalize('NFKC', email).split('@')[0]


class UMichOIDCBackend(OIDCAuthenticationBackend):
    @staticmethod
    def _set_claims(user, claims):
        user.first_name = claims.get('given_name', '')
        user.last_name = claims.get('family_name', '')

    def create_user(self, claims):
        user = super().create_user(claims)
        self._set_claims(user, claims)
        user.save()
        return user

    def update_user(self, user, claims):
        self._set_claims(user, claims)
        user.save()
        return user