From e0ac159d44d2b89859ba8b608568bc4513bd2a9d Mon Sep 17 00:00:00 2001 From: Rafael Dantas Justo Date: Wed, 18 Nov 2026 13:15:13 -0300 Subject: [PATCH] Accept bearer tokens in TEAMWORK_API_TOKEN A token that starts with "tkn." goes in a Bearer header, read by curl from stdin so it stays out of the process list. Other values still use basic auth. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- action.yml | 2 +- src/teamwork.sh | 10 +++++++-- tests/cases/bearer-token/event.json | 27 +++++++++++++++++++++++ tests/cases/bearer-token/expected | 10 +++++++++ tests/cases/bearer-token/inputs | 3 +++ tests/cases/wrong-bearer-token/event.json | 27 +++++++++++++++++++++++ tests/cases/wrong-bearer-token/expected | 8 +++++++ tests/cases/wrong-bearer-token/inputs | 3 +++ tests/mock-teamwork.py | 4 ++-- 10 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 tests/cases/bearer-token/event.json create mode 100644 tests/cases/bearer-token/expected create mode 100644 tests/cases/bearer-token/inputs create mode 100644 tests/cases/wrong-bearer-token/event.json create mode 100644 tests/cases/wrong-bearer-token/expected create mode 100644 tests/cases/wrong-bearer-token/inputs diff --git a/README.md b/README.md index 5f36515f..e7e3553e 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ ### Prerequisites Create the next environment vars in your repository: * `TEAMWORK_URI`: The URL of your installation (e.g.: https://yourcompany.teamwork.com) -* `TEAMWORK_API_TOKEN`: The API token to authenticate the workflow. Follow [this guide](https://support.teamwork.com/projects/using-teamwork/locating-your-api-key) to find your URL and API key. +* `TEAMWORK_API_TOKEN`: The API token to authenticate the workflow. Follow [this guide](https://support.teamwork.com/projects/using-teamwork/locating-your-api-key) to find your URL and API key. A bearer token (it starts with `tkn.`) also works, follow [this guide](https://apidocs.teamwork.com/guides/teamwork/authentication) to generate one. **Please Note:** The Teamwork account associated with this API key is the account which these comments will be created under. If this user does not have permission to access a task's project, that task will be skipped and the workflow run will be marked as failed. diff --git a/action.yml b/action.yml index 738df59e..0c27db6c 100644 --- a/action.yml +++ b/action.yml @@ -12,7 +12,7 @@ inputs: description: 'Teamwork URI' required: true TEAMWORK_API_TOKEN: - description: 'Teamwork API token' + description: 'Teamwork API key, or a bearer token (starts with "tkn.")' required: true AUTOMATIC_TAGGING: description: 'Do you want to enable automatic tagging: true/false' diff --git a/src/teamwork.sh b/src/teamwork.sh index 9b421cd4..f68a359f 100644 --- a/src/teamwork.sh +++ b/src/teamwork.sh @@ -34,12 +34,18 @@ teamwork::request() { local -r path=$2 local -r payload=${3:-} - local args=(-sS --fail-with-body --max-time 30 -X "$method" -u "$TEAMWORK_API_TOKEN"':') + local args=(-sS --fail-with-body --max-time 30 -X "$method") if [ -n "$payload" ]; then args+=(-H 'Content-Type: application/json; charset=utf-8' -d "$payload") fi - curl "${args[@]}" "$TEAMWORK_URI$path" + # A "tkn." token is a bearer token, anything else is an API key. The bearer + # header goes in on stdin to keep the token out of the process list. + if [[ $TEAMWORK_API_TOKEN == tkn.* ]]; then + curl "${args[@]}" -H @- "$TEAMWORK_URI$path" <<< "Authorization: Bearer $TEAMWORK_API_TOKEN" + else + curl "${args[@]}" -u "$TEAMWORK_API_TOKEN"':' "$TEAMWORK_URI$path" + fi } # Logs a failed request and records it, so the run fails once every task has diff --git a/tests/cases/bearer-token/event.json b/tests/cases/bearer-token/event.json new file mode 100644 index 00000000..5f80ac3b --- /dev/null +++ b/tests/cases/bearer-token/event.json @@ -0,0 +1,27 @@ +{ + "action": "opened", + "number": 42, + "pull_request": { + "number": 42, + "title": "Add OAuth login", + "body": "Task: http://teamwork:8080/app/tasks/1001", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": false + }, + "sender": { + "login": "octocat" + } +} diff --git a/tests/cases/bearer-token/expected b/tests/cases/bearer-token/expected new file mode 100644 index 00000000..61b2b98a --- /dev/null +++ b/tests/cases/bearer-token/expected @@ -0,0 +1,10 @@ +exit code: 0 + +GET /projects/api/v1/tasks/1001.json -> 200 + +POST /tasks/1001/comments.json -> 201 + comment: + body: "PR [**\"Add OAuth login\"**](https://github.com/acme/webapp/pull/42) opened by **octocat**" + content-type: "text" + isprivate: false + notify: true diff --git a/tests/cases/bearer-token/inputs b/tests/cases/bearer-token/inputs new file mode 100644 index 00000000..18f518c4 --- /dev/null +++ b/tests/cases/bearer-token/inputs @@ -0,0 +1,3 @@ +GITHUB_EVENT_NAME=pull_request +INPUT_TEAMWORK_API_TOKEN=tkn.test-token +INPUT_LIGHTWEIGHT_COMMENT=true diff --git a/tests/cases/wrong-bearer-token/event.json b/tests/cases/wrong-bearer-token/event.json new file mode 100644 index 00000000..5f80ac3b --- /dev/null +++ b/tests/cases/wrong-bearer-token/event.json @@ -0,0 +1,27 @@ +{ + "action": "opened", + "number": 42, + "pull_request": { + "number": 42, + "title": "Add OAuth login", + "body": "Task: http://teamwork:8080/app/tasks/1001", + "html_url": "https://github.com/acme/webapp/pull/42", + "head": { + "ref": "feature/oauth", + "repo": { + "full_name": "acme/webapp" + } + }, + "base": { + "ref": "main" + }, + "commits": 3, + "changed_files": 5, + "additions": 120, + "deletions": 8, + "merged": false + }, + "sender": { + "login": "octocat" + } +} diff --git a/tests/cases/wrong-bearer-token/expected b/tests/cases/wrong-bearer-token/expected new file mode 100644 index 00000000..23f51418 --- /dev/null +++ b/tests/cases/wrong-bearer-token/expected @@ -0,0 +1,8 @@ +exit code: 1 + +stderr: + curl: (22) The requested URL returned error: 401 + Failed to get the project of task 1001, check the task exists and the API token's user can access it + 1 Teamwork request(s) failed, see the errors above + +GET /projects/api/v1/tasks/1001.json -> 401 diff --git a/tests/cases/wrong-bearer-token/inputs b/tests/cases/wrong-bearer-token/inputs new file mode 100644 index 00000000..8690570f --- /dev/null +++ b/tests/cases/wrong-bearer-token/inputs @@ -0,0 +1,3 @@ +GITHUB_EVENT_NAME=pull_request +INPUT_TEAMWORK_API_TOKEN=tkn.wrong-token +INPUT_LIGHTWEIGHT_COMMENT=true diff --git a/tests/mock-teamwork.py b/tests/mock-teamwork.py index 2058c2ba..35829887 100644 --- a/tests/mock-teamwork.py +++ b/tests/mock-teamwork.py @@ -10,7 +10,7 @@ from http.server import BaseHTTPRequestHandler, HTTPServer LOG_PATH = sys.argv[1] -AUTHORIZATION = "Basic " + base64.b64encode(b"test-token:").decode() +AUTHORIZATIONS = {"Basic " + base64.b64encode(b"test-token:").decode(), "Bearer tkn.test-token"} # Task id -> project id. Requests about any other task fail with a 404. TASKS = {"1001": 100, "1002": 200, "1003": 300} @@ -77,7 +77,7 @@ def respond(self, raw): except ValueError as error: return 400, {"MESSAGE": "Invalid JSON"}, [f" invalid JSON: {error}", " " + json.dumps(raw)] - if self.headers.get("Authorization") != AUTHORIZATION: + if self.headers.get("Authorization") not in AUTHORIZATIONS: return 401, {"MESSAGE": "Unauthorized"}, details task = re.search(r"/tasks/(\d+)", self.path)