-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added SSO to retrieve credentials for calling Cognito related queries #41
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5ed9dc0
WIP: added boto3 to query cognito
gcarvellas 8608c8d
Removed env variables and added dynamic credential retrieval using AW…
KevinHa48 5b4f34e
integrated cognito auth into contract post
gcarvellas 002860b
merged main
gcarvellas 68a6633
removed unnecessary TODO
gcarvellas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .users import UsersDB | ||
from .contracts import ContractsDB | ||
from .contracts import ContractsDB | ||
from .cognito import CognitoIdentityProviderWrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# https://docs.aws.amazon.com/code-library/latest/ug/python_3_cognito-identity-provider_code_examples.html | ||
|
||
from botocore.exceptions import ClientError | ||
import logging | ||
import boto3 | ||
from config.env import COGNITO_USERPOOL_ID | ||
from utilities.types import JSONDict | ||
from distutils.util import strtobool | ||
|
||
|
||
class CognitoUser: | ||
|
||
def __init__(self, cognito_response_json: JSONDict) -> None: | ||
assert cognito_response_json.get("UserAttributes") | ||
for attribute_dict in cognito_response_json['UserAttributes']: | ||
assert "Name" in attribute_dict | ||
assert attribute_dict.get("Value") | ||
match attribute_dict['Name']: | ||
case 'sub': | ||
self.sub: str = attribute_dict['Value'] | ||
case 'email_verified': | ||
self.email_verified: bool = bool(strtobool(attribute_dict['Value'])) | ||
case 'email': | ||
self.email: str = attribute_dict['Value'] | ||
assert self.sub is not None | ||
assert self.email_verified is not None | ||
assert self.email is not None | ||
|
||
|
||
class CognitoIdentityProviderWrapper: | ||
"""Encapsulates Amazon Cognito actions""" | ||
def __init__(self) -> None: | ||
self.cognito_idp_client = boto3.client('cognito-idp') | ||
|
||
def get_user(self, username: str) -> CognitoUser: | ||
""" | ||
Gets a user in Cognito by it's username. | ||
|
||
:return: user | ||
""" | ||
try: | ||
response: JSONDict = self.cognito_idp_client.admin_get_user(UserPoolId=COGNITO_USERPOOL_ID, Username=username) | ||
assert type(response) is dict | ||
return CognitoUser(response) | ||
except ClientError as err: | ||
logging.error( | ||
"Couldn't list users for %s. Here's why: %s: %s", COGNITO_USERPOOL_ID, | ||
err.response['Error']['Code'], err.response['Error']['Message']) | ||
raise err |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
from database import UsersDB | ||
from utilities import NoApproverException | ||
from typing import List | ||
from database import CognitoIdentityProviderWrapper | ||
|
||
|
||
class ContractManager(): | ||
|
@@ -28,11 +29,14 @@ async def create_contract( | |
approver = await UsersDB.get_random_artist_reviewer() | ||
if approver is None: | ||
raise NoApproverException() | ||
# TODO cannot do this to get approver. Need to grab from cognito DB | ||
# approver_email = approver.get("email") | ||
# approver_name = approver.get('name') | ||
approver_email = "[email protected]" | ||
approver_name = "test" | ||
|
||
# Get the email and username from CognitoDB | ||
assert "username" in approver | ||
approver_name = approver["username"] | ||
approver_cognito_data = CognitoIdentityProviderWrapper().get_user(approver_name) | ||
approver_email = approver_cognito_data.email | ||
|
||
# TODO the approver_name should be the user's name, not username | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
assert type(approver_email) is str, "Expected a string for approver email" | ||
assert type(approver_name) is str, "Expected a string for approver name" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
castlepointanime/portal#12