Jhon Alexander Alvarez Casas commited on
Commit
02109ec
·
1 Parent(s): 6566500

update logic and new method for auth_cognito

Browse files
app/core/aws.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import boto3
2
+ from dotenv import dotenv_values
3
+ from fastapi import UploadFile
4
+
5
+ from app.util.file_util import write_file_env
6
+
7
+
8
+ def _create_session(config: dict):
9
+ return boto3.Session(
10
+ aws_access_key_id=config['AWS_ACCESS_KEY_ID'],
11
+ aws_secret_access_key=config['AWS_SECRET_ACCESS_KEY'],
12
+ aws_session_token=config['AWS_SESSION_TOKEN'],
13
+ region_name=config['AWS_REGION'],
14
+ )
15
+
16
+
17
+ def get_session(path: str, file: UploadFile):
18
+ write_file_env(path, file)
19
+ config = dotenv_values(dotenv_path=path)
20
+ return _create_session(config)
app/core/cognito.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from botocore.client import BaseClient
2
+ from botocore.exceptions import BotoCoreError, ClientError
3
+ from dotenv import dotenv_values
4
+ from fastapi import UploadFile
5
+
6
+ from app.core.aws import get_session
7
+
8
+ _PATH_ENV = "cognito.env"
9
+
10
+
11
+ class Cognito:
12
+ @staticmethod
13
+ def client(file: UploadFile) -> BaseClient:
14
+ session = get_session(_PATH_ENV, file)
15
+ return session.client("cognito-idp")
16
+
17
+ @staticmethod
18
+ def auth(client: BaseClient, username: str, password: str):
19
+ config = dotenv_values(dotenv_path=_PATH_ENV)
20
+ try:
21
+ return client.initiate_auth(
22
+ ClientId=config["AWS_CLIENT_ID_COGNITO"],
23
+ AuthFlow='USER_PASSWORD_AUTH',
24
+ AuthParameters={
25
+ 'USERNAME': username,
26
+ 'PASSWORD': password
27
+ },
28
+ )
29
+ except (BotoCoreError, ClientError) as error:
30
+ if isinstance(error, ClientError):
31
+ return error.response
32
+ else:
33
+ return {"Error": error}
app/core/s3.py CHANGED
@@ -1,22 +1,12 @@
1
- import os
2
- import shutil
3
-
4
- import boto3
5
- from dotenv import load_dotenv
6
  from fastapi import UploadFile
7
 
 
 
 
 
8
 
9
- def s3(file: UploadFile):
10
- env_path = "s3.env"
11
- if os.path.isfile(env_path):
12
- os.remove(env_path)
13
- with open(env_path, 'w+b') as f:
14
- shutil.copyfileobj(file.file, f)
15
- load_dotenv(dotenv_path=env_path)
16
- session = boto3.Session(
17
- aws_access_key_id=os.getenv('AWS_ACCESS_KEY_ID'),
18
- aws_secret_access_key=os.getenv('AWS_SECRET_ACCESS_KEY'),
19
- aws_session_token=os.getenv('AWS_SESSION_TOKEN'),
20
- region_name='us-east-1'
21
- )
22
- return session.client("s3")
 
 
 
 
 
 
1
  from fastapi import UploadFile
2
 
3
+ from app.core.aws import get_session
4
+
5
+ _PATH_ENV = "s3.env"
6
+
7
 
8
+ class S3:
9
+ @staticmethod
10
+ def client(file: UploadFile):
11
+ session = get_session(_PATH_ENV, file)
12
+ return session.client("s3")
 
 
 
 
 
 
 
 
 
app/model/credentils.py DELETED
@@ -1,7 +0,0 @@
1
- from pydantic import BaseModel
2
-
3
-
4
- class Credentials(BaseModel):
5
- aws_access_key_id: str
6
- aws_secret_access_key: str
7
- aws_session_token: str
 
 
 
 
 
 
 
 
app/util/file_util.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ import shutil
2
+
3
+ from fastapi import UploadFile
4
+
5
+
6
+ def write_file_env(path: str, file: UploadFile):
7
+ # if os.path.isfile(env_path):
8
+ # os.remove(env_path)
9
+ with open(path, 'w+b') as f:
10
+ shutil.copyfileobj(file.file, f)
main.py CHANGED
@@ -1,9 +1,11 @@
1
  import os
 
