Skip to content

Commit fcafc51

Browse files
committed
feat: add new config parameter to disable extension
A new boolean parameter added that will dislable the @auth_required decorator. This is useful for local development or testing, to prevent the need for a JWT cookie from Cognito.
1 parent 1af4d24 commit fcafc51

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

src/flask_cognito_lib/config.py

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ class Config:
4949
CONTEXT_KEY_TOKEN_SERVICE = "aws_jwt_service"
5050
COOKIE_NAME = "cognito_access_token"
5151

52+
@property
53+
def disabled(self) -> bool:
54+
"""Return True if Cognito Authentication is disabled"""
55+
return get("AWS_COGNITO_DISABLED", required=False, default=False)
56+
5257
@property
5358
def user_pool_id(self) -> str:
5459
"""Return the Cognito user pool ID"""

src/flask_cognito_lib/decorators.py

+3
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ def auth_required(groups: Optional[Iterable[str]] = None):
149149
def wrapper(fn):
150150
@wraps(fn)
151151
def decorator(*args, **kwargs):
152+
# return early if the extension is disabled
153+
if cfg.disabled:
154+
return fn(*args, **kwargs)
152155

153156
# Try and validate the access token stored in the cookie
154157
try:

tests/test_config.py

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ def test_missing_config(app, cfg):
1717
print(cfg.region)
1818

1919

20+
def test_disabled(cfg):
21+
"""Check if extension is enabled (by default it should be)"""
22+
assert not cfg.disabled
23+
24+
2025
def test_issuer(cfg):
2126
"""Check if forms the issuer URL correctly"""
2227
expected = "https://cognito-idp.eu-west-1.amazonaws.com/eu-west-1_c7O90SNDF"

tests/test_decorators.py

+8
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,11 @@ def test_auth_required_groups_invalid(client_with_cookie):
128128
# 403 as the token isn't in this group
129129
response = client_with_cookie.get("/invalid_group")
130130
assert response.status_code == 403
131+
132+
133+
def test_auth_required_extension_dislabled(client, app):
134+
# Return page with 200 OK if the extension is disabled (bypass Cognito)
135+
app.config["AWS_COGNITO_DISABLED"] = True
136+
response = client.get("/private")
137+
assert response.status_code == 200
138+
assert response.data.decode("utf-8") == "ok"

0 commit comments

Comments
 (0)