Add more stuff

This commit is contained in:
Tulir Asokan
2020-10-31 23:54:08 +02:00
parent 9151f4cb6d
commit 0b15a44820
8 changed files with 158 additions and 29 deletions

View File

@ -13,7 +13,20 @@
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from aiohttp import web
from ..database import User, AccessToken
routes = web.RouteTableDef()
@routes.get("/whoami")
async def whoami(req: web.Request) -> web.Response:
user: User = req["user"]
token: AccessToken = req["token"]
return web.json_response({
"id": user.id,
"widget_secret": user.widget_secret,
"homeserver_url": user.homeserver_url,
"last_seen": int(token.last_seen_date.timestamp() / 60) * 60,
})

View File

@ -37,7 +37,8 @@ class AccessToken(Base):
@classmethod
async def get(cls, token_id: int) -> Optional['AccessToken']:
q = "SELECT user_id, token_hash, last_seen_ip, last_seen_date FROM pack WHERE token_id=$1"
q = ("SELECT user_id, token_hash, last_seen_ip, last_seen_date "
"FROM access_token WHERE token_id=$1")
row: asyncpg.Record = await cls.db.fetchrow(q, token_id)
if row is None:
return None
@ -48,7 +49,7 @@ class AccessToken(Base):
== datetime.now().replace(second=0, microsecond=0)):
# Same IP and last seen on this minute, skip update
return
q = ("UPDATE access_token SET last_seen_ip=$3, last_seen_date=current_timestamp "
q = ("UPDATE access_token SET last_seen_ip=$2, last_seen_date=current_timestamp "
"WHERE token_id=$1 RETURNING last_seen_date")
self.last_seen_date = await self.db.fetchval(q, self.token_id, ip)
self.last_seen_ip = ip