Ask for server URL before showing sign in box

Contact the server's .well-known API before anything to get
available login flows instead of blindly assuming it will be
m.login.password, and to get the server's real URL instead of
requiring users to remember that e.g. it's "chat.privacytools.io"
and not just "privacytools.io" despite user IDs making it look like so.

The server field will also now remember the last accepted URL.
This commit is contained in:
miruka
2020-07-24 01:30:35 -04:00
parent b94d1e8168
commit d7907db547
13 changed files with 294 additions and 109 deletions

View File

@@ -6,14 +6,14 @@ import os
import sys
import traceback
from pathlib import Path
from typing import Any, DefaultDict, Dict, List, Optional
from typing import Any, DefaultDict, Dict, List, Optional, Tuple
from appdirs import AppDirs
import nio
from . import __app_name__
from .errors import MatrixError
from .errors import MatrixError, MatrixForbidden, MatrixNotFound
from .matrix_client import MatrixClient
from .media_cache import MediaCache
from .models import SyncId
@@ -129,6 +129,30 @@ class Backend:
# Clients management
async def server_info(self, homeserver: str) -> Tuple[str, List[str]]:
"""Return server's real URL and supported login flows.
Retrieving the real URL uses the `.well-known` API.
Possible login methods include `m.login.password` or `m.login.sso`.
"""
client = MatrixClient(self, homeserver=homeserver)
try:
homeserver = (await client.discovery_info()).homeserver_url
except (MatrixNotFound, MatrixForbidden):
# This is either already the real URL, or an invalid URL.
pass
else:
await client.close()
client = MatrixClient(self, homeserver=homeserver)
try:
return (homeserver, (await client.login_info()).flows)
finally:
await client.close()
async def login_client(self,
user: str,
password: str,

View File

@@ -152,11 +152,13 @@ class MatrixClient(nio.AsyncClient):
}
def __init__(self,
backend,
user: str,
homeserver: str = "https://matrix.org",
device_id: Optional[str] = None) -> None:
def __init__(
self,
backend,
user: str = "",
homeserver: str = "https://matrix.org",
device_id: Optional[str] = None,
) -> None:
if not urlparse(homeserver).scheme:
raise ValueError(