summary refs log tree commit diff
path: root/synapse/storage/databases/main/openid.py
blob: 4db8949da767e79c17a2a8e62568e127c72c12c6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from synapse.storage._base import SQLBaseStore


class OpenIdStore(SQLBaseStore):
    async def insert_open_id_token(
        self, token: str, ts_valid_until_ms: int, user_id: str
    ) -> None:
        await self.db_pool.simple_insert(
            table="open_id_tokens",
            values={
                "token": token,
                "ts_valid_until_ms": ts_valid_until_ms,
                "user_id": user_id,
            },
            desc="insert_open_id_token",
        )

    def get_user_id_for_open_id_token(self, token, ts_now_ms):
        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]

        return self.db_pool.runInteraction(
            "get_user_id_for_token", get_user_id_for_token_txn
        )