Skip to content

Commit

Permalink
Merge pull request #67 from ananace/access-token
Browse files Browse the repository at this point in the history
Add support for using predefined access tokens
  • Loading branch information
Guilhem Saurel authored Mar 8, 2023
2 parents 2f05e34 + 50362dc commit 5431e21
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Add support for using predefined access tokens
in [#67](https://github.com/nim65s/matrix-webhook/pull/67)
by [@ananace](https://github.com/ananace)

## [v3.6.0] - 2023-03-07

- update docker to python 3.11
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@ docker run --rm -it nim65s/matrix-webhook -h
```

```
usage: python -m matrix_webhook [-h] [-H HOST] [-P PORT] [-u MATRIX_URL] -i MATRIX_ID -p MATRIX_PW -k API_KEY [-v]
usage: python -m matrix_webhook [-h] [-H HOST] [-P PORT] [-u MATRIX_URL] -i MATRIX_ID (-p MATRIX_PW | -t MATRIX_TOKEN) -k API_KEY [-v]
Configuration for Matrix Webhook.
optional arguments:
options:
-h, --help show this help message and exit
-H HOST, --host HOST host to listen to. Default: `''`. Environment variable: `HOST`
-P PORT, --port PORT port to listed to. Default: 4785. Environment variable: `PORT`
Expand All @@ -49,7 +48,9 @@ optional arguments:
-i MATRIX_ID, --matrix-id MATRIX_ID
matrix user-id. Required. Environment variable: `MATRIX_ID`
-p MATRIX_PW, --matrix-pw MATRIX_PW
matrix password. Required. Environment variable: `MATRIX_PW`
matrix password. Either this or token required. Environment variable: `MATRIX_PW`
-t MATRIX_TOKEN, --matrix-token MATRIX_TOKEN
matrix access token. Either this or password required. Environment variable: `MATRIX_TOKEN`
-k API_KEY, --api-key API_KEY
shared secret to use this service. Required. Environment variable: `API_KEY`
-v, --verbose increment verbosity level
Expand Down
8 changes: 6 additions & 2 deletions matrix_webhook/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ async def main(event):
matrix client login & start web server
"""
LOGGER.info(f"Log in {conf.MATRIX_ID=} on {conf.MATRIX_URL=}")
await utils.CLIENT.login(conf.MATRIX_PW)
if conf.MATRIX_PW:
LOGGER.info(f"Log in {conf.MATRIX_ID=} on {conf.MATRIX_URL=}")
await utils.CLIENT.login(conf.MATRIX_PW)
else:
LOGGER.info(f"Restoring log in {conf.MATRIX_ID=} on {conf.MATRIX_URL=}")
utils.CLIENT.access_token = conf.MATRIX_TOKEN

server = web.Server(handler.matrix_webhook)
runner = web.ServerRunner(server)
Expand Down
22 changes: 15 additions & 7 deletions matrix_webhook/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,22 @@
else {"required": True}
),
)
parser.add_argument(
auth = parser.add_mutually_exclusive_group(
required=all(v not in os.environ for v in ["MATRIX_PW", "MATRIX_TOKEN"])
)
auth.add_argument(
"-p",
"--matrix-pw",
help="matrix password. Required. Environment variable: `MATRIX_PW`",
**(
{"default": os.environ["MATRIX_PW"]}
if "MATRIX_PW" in os.environ
else {"required": True}
),
help="matrix password. Either this or token required. "
"Environment variable: `MATRIX_PW`",
**({"default": os.environ["MATRIX_PW"]} if "MATRIX_PW" in os.environ else {}),
)
auth.add_argument(
"-t",
"--matrix-token",
help="matrix access token. Either this or password required. "
"Environment variable: `MATRIX_TOKEN`",
**({"default": os.environ["MATRIX_TOKEN"]} if "MATRIX_TOKEN" in os.environ else {}),
)
parser.add_argument(
"-k",
Expand All @@ -63,5 +70,6 @@
MATRIX_URL = args.matrix_url
MATRIX_ID = args.matrix_id
MATRIX_PW = args.matrix_pw
MATRIX_TOKEN = args.matrix_token
API_KEY = args.api_key
VERBOSE = args.verbose
6 changes: 4 additions & 2 deletions matrix_webhook/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ async def join_room(room_id):
if isinstance(resp, JoinError):
if resp.status_code == "M_UNKNOWN_TOKEN":
LOGGER.warning("Reconnecting")
await CLIENT.login(conf.MATRIX_PW)
if conf.MATRIX_PW:
await CLIENT.login(conf.MATRIX_PW)
else:
return create_json_response(error_map(resp), resp.message)
else:
Expand All @@ -67,7 +68,8 @@ async def send_room_message(room_id, content):
if isinstance(resp, RoomSendError):
if resp.status_code == "M_UNKNOWN_TOKEN":
LOGGER.warning("Reconnecting")
await CLIENT.login(conf.MATRIX_PW)
if conf.MATRIX_PW:
await CLIENT.login(conf.MATRIX_PW)
else:
return create_json_response(error_map(resp), resp.message)
else:
Expand Down

0 comments on commit 5431e21

Please sign in to comment.