File size: 1,044 Bytes
02109ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from botocore.client import BaseClient
from botocore.exceptions import BotoCoreError, ClientError
from dotenv import dotenv_values
from fastapi import UploadFile

from app.core.aws import get_session

_PATH_ENV = "cognito.env"


class Cognito:
    @staticmethod
    def client(file: UploadFile) -> BaseClient:
        session = get_session(_PATH_ENV, file)
        return session.client("cognito-idp")

    @staticmethod
    def auth(client: BaseClient, username: str, password: str):
        config = dotenv_values(dotenv_path=_PATH_ENV)
        try:
            return client.initiate_auth(
                ClientId=config["AWS_CLIENT_ID_COGNITO"],
                AuthFlow='USER_PASSWORD_AUTH',
                AuthParameters={
                    'USERNAME': username,
                    'PASSWORD': password
                },
            )
        except (BotoCoreError, ClientError) as error:
            if isinstance(error, ClientError):
                return error.response
            else:
                return {"Error": error}