Enable logging in using an access token ()

We do a `client.load_store()` to restore a previous session.

If both token and password are defined, token is preferred, since it wont create a new device for the bot. One or the other needs to be defined.

Requires https://github.com/anoadragon453/nio-template/pull/20 for the configuration change since password and access token must both be optional (but one must be given).
This commit is contained in:
Jason Robinson 2020-12-15 00:38:47 +02:00 committed by GitHub
parent ff008a6aac
commit b808119a73
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 21 deletions

View file

@ -85,7 +85,11 @@ class Config(object):
if not re.match("@.*:.*", self.user_id):
raise ConfigError("matrix.user_id must be in the form @name:domain")
self.user_password = self._get_cfg(["matrix", "user_password"], required=True)
self.user_password = self._get_cfg(["matrix", "user_password"], required=False)
self.user_token = self._get_cfg(["matrix", "user_token"], required=False)
if not self.user_token and not self.user_password:
raise ConfigError("Must supply either user token or password")
self.device_id = self._get_cfg(["matrix", "device_id"], required=True)
self.device_name = self._get_cfg(
["matrix", "device_name"], default="nio-template"

View file

@ -51,6 +51,10 @@ async def main():
config=client_config,
)
if config.user_token:
client.access_token = config.user_token
client.user_id = config.user_id
# Set up event callbacks
callbacks = Callbacks(client, store, config)
client.add_event_callback(callbacks.message, (RoomMessageText,))
@ -59,28 +63,36 @@ async def main():
# Keep trying to reconnect on failure (with some time in-between)
while True:
try:
# Try to login with the configured username/password
try:
login_response = await client.login(
password=config.user_password, device_name=config.device_name,
)
if config.user_token:
# Use token to log in
client.load_store()
# Check if login failed
if type(login_response) == LoginError:
logger.error("Failed to login: %s", login_response.message)
# Sync encryption keys with the server
if client.should_upload_keys:
await client.keys_upload()
else:
# Try to login with the configured username/password
try:
login_response = await client.login(
password=config.user_password, device_name=config.device_name,
)
# Check if login failed
if type(login_response) == LoginError:
logger.error("Failed to login: %s", login_response.message)
return False
except LocalProtocolError as e:
# There's an edge case here where the user hasn't installed the correct C
# dependencies. In that case, a LocalProtocolError is raised on login.
logger.fatal(
"Failed to login. Have you installed the correct dependencies? "
"https://github.com/poljar/matrix-nio#installation "
"Error: %s",
e,
)
return False
except LocalProtocolError as e:
# There's an edge case here where the user hasn't installed the correct C
# dependencies. In that case, a LocalProtocolError is raised on login.
logger.fatal(
"Failed to login. Have you installed the correct dependencies? "
"https://github.com/poljar/matrix-nio#installation "
"Error: %s",
e,
)
return False
# Login succeeded!
# Login succeeded!
logger.info(f"Logged in as {config.user_id}")
await client.sync_forever(timeout=30000, full_state=True)

View file

@ -9,8 +9,10 @@ command_prefix: "!c"
matrix:
# The Matrix User ID of the bot account
user_id: "@bot:example.com"
# Matrix account password
# Matrix account password (optional if access token used)
user_password: ""
# Matrix account access token (optional if password used)
#user_token: ""
# The URL of the homeserver to connect to
homeserver_url: https://example.com
# The device ID that is **non pre-existing** device