This repository has been archived by the owner on Jan 20, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add session manipulation API
- Loading branch information
Showing
8 changed files
with
127 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from json import dumps | ||
from time import time | ||
|
||
from flask import request | ||
|
||
from project_amber.const import MATURE_SESSION, MSG_IMMATURE_SESSION, EMPTY_RESP | ||
from project_amber.errors import Forbidden | ||
from project_amber.helpers.auth import handleChecks, getSessions, getSession,\ | ||
removeSessionById | ||
|
||
def handle_session_req(): | ||
""" | ||
Request handler for `/api/session`. Only accepts GET requests. Returns a | ||
list of sessions like the one below: | ||
``` | ||
{ | ||
"sessions": [ | ||
{ | ||
"id": 1, | ||
"login_time": 123456, // timestamp | ||
"address": "127.0.0.1" | ||
} | ||
{ | ||
"id": 2, | ||
"login_time": 123457, | ||
"address": "10.0.0.1" | ||
} | ||
] | ||
} | ||
``` | ||
""" | ||
user = handleChecks() | ||
sessions = getSessions(user.id) | ||
sessionList = [] | ||
for session in sessions: | ||
sessionList.append({ | ||
"id": session.id, | ||
"login_time": session.login_time, | ||
"address": session.address | ||
}) | ||
return dumps({ | ||
"sessions": sessionList | ||
}) | ||
|
||
def handle_session_id_req(session_id: int): | ||
""" | ||
Login handler for `/api/session/<id>`. Accepts GET and DELETE | ||
requests. Returns 404 if this session does not exist. On successful | ||
GET, returns JSON like this: | ||
``` | ||
{ | ||
"id": 1, | ||
"login_time": 123456, // timestamp | ||
"address": "127.0.0.1" | ||
} | ||
``` | ||
On DELETE, this will return HTTP 200 with empty JSON. There is a special | ||
case here: if a client session is too recent, this will respond with | ||
HTTP 403. | ||
""" | ||
user = handleChecks() | ||
if request.method == "GET": | ||
session = getSession(session_id, user.id) | ||
return dumps({ | ||
"id": session.id, | ||
"login_time": session.login_time, | ||
"address": session.address | ||
}) | ||
if request.method == "DELETE": | ||
if (time() - user.login_time) < MATURE_SESSION: | ||
raise Forbidden(MSG_IMMATURE_SESSION) | ||
removeSessionById(session_id, user.id) | ||
return EMPTY_RESP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters