feat(client): replace basic auth with OAuth ROPC flow by nejch · Pull Request #2422 · python-gitlab/python-gitlab
REDIRECT_MSG = ( "python-gitlab detected a {status_code} ({reason!r}) redirection. You must update "
Keyword Args: requests.Session session: HTTP Requests Session
self.private_token = private_token self.http_username = http_username self.http_password = http_password self.oauth_token = oauth_token self.job_token = job_token self._set_auth_info() self.oauth_credentials = oauth_credentials
#: Create a session object for requests _backend: Type[_backends.DefaultBackend] = kwargs.pop(
self._set_auth_info() self.per_page = per_page self.pagination = pagination self.order_by = order_by
def _set_auth_info(self) -> None: tokens = [ token for token in [self.private_token, self.oauth_token, self.job_token] if token auth_types = [ auth for auth in [ self.private_token, self.oauth_token, self.oauth_credentials, self.job_token, ] if auth ] if len(tokens) > 1: if len(auth_types) > 1: raise ValueError( "Only one of private_token, oauth_token or job_token should " "be defined" ) if (self.http_username and not self.http_password) or ( not self.http_username and self.http_password ): raise ValueError("Both http_username and http_password should be defined") if tokens and self.http_username: raise ValueError( "Only one of token authentications or http " "authentication should be defined" "Only one of private_token, oauth_token, oauth_credentials" "or job_token should be defined" )
self._auth: Optional[requests.auth.AuthBase] = None if self.private_token: self._auth = _backends.PrivateTokenAuth(self.private_token) return
if self.oauth_token: self._auth = _backends.OAuthTokenAuth(self.oauth_token) return
if self.oauth_credentials: post_data = { "grant_type": self.oauth_credentials.grant_type, "scope": self.oauth_credentials.scope, "username": self.oauth_credentials.username, "password": self.oauth_credentials.password, } response = self.http_post( f"{self._base_url}/oauth/token", post_data=post_data ) if isinstance(response, dict): self.oauth_token = response["access_token"] else: self.oauth_token = response.json()["access_token"] self._auth = self.oauth_credentials.basic_auth return
if self.job_token: self._auth = _backends.JobTokenAuth(self.job_token)
if self.http_username and self.http_password: self._auth = requests.auth.HTTPBasicAuth( self.http_username, self.http_password )
@staticmethod def enable_debug() -> None: import logging