Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for bearer token authorization #1189

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion jira/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ class JIRA:
or ``atlas-run-standalone`` commands. By default, this instance runs at
``http://localhost:2990/jira``. The ``options`` argument can be used to set the Jira instance to use.

Authentication is handled with the ``basic_auth`` argument. If authentication is supplied (and is
Authentication is handled with either the ``basic_auth`` or ``token_auth`` argument. If authentication is supplied (and is
accepted by Jira), the client will remember it for subsequent requests.

For quick command line access to a server, see the ``jirashell`` script included with this distribution.
Expand Down Expand Up @@ -326,6 +326,7 @@ def __init__(
server: str = None,
options: Dict[str, Union[str, bool, Any]] = None,
basic_auth: Union[None, Tuple[str, str]] = None,
token_auth: Union[None, str] = None,
oauth: Dict[str, Any] = None,
jwt: Dict[str, Any] = None,
kerberos=False,
Expand Down Expand Up @@ -371,6 +372,10 @@ def __init__(

basic_auth (Union[None, Tuple[str, str]]): A tuple of username and password to use when
establishing a session via HTTP BASIC authentication.

token_auth (Union[None, str]): A string to use for bearer token authorization. This will work for Jira Server
when Basic Auth has been disabled.

oauth (Optional[Any]): A dict of properties for OAuth authentication. The following properties are required:

* access_token -- OAuth access token for the user
Expand Down Expand Up @@ -464,6 +469,16 @@ def __init__(
elif basic_auth:
self._create_http_basic_session(*basic_auth, timeout=timeout)
self._session.headers.update(self._options["headers"])
elif token_auth:
self._create_http_token_session(token_auth, timeout=timeout)
# Avoid overwriting the Authorization header the above method sets
self._session.headers.update(
{
k: v
for k, v in self._options["headers"].items()
if not k == "Authorization"
}
)
elif jwt:
self._create_jwt_session(jwt, timeout)
elif kerberos:
Expand Down Expand Up @@ -3328,6 +3343,27 @@ def _create_http_basic_session(
client_cert: Tuple[str, str] = self._options["client_cert"] # to help mypy
self._session.cert = client_cert

def _create_http_token_session(
self,
token: str,
timeout: Optional[Union[Union[float, int], Tuple[float, float]]] = None,
):
"""Creates a bearer token http session.

Args:
token (str): Bearer token (Personal Access Token) for the session
timeout (Optional[int]): If set determines the timeout period for the Session.

Returns:
ResilientSession
"""
verify = bool(self._options["verify"])
self._session = ResilientSession(timeout=timeout)
self._session.verify = verify
self._session.headers.update({"Authorization": f"Bearer {token}"})
client_cert: Tuple[str, str] = self._options["client_cert"] # to help mypy
self._session.cert = client_cert

def _create_oauth_session(
self, oauth, timeout: Optional[Union[Union[float, int], Tuple[float, float]]]
):
Expand Down