Skip to content

Commit 7b365fc

Browse files
committed
Add Public EDI TOKEN for all public requests (#13)
1 parent b163cf5 commit 7b365fc

4 files changed

Lines changed: 111 additions & 0 deletions

File tree

tests/test_iam.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
:Mod:
6+
test_iam
7+
8+
:Synopsis:
9+
10+
:Author:
11+
pasta
12+
13+
:Created:
14+
7/31/25
15+
"""
16+
import pytest
17+
18+
from config import Config
19+
from edi.iam import IAM
20+
21+
22+
@pytest.mark.asyncio
23+
async def test_create_token():
24+
iam = IAM()
25+
edi_token = await iam.create_token(Config.PUBLIC_ID)
26+
assert edi_token is not None

webapp/auth/authenticate.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from auth.pasta_token import PastaToken
3131
import auth.pasta_crypto as pasta_crypto
3232
from config import Config
33+
from edi.iam import IAM
3334

3435

3536
logger = daiquiri.getLogger(__name__)
@@ -38,6 +39,7 @@
3839
async def authenticate(request: Request) -> tuple:
3940
pasta_token = PastaToken()
4041
edi_token = None
42+
is_public = False;
4143

4244
# Old-style PASTA authentication
4345
if "authorization" in request.headers:
@@ -60,6 +62,8 @@ async def authenticate(request: Request) -> tuple:
6062
else:
6163
pasta_token.uid = Config.PUBLIC
6264
pasta_token.system = Config.SYSTEM
65+
is_public = True
66+
6367
msg = f"Authentication for user: '{pasta_token.to_string()}'"
6468
logger.info(msg)
6569

@@ -68,6 +72,9 @@ async def authenticate(request: Request) -> tuple:
6872
edi_token = request.cookies.get("edi-token")
6973
msg = f"EDI Token '{edi_token}' exists"
7074
logger.info(msg)
75+
elif is_public:
76+
iam = IAM()
77+
edi_token = await iam.create_token(Config.PUBLIC_ID)
7178

7279
return pasta_token, edi_token
7380

webapp/edi/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
:Mod:
6+
__init__.py
7+
8+
:Synopsis:
9+
10+
:Author:
11+
pasta
12+
13+
:Created:
14+
7/31/25
15+
"""

webapp/edi/iam.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
:Mod:
6+
iam
7+
8+
:Synopsis:
9+
10+
:Author:
11+
Mark Servilla
12+
13+
:Created:
14+
7/31/25
15+
"""
16+
import json
17+
from pathlib import Path
18+
19+
import daiquiri
20+
import httpx
21+
import ssl
22+
23+
from config import Config
24+
25+
26+
logger = daiquiri.getLogger(__name__)
27+
28+
29+
30+
class IAM:
31+
32+
def __init__(self):
33+
self.base_url = Config.AUTH
34+
35+
async def create_token(self, edi_id: str) -> str:
36+
route = f"/auth/v1/token/{edi_id}"
37+
url = self.base_url + route
38+
data = {
39+
"key": Config.AUTH_KEY
40+
}
41+
42+
verify = True
43+
if Path(str(Config.CA_FILE)).exists() and Path(str(Config.CA_FILE)).is_file():
44+
# Create local SSL CA context if Config.CA_FILE is valid path
45+
verify = ssl.create_default_context(cafile=Config.CA_FILE)
46+
else:
47+
msg = f"Truststore file '{Config.CA_FILE}' does not exist"
48+
logger.error(msg)
49+
try:
50+
response = httpx.post(url, json=data, verify=verify)
51+
response.raise_for_status()
52+
except httpx.HTTPError as ex:
53+
logger.error(ex)
54+
raise ex
55+
56+
payload = response.json()
57+
try:
58+
edi_token = payload["token"]
59+
except KeyError as ex:
60+
logger.error(ex)
61+
edi_token = None
62+
63+
return edi_token

0 commit comments

Comments
 (0)