| 14 | |
| 15 | # noinspection PyProtectedMember |
| 16 | class GitlabOAuthAuthenticator(AbstractOauthAuthenticator, OAuth2Mixin): |
| 17 | def __init__(self, params_dict): |
| 18 | self.gitlab_host = params_dict.get('url', 'https://gitlab.com') |
| 19 | gitlab_group_support = params_dict.get('group_support', True) |
| 20 | |
| 21 | super().__init__( |
| 22 | _OAUTH_AUTHORIZE_URL % self.gitlab_host, |
| 23 | _OAUTH_ACCESS_TOKEN_URL % self.gitlab_host, |
| 24 | 'api' if gitlab_group_support else 'read_user', |
| 25 | params_dict) |
| 26 | |
| 27 | self.gitlab_group_search = params_dict.get('group_search') |
| 28 | |
| 29 | async def fetch_user_info(self, access_token) -> _OauthUserInfo: |
| 30 | user = await self.oauth2_request( |
| 31 | _OAUTH_GITLAB_USERINFO % self.gitlab_host, |
| 32 | access_token=access_token) |
| 33 | if user is None: |
| 34 | return None |
| 35 | |
| 36 | active = user.get('state') == 'active' |
| 37 | return _OauthUserInfo(user.get('email'), active, user) |
| 38 | |
| 39 | async def fetch_user_groups(self, access_token): |
| 40 | args = { |
| 41 | 'access_token': access_token, |
| 42 | 'all_available': 'false', |
| 43 | 'per_page': 100, |
| 44 | } |
| 45 | |
| 46 | if self.gitlab_group_search is not None: |
| 47 | args['search'] = self.gitlab_group_search |
| 48 | |
| 49 | group_list_future = self.oauth2_request( |
| 50 | _OAUTH_GITLAB_GROUPS % self.gitlab_host, |
| 51 | **args |
| 52 | ) |
| 53 | |
| 54 | group_list = await group_list_future |
| 55 | |
| 56 | if group_list is None: |
| 57 | return None |
| 58 | |
| 59 | groups = [] |
| 60 | for group in group_list: |
| 61 | if group.get('full_path'): |
| 62 | groups.append(group['full_path']) |
| 63 | |
| 64 | return groups |
no outgoing calls