2021-11-11 08:47:31 -05:00
|
|
|
#
|
2023-11-21 15:29:58 -05:00
|
|
|
# This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2023 New Vector, Ltd
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# See the GNU Affero General Public License for more details:
|
|
|
|
# <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
|
|
#
|
|
|
|
# Originally licensed under the Apache License, Version 2.0:
|
|
|
|
# <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
|
|
#
|
|
|
|
# [This file includes modifications made by New Vector Limited]
|
2021-11-11 08:47:31 -05:00
|
|
|
#
|
|
|
|
#
|
|
|
|
|
2020-09-01 08:39:04 -04:00
|
|
|
from typing import Optional
|
|
|
|
|
2019-10-21 07:56:42 -04:00
|
|
|
from synapse.storage._base import SQLBaseStore
|
2021-11-11 08:47:31 -05:00
|
|
|
from synapse.storage.database import LoggingTransaction
|
2016-05-05 08:42:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
class OpenIdStore(SQLBaseStore):
|
2020-08-27 13:38:41 -04:00
|
|
|
async def insert_open_id_token(
|
|
|
|
self, token: str, ts_valid_until_ms: int, user_id: str
|
|
|
|
) -> None:
|
|
|
|
await self.db_pool.simple_insert(
|
2016-05-05 08:42:44 -04:00
|
|
|
table="open_id_tokens",
|
|
|
|
values={
|
|
|
|
"token": token,
|
|
|
|
"ts_valid_until_ms": ts_valid_until_ms,
|
|
|
|
"user_id": user_id,
|
|
|
|
},
|
2019-04-03 05:07:29 -04:00
|
|
|
desc="insert_open_id_token",
|
2016-05-05 08:42:44 -04:00
|
|
|
)
|
|
|
|
|
2020-09-01 08:39:04 -04:00
|
|
|
async def get_user_id_for_open_id_token(
|
|
|
|
self, token: str, ts_now_ms: int
|
|
|
|
) -> Optional[str]:
|
2021-11-11 08:47:31 -05:00
|
|
|
def get_user_id_for_token_txn(txn: LoggingTransaction) -> Optional[str]:
|
2016-05-05 08:42:44 -04:00
|
|
|
sql = (
|
|
|
|
"SELECT user_id FROM open_id_tokens"
|
|
|
|
" WHERE token = ? AND ? <= ts_valid_until_ms"
|
|
|
|
)
|
|
|
|
|
|
|
|
txn.execute(sql, (token, ts_now_ms))
|
|
|
|
|
|
|
|
rows = txn.fetchall()
|
|
|
|
if not rows:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return rows[0][0]
|
2019-04-03 05:07:29 -04:00
|
|
|
|
2020-09-01 08:39:04 -04:00
|
|
|
return await self.db_pool.runInteraction(
|
2019-12-04 08:52:46 -05:00
|
|
|
"get_user_id_for_token", get_user_id_for_token_txn
|
|
|
|
)
|