-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathclient.py
More file actions
85 lines (71 loc) · 2.54 KB
/
client.py
File metadata and controls
85 lines (71 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from abc import abstractmethod
from typing import Dict
from socketsecurity import USER_AGENT
from ..cli_client import CliClient
class ScmClient(CliClient):
def __init__(self, token: str, api_url: str):
self.token = token
self.api_url = api_url
@abstractmethod
def get_headers(self) -> Dict:
"""Each SCM implements its own auth headers"""
pass
def request(self, path: str, **kwargs):
"""Override base request to use SCM-specific headers and base_url"""
headers = kwargs.pop('headers', None) or self.get_headers()
return super().request(
path=path,
headers=headers,
base_url=self.api_url,
**kwargs
)
class GithubClient(ScmClient):
def get_headers(self) -> Dict:
return {
'Authorization': f"Bearer {self.token}",
'User-Agent': USER_AGENT,
"accept": "application/json"
}
class GitlabClient(ScmClient):
def get_headers(self) -> Dict:
"""
Determine the appropriate authentication headers for GitLab API.
Uses the same logic as GitlabConfig._get_auth_headers()
"""
return self._get_gitlab_auth_headers(self.token)
@staticmethod
def _get_gitlab_auth_headers(token: str) -> dict:
"""
Determine the appropriate authentication headers for GitLab API.
GitLab supports two authentication patterns:
1. Bearer token (OAuth 2.0 tokens, personal access tokens with api scope)
2. Private token (personal access tokens)
"""
import os
base_headers = {
'User-Agent': USER_AGENT,
"accept": "application/json"
}
# Check if this is a GitLab CI job token
if token == os.getenv('CI_JOB_TOKEN'):
return {
**base_headers,
'Authorization': f"Bearer {token}"
}
# Check for personal access token pattern
if token.startswith('glpat-'):
return {
**base_headers,
'Authorization': f"Bearer {token}"
}
# Check for OAuth token pattern (typically longer and alphanumeric)
if len(token) > 40 and token.isalnum():
return {
**base_headers,
'Authorization': f"Bearer {token}"
}
# Default to PRIVATE-TOKEN for other token types
return {
**base_headers,
'PRIVATE-TOKEN': f"{token}"
}