Skip to content

Commit 2d1fe18

Browse files
Initial serial verification code, need to test
1 parent b555391 commit 2d1fe18

3 files changed

Lines changed: 32 additions & 21 deletions

File tree

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM python:3.11-buster
1+
FROM python:3.11-bullseye
22

33
LABEL maintainer="Penn Labs"
44

src/auth.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,26 @@
99
JWKS_URL = settings.JWKS_URL
1010

1111

12-
def get_jwk():
12+
def get_jwks():
1313
if settings.JWKS_CACHE:
14-
key = settings.JWKS_CACHE
15-
return key
14+
# Check to make sure we have a cached JWK for each key, otherwise refetch all.
15+
missing = False
16+
for key in settings.JWKS_URL.keys():
17+
if key not in settings.JWKS_CACHE:
18+
missing = True
19+
20+
if not missing:
21+
return settings.JWKS_CACHE
1622

1723
# Make a request to get the JWKS
18-
try:
19-
response = requests.get(JWKS_URL)
20-
jwks = jwk.JWKSet.from_json(response.text)
21-
settings.JWKS_CACHE = jwks
22-
except Exception as e:
23-
raise HTTPException(status_code=500, detail=str(e))
24-
24+
for key in settings.JWKS_URL:
25+
try:
26+
response = requests.get(JWKS_URL)
27+
jwks = jwk.JWKSet.from_json(response.text)
28+
settings.JWKS_CACHE[key] = jwks
29+
except Exception as e:
30+
del settings.JWKS_CACHE[key]
31+
raise HTTPException(status_code=500, detail=str(e))
2532
return settings.JWKS_CACHE
2633

2734

@@ -41,11 +48,12 @@ def get_token_from_header(request: Request):
4148

4249

4350
def verify_jwt(token: str = Depends(get_token_from_header)):
44-
try:
45-
# Load the public key
46-
public_key = get_jwk()
47-
# Decode and verify the JWT
48-
decoded_token = jwt.JWT(key=public_key, jwt=token)
49-
return decoded_token.claims
50-
except Exception as e:
51-
raise HTTPException(status_code=401, detail=str(e))
51+
public_keys = get_jwks()
52+
for key in public_keys:
53+
try:
54+
decoded_token = jwt.JWT(key=key, jwt=token)
55+
return decoded_token.claims
56+
except:
57+
pass
58+
59+
raise HTTPException(status_code=401, detail="Failed to verify JWT token against public keys array.")

src/config.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ class Config(BaseSettings):
1111
DATABASE_URL: PostgresDsn
1212
REDIS_URL: RedisDsn
1313

14-
JWKS_CACHE: JWKSet | None = None
15-
JWKS_URL: str = "https://platform.pennlabs.org/identity/jwks/"
14+
JWKS_CACHE: list[JWKSet] | None = None
15+
JWKS_URL: dict[str, str] = {
16+
"b2b":"https://platform.pennlabs.org/identity/jwks/",
17+
"user":"https://platform.pennlabs.org/accounts/.well-known/jwks.json"
18+
}
1619

1720
SITE_DOMAIN: str = "analytics.pennlabs.org"
1821

0 commit comments

Comments
 (0)