2020-04-30 11:47:49 -06:00
|
|
|
# Copyright 2020 Matrix.org Foundation C.I.C.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2021-12-29 06:04:28 -07:00
|
|
|
from typing import Any, Dict, List, Optional, Tuple, Union, cast
|
2020-04-30 11:47:49 -06:00
|
|
|
|
|
|
|
import attr
|
|
|
|
|
2021-08-21 15:14:43 -06:00
|
|
|
from synapse.api.constants import LoginType
|
2020-04-30 11:47:49 -06:00
|
|
|
from synapse.api.errors import StoreError
|
2020-07-16 09:32:19 -06:00
|
|
|
from synapse.storage._base import SQLBaseStore, db_to_json
|
2020-08-20 07:00:59 -06:00
|
|
|
from synapse.storage.database import LoggingTransaction
|
2020-04-30 11:47:49 -06:00
|
|
|
from synapse.types import JsonDict
|
2020-08-20 08:32:33 -06:00
|
|
|
from synapse.util import json_encoder, stringutils
|
2020-04-30 11:47:49 -06:00
|
|
|
|
|
|
|
|
2022-01-13 06:49:28 -07:00
|
|
|
@attr.s(slots=True, auto_attribs=True)
|
2020-04-30 11:47:49 -06:00
|
|
|
class UIAuthSessionData:
|
2022-01-13 06:49:28 -07:00
|
|
|
session_id: str
|
2020-04-30 11:47:49 -06:00
|
|
|
# The dictionary from the client root level, not the 'auth' key.
|
2022-01-13 06:49:28 -07:00
|
|
|
clientdict: JsonDict
|
2020-04-30 11:47:49 -06:00
|
|
|
# The URI and method the session was intiatied with. These are checked at
|
|
|
|
# each stage of the authentication to ensure that the asked for operation
|
|
|
|
# has not changed.
|
2022-01-13 06:49:28 -07:00
|
|
|
uri: str
|
|
|
|
method: str
|
2020-04-30 11:47:49 -06:00
|
|
|
# A string description of the operation that the current authentication is
|
|
|
|
# authorising.
|
2022-01-13 06:49:28 -07:00
|
|
|
description: str
|
2020-04-30 11:47:49 -06:00
|
|
|
|
|
|
|
|
|
|
|
class UIAuthWorkerStore(SQLBaseStore):
|
|
|
|
"""
|
|
|
|
Manage user interactive authentication sessions.
|
|
|
|
"""
|
|
|
|
|
|
|
|
async def create_ui_auth_session(
|
|
|
|
self,
|
|
|
|
clientdict: JsonDict,
|
|
|
|
uri: str,
|
|
|
|
method: str,
|
|
|
|
description: str,
|
|
|
|
) -> UIAuthSessionData:
|
|
|
|
"""
|
|
|
|
Creates a new user interactive authentication session.
|
|
|
|
|
|
|
|
The session can be used to track the stages necessary to authenticate a
|
|
|
|
user across multiple HTTP requests.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
clientdict:
|
|
|
|
The dictionary from the client root level, not the 'auth' key.
|
|
|
|
uri:
|
|
|
|
The URI this session was initiated with, this is checked at each
|
|
|
|
stage of the authentication to ensure that the asked for
|
|
|
|
operation has not changed.
|
|
|
|
method:
|
|
|
|
The method this session was initiated with, this is checked at each
|
|
|
|
stage of the authentication to ensure that the asked for
|
|
|
|
operation has not changed.
|
|
|
|
description:
|
|
|
|
A string description of the operation that the current
|
|
|
|
authentication is authorising.
|
|
|
|
Returns:
|
|
|
|
The newly created session.
|
|
|
|
Raises:
|
|
|
|
StoreError if a unique session ID cannot be generated.
|
|
|
|
"""
|
|
|
|
# The clientdict gets stored as JSON.
|
2020-08-20 08:32:33 -06:00
|
|
|
clientdict_json = json_encoder.encode(clientdict)
|
2020-04-30 11:47:49 -06:00
|
|
|
|
|
|
|
# autogen a session ID and try to create it. We may clash, so just
|
|
|
|
# try a few times till one goes through, giving up eventually.
|
|
|
|
attempts = 0
|
|
|
|
while attempts < 5:
|
|
|
|
session_id = stringutils.random_string(24)
|
|
|
|
|
|
|
|
try:
|
2020-08-05 14:38:57 -06:00
|
|
|
await self.db_pool.simple_insert(
|
2020-04-30 11:47:49 -06:00
|
|
|
table="ui_auth_sessions",
|
|
|
|
values={
|
|
|
|
"session_id": session_id,
|
|
|
|
"clientdict": clientdict_json,
|
|
|
|
"uri": uri,
|
|
|
|
"method": method,
|
|
|
|
"description": description,
|
|
|
|
"serverdict": "{}",
|
|
|
|
"creation_time": self.hs.get_clock().time_msec(),
|
|
|
|
},
|
|
|
|
desc="create_ui_auth_session",
|
|
|
|
)
|
|
|
|
return UIAuthSessionData(
|
|
|
|
session_id, clientdict, uri, method, description
|
|
|
|
)
|
2020-08-05 14:38:57 -06:00
|
|
|
except self.db_pool.engine.module.IntegrityError:
|
2020-04-30 11:47:49 -06:00
|
|
|
attempts += 1
|
|
|
|
raise StoreError(500, "Couldn't generate a session ID.")
|
|
|
|
|
|
|
|
async def get_ui_auth_session(self, session_id: str) -> UIAuthSessionData:
|
|
|
|
"""Retrieve a UI auth session.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
session_id: The ID of the session.
|
|
|
|
Returns:
|
|
|
|
A dict containing the device information.
|
|
|
|
Raises:
|
|
|
|
StoreError if the session is not found.
|
|
|
|
"""
|
2020-08-05 14:38:57 -06:00
|
|
|
result = await self.db_pool.simple_select_one(
|
2020-04-30 11:47:49 -06:00
|
|
|
table="ui_auth_sessions",
|
|
|
|
keyvalues={"session_id": session_id},
|
|
|
|
retcols=("clientdict", "uri", "method", "description"),
|
|
|
|
desc="get_ui_auth_session",
|
|
|
|
)
|
|
|
|
|
2020-07-16 09:32:19 -06:00
|
|
|
result["clientdict"] = db_to_json(result["clientdict"])
|
2020-04-30 11:47:49 -06:00
|
|
|
|
|
|
|
return UIAuthSessionData(session_id, **result)
|
|
|
|
|
|
|
|
async def mark_ui_auth_stage_complete(
|
|
|
|
self,
|
|
|
|
session_id: str,
|
|
|
|
stage_type: str,
|
|
|
|
result: Union[str, bool, JsonDict],
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Mark a session stage as completed.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
session_id: The ID of the corresponding session.
|
|
|
|
stage_type: The completed stage type.
|
|
|
|
result: The result of the stage verification.
|
|
|
|
Raises:
|
|
|
|
StoreError if the session cannot be found.
|
|
|
|
"""
|
|
|
|
# Add (or update) the results of the current stage to the database.
|
|
|
|
#
|
|
|
|
# Note that we need to allow for the same stage to complete multiple
|
|
|
|
# times here so that registration is idempotent.
|
|
|
|
try:
|
2020-08-05 14:38:57 -06:00
|
|
|
await self.db_pool.simple_upsert(
|
2020-04-30 11:47:49 -06:00
|
|
|
table="ui_auth_sessions_credentials",
|
|
|
|
keyvalues={"session_id": session_id, "stage_type": stage_type},
|
2020-08-20 08:32:33 -06:00
|
|
|
values={"result": json_encoder.encode(result)},
|
2020-04-30 11:47:49 -06:00
|
|
|
desc="mark_ui_auth_stage_complete",
|
|
|
|
)
|
2020-08-05 14:38:57 -06:00
|
|
|
except self.db_pool.engine.module.IntegrityError:
|
2020-04-30 11:47:49 -06:00
|
|
|
raise StoreError(400, "Unknown session ID: %s" % (session_id,))
|
|
|
|
|
|
|
|
async def get_completed_ui_auth_stages(
|
|
|
|
self, session_id: str
|
|
|
|
) -> Dict[str, Union[str, bool, JsonDict]]:
|
|
|
|
"""
|
|
|
|
Retrieve the completed stages of a UI authentication session.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
session_id: The ID of the session.
|
|
|
|
Returns:
|
|
|
|
The completed stages mapped to the result of the verification of
|
|
|
|
that auth-type.
|
|
|
|
"""
|
|
|
|
results = {}
|
2020-08-05 14:38:57 -06:00
|
|
|
for row in await self.db_pool.simple_select_list(
|
2020-04-30 11:47:49 -06:00
|
|
|
table="ui_auth_sessions_credentials",
|
|
|
|
keyvalues={"session_id": session_id},
|
|
|
|
retcols=("stage_type", "result"),
|
|
|
|
desc="get_completed_ui_auth_stages",
|
|
|
|
):
|
2020-07-16 09:32:19 -06:00
|
|
|
results[row["stage_type"]] = db_to_json(row["result"])
|
2020-04-30 11:47:49 -06:00
|
|
|
|
|
|
|
return results
|
|
|
|
|
2020-05-08 14:08:58 -06:00
|
|
|
async def set_ui_auth_clientdict(
|
|
|
|
self, session_id: str, clientdict: JsonDict
|
|
|
|
) -> None:
|
|
|
|
"""
|
|
|
|
Store an updated clientdict for a given session ID.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
session_id: The ID of this session as returned from check_auth
|
|
|
|
clientdict:
|
|
|
|
The dictionary from the client root level, not the 'auth' key.
|
|
|
|
"""
|
|
|
|
# The clientdict gets stored as JSON.
|
2020-08-20 08:32:33 -06:00
|
|
|
clientdict_json = json_encoder.encode(clientdict)
|
2020-05-08 14:08:58 -06:00
|
|
|
|
2020-08-05 14:38:57 -06:00
|
|
|
await self.db_pool.simple_update_one(
|
2020-05-08 14:08:58 -06:00
|
|
|
table="ui_auth_sessions",
|
|
|
|
keyvalues={"session_id": session_id},
|
|
|
|
updatevalues={"clientdict": clientdict_json},
|
|
|
|
desc="set_ui_auth_client_dict",
|
|
|
|
)
|
|
|
|
|
2020-04-30 11:47:49 -06:00
|
|
|
async def set_ui_auth_session_data(self, session_id: str, key: str, value: Any):
|
|
|
|
"""
|
|
|
|
Store a key-value pair into the sessions data associated with this
|
|
|
|
request. This data is stored server-side and cannot be modified by
|
|
|
|
the client.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
session_id: The ID of this session as returned from check_auth
|
|
|
|
key: The key to store the data under
|
|
|
|
value: The data to store
|
|
|
|
Raises:
|
|
|
|
StoreError if the session cannot be found.
|
|
|
|
"""
|
2020-08-05 14:38:57 -06:00
|
|
|
await self.db_pool.runInteraction(
|
2020-04-30 11:47:49 -06:00
|
|
|
"set_ui_auth_session_data",
|
|
|
|
self._set_ui_auth_session_data_txn,
|
|
|
|
session_id,
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
)
|
|
|
|
|
2020-08-20 07:00:59 -06:00
|
|
|
def _set_ui_auth_session_data_txn(
|
|
|
|
self, txn: LoggingTransaction, session_id: str, key: str, value: Any
|
|
|
|
):
|
2020-04-30 11:47:49 -06:00
|
|
|
# Get the current value.
|
2021-12-29 06:04:28 -07:00
|
|
|
result = cast(
|
|
|
|
Dict[str, Any],
|
|
|
|
self.db_pool.simple_select_one_txn(
|
|
|
|
txn,
|
|
|
|
table="ui_auth_sessions",
|
|
|
|
keyvalues={"session_id": session_id},
|
|
|
|
retcols=("serverdict",),
|
|
|
|
),
|
2021-07-15 10:46:54 -06:00
|
|
|
)
|
2020-04-30 11:47:49 -06:00
|
|
|
|
|
|
|
# Update it and add it back to the database.
|
2020-07-16 09:32:19 -06:00
|
|
|
serverdict = db_to_json(result["serverdict"])
|
2020-04-30 11:47:49 -06:00
|
|
|
serverdict[key] = value
|
|
|
|
|
2020-08-05 14:38:57 -06:00
|
|
|
self.db_pool.simple_update_one_txn(
|
2020-04-30 11:47:49 -06:00
|
|
|
txn,
|
|
|
|
table="ui_auth_sessions",
|
|
|
|
keyvalues={"session_id": session_id},
|
2020-08-20 08:32:33 -06:00
|
|
|
updatevalues={"serverdict": json_encoder.encode(serverdict)},
|
2020-04-30 11:47:49 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
async def get_ui_auth_session_data(
|
|
|
|
self, session_id: str, key: str, default: Optional[Any] = None
|
|
|
|
) -> Any:
|
|
|
|
"""
|
|
|
|
Retrieve data stored with set_session_data
|
|
|
|
|
|
|
|
Args:
|
|
|
|
session_id: The ID of this session as returned from check_auth
|
|
|
|
key: The key to store the data under
|
|
|
|
default: Value to return if the key has not been set
|
|
|
|
Raises:
|
|
|
|
StoreError if the session cannot be found.
|
|
|
|
"""
|
2020-08-05 14:38:57 -06:00
|
|
|
result = await self.db_pool.simple_select_one(
|
2020-04-30 11:47:49 -06:00
|
|
|
table="ui_auth_sessions",
|
|
|
|
keyvalues={"session_id": session_id},
|
|
|
|
retcols=("serverdict",),
|
|
|
|
desc="get_ui_auth_session_data",
|
|
|
|
)
|
|
|
|
|
2020-07-16 09:32:19 -06:00
|
|
|
serverdict = db_to_json(result["serverdict"])
|
2020-04-30 11:47:49 -06:00
|
|
|
|
|
|
|
return serverdict.get(key, default)
|
|
|
|
|
2020-08-20 13:42:58 -06:00
|
|
|
async def add_user_agent_ip_to_ui_auth_session(
|
|
|
|
self,
|
|
|
|
session_id: str,
|
|
|
|
user_agent: str,
|
|
|
|
ip: str,
|
|
|
|
):
|
|
|
|
"""Add the given user agent / IP to the tracking table"""
|
|
|
|
await self.db_pool.simple_upsert(
|
|
|
|
table="ui_auth_sessions_ips",
|
|
|
|
keyvalues={"session_id": session_id, "user_agent": user_agent, "ip": ip},
|
|
|
|
values={},
|
|
|
|
desc="add_user_agent_ip_to_ui_auth_session",
|
|
|
|
)
|
|
|
|
|
|
|
|
async def get_user_agents_ips_to_ui_auth_session(
|
|
|
|
self,
|
|
|
|
session_id: str,
|
|
|
|
) -> List[Tuple[str, str]]:
|
|
|
|
"""Get the given user agents / IPs used during the ui auth process
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
List of user_agent/ip pairs
|
|
|
|
"""
|
|
|
|
rows = await self.db_pool.simple_select_list(
|
|
|
|
table="ui_auth_sessions_ips",
|
|
|
|
keyvalues={"session_id": session_id},
|
|
|
|
retcols=("user_agent", "ip"),
|
|
|
|
desc="get_user_agents_ips_to_ui_auth_session",
|
|
|
|
)
|
|
|
|
return [(row["user_agent"], row["ip"]) for row in rows]
|
|
|
|
|
2020-09-01 06:39:04 -06:00
|
|
|
async def delete_old_ui_auth_sessions(self, expiration_time: int) -> None:
|
2020-04-30 11:47:49 -06:00
|
|
|
"""
|
|
|
|
Remove sessions which were last used earlier than the expiration time.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
expiration_time: The latest time that is still considered valid.
|
|
|
|
This is an epoch time in milliseconds.
|
|
|
|
|
|
|
|
"""
|
2020-09-01 06:39:04 -06:00
|
|
|
await self.db_pool.runInteraction(
|
2020-04-30 11:47:49 -06:00
|
|
|
"delete_old_ui_auth_sessions",
|
|
|
|
self._delete_old_ui_auth_sessions_txn,
|
|
|
|
expiration_time,
|
|
|
|
)
|
|
|
|
|
2020-08-20 07:00:59 -06:00
|
|
|
def _delete_old_ui_auth_sessions_txn(
|
|
|
|
self, txn: LoggingTransaction, expiration_time: int
|
|
|
|
):
|
2020-04-30 11:47:49 -06:00
|
|
|
# Get the expired sessions.
|
|
|
|
sql = "SELECT session_id FROM ui_auth_sessions WHERE creation_time <= ?"
|
|
|
|
txn.execute(sql, [expiration_time])
|
|
|
|
session_ids = [r[0] for r in txn.fetchall()]
|
|
|
|
|
2020-08-20 13:42:58 -06:00
|
|
|
# Delete the corresponding IP/user agents.
|
|
|
|
self.db_pool.simple_delete_many_txn(
|
|
|
|
txn,
|
|
|
|
table="ui_auth_sessions_ips",
|
|
|
|
column="session_id",
|
2021-09-20 03:26:13 -06:00
|
|
|
values=session_ids,
|
2020-08-20 13:42:58 -06:00
|
|
|
keyvalues={},
|
|
|
|
)
|
|
|
|
|
2021-08-21 15:14:43 -06:00
|
|
|
# If a registration token was used, decrement the pending counter
|
|
|
|
# before deleting the session.
|
|
|
|
rows = self.db_pool.simple_select_many_txn(
|
|
|
|
txn,
|
|
|
|
table="ui_auth_sessions_credentials",
|
|
|
|
column="session_id",
|
|
|
|
iterable=session_ids,
|
|
|
|
keyvalues={"stage_type": LoginType.REGISTRATION_TOKEN},
|
|
|
|
retcols=["result"],
|
|
|
|
)
|
|
|
|
|
|
|
|
# Get the tokens used and how much pending needs to be decremented by.
|
|
|
|
token_counts: Dict[str, int] = {}
|
|
|
|
for r in rows:
|
|
|
|
# If registration was successfully completed, the result of the
|
|
|
|
# registration token stage for that session will be True.
|
|
|
|
# If a token was used to authenticate, but registration was
|
|
|
|
# never completed, the result will be the token used.
|
|
|
|
token = db_to_json(r["result"])
|
|
|
|
if isinstance(token, str):
|
|
|
|
token_counts[token] = token_counts.get(token, 0) + 1
|
|
|
|
|
|
|
|
# Update the `pending` counters.
|
|
|
|
if len(token_counts) > 0:
|
|
|
|
token_rows = self.db_pool.simple_select_many_txn(
|
|
|
|
txn,
|
|
|
|
table="registration_tokens",
|
|
|
|
column="token",
|
|
|
|
iterable=list(token_counts.keys()),
|
|
|
|
keyvalues={},
|
|
|
|
retcols=["token", "pending"],
|
|
|
|
)
|
|
|
|
for token_row in token_rows:
|
|
|
|
token = token_row["token"]
|
|
|
|
new_pending = token_row["pending"] - token_counts[token]
|
|
|
|
self.db_pool.simple_update_one_txn(
|
|
|
|
txn,
|
|
|
|
table="registration_tokens",
|
|
|
|
keyvalues={"token": token},
|
|
|
|
updatevalues={"pending": new_pending},
|
|
|
|
)
|
|
|
|
|
2020-04-30 11:47:49 -06:00
|
|
|
# Delete the corresponding completed credentials.
|
2020-08-05 14:38:57 -06:00
|
|
|
self.db_pool.simple_delete_many_txn(
|
2020-04-30 11:47:49 -06:00
|
|
|
txn,
|
|
|
|
table="ui_auth_sessions_credentials",
|
|
|
|
column="session_id",
|
2021-09-20 03:26:13 -06:00
|
|
|
values=session_ids,
|
2020-04-30 11:47:49 -06:00
|
|
|
keyvalues={},
|
|
|
|
)
|
|
|
|
|
|
|
|
# Finally, delete the sessions.
|
2020-08-05 14:38:57 -06:00
|
|
|
self.db_pool.simple_delete_many_txn(
|
2020-04-30 11:47:49 -06:00
|
|
|
txn,
|
|
|
|
table="ui_auth_sessions",
|
|
|
|
column="session_id",
|
2021-09-20 03:26:13 -06:00
|
|
|
values=session_ids,
|
2020-04-30 11:47:49 -06:00
|
|
|
keyvalues={},
|
|
|
|
)
|
2020-10-02 06:23:15 -06:00
|
|
|
|
|
|
|
|
|
|
|
class UIAuthStore(UIAuthWorkerStore):
|
|
|
|
pass
|