2020-09-01 14:39:04 +02:00
|
|
|
from typing import Optional
|
|
|
|
|
2019-10-21 13:56:42 +02:00
|
|
|
from synapse.storage._base import SQLBaseStore
|
2016-05-05 14:42:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
class OpenIdStore(SQLBaseStore):
|
2020-08-27 19:38:41 +02: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 14:42:44 +02:00
|
|
|
table="open_id_tokens",
|
|
|
|
values={
|
|
|
|
"token": token,
|
|
|
|
"ts_valid_until_ms": ts_valid_until_ms,
|
|
|
|
"user_id": user_id,
|
|
|
|
},
|
2019-04-03 11:07:29 +02:00
|
|
|
desc="insert_open_id_token",
|
2016-05-05 14:42:44 +02:00
|
|
|
)
|
|
|
|
|
2020-09-01 14:39:04 +02:00
|
|
|
async def get_user_id_for_open_id_token(
|
|
|
|
self, token: str, ts_now_ms: int
|
|
|
|
) -> Optional[str]:
|
2016-05-05 14:42:44 +02:00
|
|
|
def get_user_id_for_token_txn(txn):
|
|
|
|
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 11:07:29 +02:00
|
|
|
|
2020-09-01 14:39:04 +02:00
|
|
|
return await self.db_pool.runInteraction(
|
2019-12-04 14:52:46 +01:00
|
|
|
"get_user_id_for_token", get_user_id_for_token_txn
|
|
|
|
)
|