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

Conversation

ametzger
Copy link
Contributor

This adds support for Authorization: Bearer <some_token>-style authorization to Jira. This is relevant in cases where someone has a Jira Server on which they have disabled basic authentication. For these users, using a Personal Access Token in combination with this change will allow authorization where basic_auth will not.

This is currently achievable by setting the options parameter in the constructor (see Jellyfish-AI/jf_agent#161) but that does not play nicely with the default headers.

Please let me know if this isn't clear or doesn't abide by proper contribution guidelines.

In action:

In [1]: from jira import JIRA

In [2]: personal_access_token = '<redacted>'

In [3]: j = JIRA('http://localhost:8080', basic_auth=('<redacted>', personal_access_token))
---------------------------------------------------------------------------
JIRAError                                 Traceback (most recent call last)
<ipython-input-3-c1f3b39bc65e> in <module>
----> 1 j = JIRA('http://localhost:8080', basic_auth=('<redacted>', personal_access_token))

~/proj/vendor/jira/jira/client.py in __init__(self, server, options, basic_auth, token_auth, oauth, jwt, kerberos, kerberos_options, validate, get_server_info, async_, async_workers, logging, max_retries, proxies, timeout, auth)
    517         if get_server_info:
    518             # We need version in order to know what API calls are available or not
--> 519             si = self.server_info()
    520             try:
    521                 self._version = tuple(si["versionNumbers"])

~/proj/vendor/jira/jira/client.py in server_info(self)
   2834         """
   2835         retry = 0
-> 2836         j = self._get_json("serverInfo")
   2837         while not j and retry < 3:
   2838             self.log.warning(

~/proj/vendor/jira/jira/client.py in _get_json(self, path, params, base)
   3488         """
   3489         url = self._get_url(path, base)
-> 3490         r = self._session.get(url, params=params)
   3491         try:
   3492             r_json = json_loads(r)

~/proj/vendor/jira/jira/resilientsession.py in get(self, url, **kwargs)
    193
    194     def get(self, url: Union[str, bytes], **kwargs) -> Response:  # type: ignore
--> 195         return self.__verb("GET", str(url), **kwargs)
    196
    197     def post(self, url: Union[str, bytes], data=None, json=None, **kwargs) -> Response:  # type: ignore

~/proj/vendor/jira/jira/resilientsession.py in __verb(self, verb, url, retry_data, **kwargs)
    187         if exception is not None:
    188             raise exception
--> 189         raise_on_error(response, verb=verb, **kwargs)
    190         # after raise_on_error, only Response objects are allowed through
    191         response = cast(Response, response)  # tell mypy only Response-like are here

~/proj/vendor/jira/jira/resilientsession.py in raise_on_error(r, verb, **kwargs)
     68             request=request,
     69             response=r,
---> 70             **kwargs,
     71         )
     72     # for debugging weird errors on CI

JIRAError: JiraError HTTP 403 url: http://localhost:8080/rest/api/2/serverInfo
        text: Basic Authentication has been disabled on this instance.

        response headers = {'X-AREQUESTID': '1275x843x1', 'Referrer-Policy': 'strict-origin-when-cross-origin', 'X-XSS-Protection': '1; mode=block', 'X-Content-Type-Options': 'nosniff', 'X-Frame-Options': 'SAMEORIGIN', 'Content-Security-Policy': 'sandbox', 'Strict-Transport-Security': 'max-age=31536000', 'Content-Encoding': 'gzip', 'Vary': 'User-Agent', 'Content-Type': 'application/json;charset=UTF-8', 'Content-Length': '85', 'Date': 'Mon, 18 Oct 2021 21:15:13 GMT', 'Keep-Alive': 'timeout=20', 'Connection': 'keep-alive'}
        response text = {"message":"Basic Authentication has been disabled on this instance."}

In [4]: j = JIRA('http://localhost:8080', token_auth=personal_access_token)

In [5]: j.projects()
Out[5]: [<JIRA Project: key='TEST', name='test', id='10000'>]

@adehad adehad added the feature label Oct 21, 2021
@adehad
Copy link
Contributor

adehad commented Oct 21, 2021

Thanks @ametzger , we were looking into this in #991 and were trying to see if we could get some unit testing setup: #991 (comment)

I was supposed to try having a go, but didn't get the chance. And I won't be free for a while again. Would you be able to try whipping something up?

@ametzger
Copy link
Contributor Author

Sure thing! Sorry, missed that PR. I'll close this one as that one looks more idiomatic. Will see if there's something simple to put together.

@ametzger
Copy link
Contributor Author

Closing: dupe of #991

@ametzger ametzger closed this Oct 21, 2021
@adehad
Copy link
Contributor

adehad commented Oct 21, 2021

No worries at all, feel free to reopen this PR if appropriate. (As we've been making several improvements to help contributors that probably haven't made its way to that original branch)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants