Skip to content

Commit 0392139

Browse files
committed
Add support for API Key to token
1 parent d82d46d commit 0392139

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

tests/test_iam.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
7/31/25
1515
"""
1616
import pytest
17+
from iam_lib.api.api_key import ApiKeyClient
1718

1819
from config import Config
1920
from edi.iam import IAM
@@ -32,6 +33,18 @@ def edi_token_client():
3233
truststore="/etc/ssl/certs/ca-certificates.crt"
3334
)
3435

36+
@pytest.fixture
37+
def api_key_client():
38+
return ApiKeyClient(
39+
scheme="https",
40+
host="127.0.0.1:5443",
41+
accept="json",
42+
public_key_path="/home/pasta/git/iam-lib/tests/data/public_key.pem",
43+
algorithm="ES256",
44+
token=None,
45+
truststore="/etc/ssl/certs/ca-certificates.crt"
46+
)
47+
3548
@pytest.mark.asyncio
3649
async def test_create_token():
3750
iam = IAM()
@@ -40,7 +53,13 @@ async def test_create_token():
4053

4154
def test_iam_lib_create_token(edi_token_client: EdiTokenClient):
4255
edi_token_response = edi_token_client.create_token(profile_edi_identifier=Config.PUBLIC_ID, key=Config.AUTH_KEY)
43-
edi_token = edi_token_response["token"]
56+
edi_token = edi_token_response["edi-token"]
4457
assert edi_token is not None
45-
token = Token(edi_token_response["token"])
58+
token = Token(edi_token)
4659
token.validate(public_key_path=Config.AUTH_PUBLIC_KEY, algorithm=Config.JWT_ALGORITHM),
60+
61+
def test_iam_lib_key_to_token(api_key_client: ApiKeyClient):
62+
api_key_response = api_key_client.key_to_token(key="pddKCt7H_pODw-k0ExVpNmGhLO8")
63+
# auth_token = api_key_response["auth-token"]
64+
edi_token = api_key_response["edi-token"]
65+
assert edi_token is not None

webapp/auth/authenticate.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import daiquiri
1919
import httpx
20+
from iam_lib.api.api_key import ApiKeyClient
2021
from iam_lib.api.edi_token import EdiTokenClient
2122
from iam_lib.exceptions import IAMResponseError, IAMInvalidToken
2223
import ssl
@@ -50,11 +51,28 @@ async def authenticate(request: Request) -> tuple:
5051
truststore=Config.CA_FILE
5152
)
5253

54+
api_key_client = ApiKeyClient(
55+
scheme=Config.AUTH_SCHEME,
56+
host=Config.AUTH_HOST,
57+
accept=Config.ACCEPT_TYPE,
58+
public_key_path=Config.AUTH_PUBLIC_KEY,
59+
algorithm=Config.JWT_ALGORITHM,
60+
token=None,
61+
truststore=Config.CA_FILE
62+
63+
)
64+
5365
if ((auth_token is None) and (edi_token is not None)) or ((auth_token is not None) and (edi_token is None)):
5466
msg = "EDI token and PASTA token must be present together"
5567
raise InvalidTokenException(msg, status.HTTP_400_BAD_REQUEST)
5668
elif auth_token is None and edi_token is None:
57-
if "authorization" in request.headers:
69+
if "key" in request.query_params:
70+
key = request.query_params["key"]
71+
api_key_response = api_key_client.key_to_token(key=key)
72+
auth_token = api_key_response["pasta-token"]
73+
pasta_token.from_auth_token(auth_token)
74+
edi_token = api_key_response["edi-token"]
75+
elif "authorization" in request.headers:
5876
basic_auth = request.headers["authorization"]
5977
auth_token, edi_token = await ldap_authenticate(basic_auth)
6078
pasta_token.from_auth_token(auth_token)

0 commit comments

Comments
 (0)