2
 
3
- from fastapi import FastAPI, File, UploadFile
4
  from ttp import ttp
5
 
6
- from app.core.s3 import s3
 
7
  from app.core.supabase import supabase_client
8
  from app.core.template import Template
9
 
@@ -11,20 +13,30 @@ app = FastAPI()
11
 
12
 
13
  @app.post("/data")
14
- def data(bucket: str, project: str, local_date: str, env_file: UploadFile = File(...)):
 
 
 
 
 
15
  server_folder = f"{project}/{local_date}"
16
  local_folder = f"data/{server_folder}"
17
- response = s3(env_file).list_objects_v2(Bucket=bucket, Prefix=server_folder)
 
18
  if not os.path.exists(local_folder):
19
  os.makedirs(local_folder)
20
  with open(f"{local_folder}/data.txt", "ab") as f:
21
  for obj in response.get('Contents', []):
22
- s3(env_file).download_fileobj(bucket, obj['Key'], f)
23
  return {"message": "success"}
24
 
25
 
26
  @app.get("/data")
27
- async def data(project: str, local_date: str, process: str):
 
 
 
 
28
  local_folder = f"data/{project}/{local_date}"
29
  template = Template.read(process)
30
  values = {"project": project, "local_date": local_date, "process": process}
@@ -41,3 +53,18 @@ async def data(project: str, local_date: str, process: str):
41
  except Exception as e:
42
  print(f"error desde {i}: {e}")
43
  return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
+ from typing import Annotated
3
 
4
+ from fastapi import FastAPI, File, UploadFile, Form
5
  from ttp import ttp
6
 
7
+ from app.core.cognito import Cognito
8
+ from app.core.s3 import S3
9
  from app.core.supabase import supabase_client
10
  from app.core.template import Template
11
 
 
13
 
14
 
15
  @app.post("/data")
16
+ def data(
17
+ bucket: str,
18
+ project: str,
19
+ local_date: str,
20
+ env_file: UploadFile = File(...)
21
+ ):
22
  server_folder = f"{project}/{local_date}"
23
  local_folder = f"data/{server_folder}"
24
+ s3 = S3.client(file=env_file)
25
+ response = s3.list_objects_v2(Bucket=bucket, Prefix=server_folder)
26
  if not os.path.exists(local_folder):
27
  os.makedirs(local_folder)
28
  with open(f"{local_folder}/data.txt", "ab") as f:
29
  for obj in response.get('Contents', []):
30
+ s3.download_fileobj(bucket, obj['Key'], f)
31
  return {"message": "success"}
32
 
33
 
34
  @app.get("/data")
35
+ def data(
36
+ project: str,
37
+ local_date: str,
38
+ process: str
39
+ ):
40
  local_folder = f"data/{project}/{local_date}"
41
  template = Template.read(process)
42
  values = {"project": project, "local_date": local_date, "process": process}
 
53
  except Exception as e:
54
  print(f"error desde {i}: {e}")
55
  return result
56
+
57
+
58
+ @app.post("/auth")
59
+ def auth(
60
+ username: Annotated[str, Form()],
61
+ password: Annotated[str, Form()],
62
+ env_file: UploadFile = File(...)
63
+ ):
64
+ cognito = Cognito.client(file=env_file)
65
+ response = Cognito.auth(
66
+ client=cognito,
67
+ username=username,
68
+ password=password
69
+ )
70
+ return response
requirements.txt CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:38a92b404f75f1646f77b70e17561f1ebd7802b72eba7f013cb40bb85ecc006e
3
- size 144
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bea22f86f1eca5015436c9ac7ba1d4dd13e93b594111917741be549cd7dc8376
3
+ size 163