• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SwissDataScienceCenter / renku-data-services / 6496472008

12 Oct 2023 01:39PM UTC coverage: 86.869% (+0.06%) from 86.813%
6496472008

push

gihub-action

web-flow
fix gitlab graphql query (#53)

20 of 20 new or added lines in 2 files covered. (100.0%)

2375 of 2734 relevant lines covered (86.87%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.11
/components/renku_data_services/users/gitlab.py
1
"""Gitlab authenticator."""
1✔
2
import urllib.parse as parse
1✔
3
from dataclasses import dataclass
1✔
4

5
import gitlab
1✔
6
from sanic import Request
1✔
7

8
import renku_data_services.base_models as base_models
1✔
9
from renku_data_services import errors
1✔
10

11

12
@dataclass
1✔
13
class GitlabAuthenticator:
1✔
14
    """Authenticator for gitlab repos.
1✔
15

16
    Note:
17
        Once we have a project service, this should get information on what type of git provider is used from there
18
        and support different backends.
19
    """
20

21
    gitlab_url: str
1✔
22

23
    token_field: str = "Gitlab-Access-Token"
1✔
24

25
    def __post_init__(self):
1✔
26
        """Properly set gitlab url."""
27
        parsed_url = parse.urlparse(self.gitlab_url)
1✔
28

29
        if not parsed_url.scheme:
1✔
30
            self.gitlab_url = f"https://{self.gitlab_url}"
×
31

32
    async def authenticate(self, access_token: str, request: Request) -> base_models.APIUser:
1✔
33
        """Checks the validity of the access token."""
34
        if self.token_field != "Authorization":  # nosec: B105
1✔
35
            access_token = str(request.headers.get(self.token_field))
1✔
36

37
        result = await self._get_gitlab_api_user(access_token)
1✔
38
        return result
1✔
39

40
    async def _get_gitlab_api_user(self, access_token: str) -> base_models.GitlabAPIUser:
1✔
41
        """Get and validate a Gitlab API User."""
42
        client = gitlab.Gitlab(self.gitlab_url, oauth_token=access_token)
1✔
43
        try:
1✔
44
            client.auth()  # needed for the user property to be set
1✔
45
        except gitlab.GitlabAuthenticationError:
×
46
            raise errors.Unauthorized(message="User not authorized with Gitlab")
×
47
        user = client.user
1✔
48
        if user is None:
1✔
49
            raise errors.Unauthorized(message="User not authorized with Gitlab")
×
50

51
        if user.state != "active":
1✔
52
            raise errors.Unauthorized(message="User isn't active in Gitlab")
1✔
53

54
        user_id = user.id
1✔
55

56
        if user_id is None:
1✔
57
            raise errors.Unauthorized(message="Could not get user id")
×
58

59
        return base_models.GitlabAPIUser(
1✔
60
            is_admin=False, id=str(user_id), access_token=access_token, name=user.name, gitlab_url=self.gitlab_url
61
        )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc