forked from vbatts/maubot
Add option for listing auth servers
This commit is contained in:
parent
f52f8988f4
commit
37eeb73c47
2 changed files with 35 additions and 12 deletions
|
@ -29,7 +29,6 @@ history_count: int = 10
|
|||
|
||||
enc = functools.partial(quote, safe="")
|
||||
|
||||
|
||||
friendly_errors = {
|
||||
"server_not_found": "Registration target server not found.\n\n"
|
||||
"To log in or register through maubot, you must add the server to the\n"
|
||||
|
@ -39,24 +38,33 @@ friendly_errors = {
|
|||
|
||||
|
||||
@cliq.command(help="Log into a Matrix account via the Maubot server")
|
||||
@cliq.option("-h", "--homeserver", help="The homeserver to log into", required=True)
|
||||
@cliq.option("-u", "--username", help="The username to log in with", required=True)
|
||||
@cliq.option("-h", "--homeserver", help="The homeserver to log into", required_unless="list")
|
||||
@cliq.option("-u", "--username", help="The username to log in with", required_unless="list")
|
||||
@cliq.option("-p", "--password", help="The password to log in with", inq_type="password",
|
||||
required=True)
|
||||
required_unless="list")
|
||||
@cliq.option("-s", "--server", help="The maubot instance to log in through", default="",
|
||||
required=False, prompt=False)
|
||||
@click.option("-r", "--register", help="Register instead of logging in", is_flag=True,
|
||||
default=False)
|
||||
def auth(homeserver: str, username: str, password: str, server: str, register: bool) -> None:
|
||||
@click.option("-l", "--list", help="List available homeservers", is_flag=True, default=False)
|
||||
def auth(homeserver: str, username: str, password: str, server: str, register: bool, list: bool
|
||||
) -> None:
|
||||
server, token = get_token(server)
|
||||
if not token:
|
||||
return
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
if list:
|
||||
url = f"{server}/_matrix/maubot/v1/client/auth/servers"
|
||||
with urlopen(Request(url, headers=headers)) as resp_data:
|
||||
resp = json.load(resp_data)
|
||||
print(f"{Fore.GREEN}Available Matrix servers for registration and login:{Fore.RESET}")
|
||||
for server in resp.keys():
|
||||
print(f"* {Fore.CYAN}{server}{Fore.RESET}")
|
||||
return
|
||||
endpoint = "register" if register else "login"
|
||||
req = Request(f"{server}/_matrix/maubot/v1/client/auth/{enc(homeserver)}/{enc(endpoint)}",
|
||||
headers={
|
||||
"Authorization": f"Bearer {token}",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
headers["Content-Type"] = "application/json"
|
||||
url = f"{server}/_matrix/maubot/v1/client/auth/{enc(homeserver)}/{endpoint}"
|
||||
req = Request(url, headers=headers,
|
||||
data=json.dumps({
|
||||
"username": username,
|
||||
"password": password,
|
||||
|
@ -68,6 +76,7 @@ def auth(homeserver: str, username: str, password: str, server: str, register: b
|
|||
print(f"{Fore.GREEN}Successfully {action} "
|
||||
f"{Fore.CYAN}{resp['user_id']}{Fore.GREEN}.")
|
||||
print(f"{Fore.GREEN}Access token: {Fore.CYAN}{resp['access_token']}{Fore.RESET}")
|
||||
print(f"{Fore.GREEN}Device ID: {Fore.CYAN}{resp['device_id']}{Fore.RESET}")
|
||||
except HTTPError as e:
|
||||
try:
|
||||
err_data = json.load(e)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue