Skip to content

Commit

Permalink
Add support for general api token (#54)
Browse files Browse the repository at this point in the history
* Add support for general api token

---------

Co-authored-by: mjtice <[email protected]>
  • Loading branch information
mjtice and mjtice authored Jul 7, 2023
1 parent 6b0bf49 commit 4fdb094
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## 2.3.0

- Add new `api_token` auth method. This authentication method is different than a `pat` authentication request. (PR #54)
- Added `pat` and `cookie` auth methods to the sensors.

## 2.2.0

- Adjust jql in sensor to better support large JIRA projects
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ Include the following settings when using the `PAT` auth_method:

* ``token`` - PAT token

Include the following settings when using the `api_token` auth_method:

* ``token`` - api token
* ``username`` - Username

If using the `oauth` auth_method, take a look at the OAuth section below for further setup instructions.

You can also use dynamic values from the datastore. See the
Expand Down
9 changes: 8 additions & 1 deletion actions/lib/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from jira import JIRA
import base64

# from st2common.runners.base_action import Action
__all__ = [
Expand Down Expand Up @@ -50,9 +51,15 @@ def _get_client(self):
basic_creds = (config['username'], config['password'])
client = JIRA(options=options, auth=basic_creds)

elif auth_method == 'api_token':
headers = JIRA.DEFAULT_OPTIONS["headers"].copy()
b64_header = base64.b64encode(f"{config['username']}:{config['token']}".encode())
headers["Authorization"] = f"Basic {b64_header.decode()}"
client = JIRA(server=config['url'], options={"headers": headers})

else:
msg = ('You must set auth_method to either "oauth", ',
'"basic", or "cookie" in your Jira pack config file.')
'"basic", "pat", "api_token", or "cookie" in your Jira pack config file.')
raise Exception(msg)

return client
Expand Down
5 changes: 3 additions & 2 deletions config.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
- basic
- pat
- cookie
- api_token
username:
description: "Username if using the basic or cookie auth_method"
description: "Username if using the basic, api_token, or cookie auth_method"
type: "string"
secret: false
required: false
Expand All @@ -30,7 +31,7 @@
secret: true
required: false
token:
description: "PAT token"
description: "PAT or API token"
type: "string"
secret: true
required: false
Expand Down
2 changes: 1 addition & 1 deletion pack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ keywords:
- issues
- ticket management
- project management
version: 2.2.0
version: 2.3.0
python_versions:
- "3"
author : StackStorm, Inc.
Expand Down
22 changes: 19 additions & 3 deletions sensors/jira_sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# See ./requirements.txt for requirements.
import os

import base64
from jira.client import JIRA

from st2reactor.sensor.base import PollingSensor
Expand Down Expand Up @@ -61,9 +61,25 @@ def setup(self):
self._jira_client = JIRA(options={'server': self._jira_url},
basic_auth=basic_creds)

elif auth_method == 'pat':
headers = JIRA.DEFAULT_OPTIONS["headers"].copy()
headers["Authorization"] = f"Bearer {self._config['token']}"
self._jira_client = JIRA(server=self._jira_url, options={"headers": headers})

elif auth_method == 'cookie':
basic_creds = (self._config['username'], self._config['password'])
self._jira_client = JIRA(server=self._jira_url, auth=basic_creds)

elif auth_method == 'api_token':
headers = JIRA.DEFAULT_OPTIONS["headers"].copy()
b64_header = base64.b64encode(f"{self._config['username']}:{self._config['token']}"
.encode())
headers["Authorization"] = f"Basic {b64_header.decode()}"
self._jira_client = JIRA(server=self._jira_url, options={"headers": headers})

else:
msg = ('You must set auth_method to either "oauth"',
'or "basic" your jira.yaml config file.')
msg = ('You must set auth_method to either "oauth", ',
'"basic", "pat", "api_token", or "cookie" in your Jira pack config file.')
raise Exception(msg)

if self._projects_available is None:
Expand Down
21 changes: 19 additions & 2 deletions sensors/jira_sensor_for_apiv2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# See ./requirements.txt for requirements.
import os
import base64

from jira.client import JIRA

Expand Down Expand Up @@ -61,9 +62,25 @@ def setup(self):
self._jira_client = JIRA(options={'server': self._jira_url},
basic_auth=basic_creds)

elif auth_method == 'pat':
headers = JIRA.DEFAULT_OPTIONS["headers"].copy()
headers["Authorization"] = f"Bearer {self._config['token']}"
self._jira_client = JIRA(server=self._jira_url, options={"headers": headers})

elif auth_method == 'cookie':
basic_creds = (self._config['username'], self._config['password'])
self._jira_client = JIRA(server=self._jira_url, auth=basic_creds)

elif auth_method == 'api_token':
headers = JIRA.DEFAULT_OPTIONS["headers"].copy()
b64_header = base64.b64encode(f"{self._config['username']}:{self._config['token']}"
.encode())
headers["Authorization"] = f"Basic {b64_header.decode()}"
self._jira_client = JIRA(server=self._jira_url, options={"headers": headers})

else:
msg = ('You must set auth_method to either "oauth"',
'or "basic" your jira.yaml config file.')
msg = ('You must set auth_method to either "oauth", ',
'"basic", "pat", "api_token", or "cookie" in your Jira pack config file.')
raise Exception(msg)

if self._projects_available is None:
Expand Down

0 comments on commit 4fdb094

Please sign in to comment.