Skip to content

auth.py

create_admin_user(uid, response, details, user, social, *args, **kwargs)

Give Django admin privileges to a user who login via GitHub and belong to a specific team. The parameters are as per python-social-auth docs https://python-social-auth.readthedocs.io/en/latest/pipeline.html#extending-the-pipeline

Parameters:

Name Type Description Default
uid int

user id

required
response Dict

request dictionary

required
details Dict

user details generated by the backend

required
user User

Django user model object

required
social UserSocialAuth

Social auth user model object

required

Returns:

Type Description
Dict

return a dictionary with the Django User object in it or empty if

Dict

no action are taken

Source code in vast_pipeline/utils/auth.py
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
def create_admin_user(uid: int, response: Dict, details: Dict, user: User,
    social: UserSocialAuth , *args, **kwargs) -> Dict:
    """
    Give Django admin privileges to a user who login via GitHub and belong to
    a specific team. The parameters are as per python-social-auth docs
    https://python-social-auth.readthedocs.io/en/latest/pipeline.html#extending-the-pipeline

    Args:
        uid:
            user id
        response:
            request dictionary
        details:
            user details generated by the backend
        user:
            Django user model object
        social:
            Social auth user model object

    Returns:
        return a dictionary with the Django User object in it or empty if
        no action are taken
    """
    # assume github-org backend, add <if backend.name == 'github-org'>
    # if other backend are implemented
    admin_team = settings.SOCIAL_AUTH_GITHUB_ADMIN_TEAM
    usr = response.get('login', '')
    if (usr != '' and admin_team != '' and user and not user.is_staff and
        not user.is_superuser):
        logger.info('Trying to add Django admin privileges to user')
        # check if github user belong to admin team
        org = settings.SOCIAL_AUTH_GITHUB_ORG_NAME
        header = {
            'Authorization': f"token {response.get('access_token', '')}"
        }
        url = (
            f'https://api.github.com/orgs/{org}/teams/{admin_team}'
            f'/memberships/{usr}'
        )
        resp = requests.get(url, headers=header)
        if resp.ok:
            # add user to admin
            user.is_superuser = True
            user.is_staff = True
            user.save()
            logger.info('Django admin privileges successfully added to user')
            return {'user': user}
        logger.info(f'GitHub request failed, reason: {resp.reason}')

        return {}

    return {}

load_github_avatar(response, social, *args, **kwargs)

Add GitHub avatar url to the extra data stored by social_django app

Parameters:

Name Type Description Default
response Dict

request dictionary

required
social UserSocialAuth

Social auth user model object

required
*args

Variable length argument list.

()
**kwargs

Arbitrary keyword arguments.

{}

Returns:

Type Description
Dict

return a dictionary with the Social auth user object in it or empty if

Dict

no action are taken

Source code in vast_pipeline/utils/auth.py
 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
def load_github_avatar(response: Dict, social: UserSocialAuth, *args,
    **kwargs) -> Dict:
    """
    Add GitHub avatar url to the extra data stored by social_django app

    Args:
        response:
            request dictionary
        social:
            Social auth user model object
        *args:
            Variable length argument list.
        **kwargs:
            Arbitrary keyword arguments.

    Returns:
        return a dictionary with the Social auth user object in it or empty if
        no action are taken
    """
    # assume github-org backend, add <if backend.name == 'github-org'>
    # if other backend are implemented
    # if social and social.get('extra_data', None)
    # print(vars(social))
    if 'avatar_url' not in social.extra_data:
        logger.info('Adding GitHub avatar url to user extra data')
        social.extra_data['avatar_url'] = response['avatar_url']
        social.save()
        return {'social': social}

    return {}

Last update: March 2, 2022
Created: March 2, 2022