Skip to content

Commit 57652b8

Browse files
authored
Add blueprint level auth guards to graphs, users and remote filesystems endpoints (#1219)
1 parent fb6f3e3 commit 57652b8

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

pydatalab/src/pydatalab/routes/v0_1/graphs.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
from flask import Blueprint, jsonify, request
22

33
from pydatalab.mongo import flask_mongo
4-
from pydatalab.permissions import get_default_permissions
4+
from pydatalab.permissions import active_users_or_get_only, get_default_permissions
55

66
GRAPHS = Blueprint("graphs", __name__)
77

88

9+
@GRAPHS.before_request
10+
@active_users_or_get_only
11+
def _(): ...
12+
13+
914
@GRAPHS.route("/item-graph", methods=["GET"])
1015
@GRAPHS.route("/item-graph/<item_id>", methods=["GET"])
1116
def get_graph_cy_format(

pydatalab/src/pydatalab/routes/v0_1/remotes.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from flask_login import current_user
66

77
from pydatalab.config import CONFIG
8+
from pydatalab.permissions import active_users_or_get_only
89
from pydatalab.remote_filesystems import (
910
get_directory_structure,
1011
get_directory_structures,
@@ -25,6 +26,11 @@ def _check_invalidate_cache(args: dict[str, str]) -> bool | None:
2526
REMOTES = Blueprint("remotes", __name__)
2627

2728

29+
@REMOTES.before_request
30+
@active_users_or_get_only
31+
def _(): ...
32+
33+
2834
@REMOTES.route("/list-remote-directories", methods=["GET"])
2935
@REMOTES.route("/remotes", methods=["GET"])
3036
def list_remote_directories():
@@ -34,7 +40,10 @@ def list_remote_directories():
3440
then it will be reconstructed.
3541
3642
"""
37-
if not current_user.is_authenticated and not CONFIG.TESTING:
43+
if (
44+
not (current_user.is_authenticated and current_user.account_status == "active")
45+
and not CONFIG.TESTING
46+
):
3847
return (
3948
jsonify(
4049
{

pydatalab/src/pydatalab/routes/v0_1/users.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@
55
from pydatalab.config import CONFIG
66
from pydatalab.models.people import DisplayName, EmailStr
77
from pydatalab.mongo import flask_mongo
8+
from pydatalab.permissions import active_users_or_get_only
89

910
USERS = Blueprint("users", __name__)
1011

1112

13+
@USERS.before_request
14+
@active_users_or_get_only
15+
def _(): ...
16+
17+
1218
@USERS.route("/users/<user_id>", methods=["PATCH"])
1319
def save_user(user_id):
1420
request_json = request.get_json()

pydatalab/tests/server/test_remotes.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,33 @@
22

33

44
@pytest.mark.dependency()
5-
def test_directories_list(client):
5+
def test_directories_list(client, unauthenticated_client, unverified_client, deactivated_client):
66
response = client.get("/list-remote-directories")
77
assert response.json
88
toplevel = response.json["data"][0]
99
assert toplevel["type"] == "toplevel"
1010
assert toplevel["status"] == "updated"
1111

12+
response = unauthenticated_client.get("/list-remote-directories")
13+
assert response.status_code == 401
14+
response = unverified_client.get("/list-remote-directories")
15+
assert response.status_code == 401
16+
response = deactivated_client.get("/list-remote-directories")
17+
assert response.status_code == 401
18+
1219
response = client.get("/remotes")
1320
assert response.json
1421
toplevel = response.json["data"][0]
1522
assert toplevel["type"] == "toplevel"
1623
assert toplevel["status"] == "cached"
1724

25+
response = unauthenticated_client.get("/remotes")
26+
assert response.status_code == 401
27+
response = unverified_client.get("/remotes")
28+
assert response.status_code == 401
29+
response = deactivated_client.get("/remotes")
30+
assert response.status_code == 401
31+
1832

1933
@pytest.mark.dependency(depends=["test_directories_list"])
2034
def test_single_directory(client):

0 commit comments

Comments
 (0)