-
Notifications
You must be signed in to change notification settings - Fork 2
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
Seatool indexes and api authorization pattern #46
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b26be1b
update dynamo gsis and add lambda authenticator - wip
benjaminpaige bca6fb4
add api auth - wip
benjaminpaige c3b7ffa
update types
benjaminpaige 5c2a004
remove authorizer
benjaminpaige f0017d2
refactor
benjaminpaige e9e4a41
refactor more
benjaminpaige f22289b
refactor more more
benjaminpaige 097d04a
update types
benjaminpaige 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type ReactQueryApiError = { | ||
response: { data: { message: string } }; | ||
}; |
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,4 @@ | ||
export * from "./issue"; | ||
export * from "./user"; | ||
export * from "./seatoolData"; | ||
export * from "./errors"; |
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,6 @@ | ||
export type SeatoolData = { | ||
ID: string; | ||
SUBMISSION_DATE: number; | ||
PLAN_TYPE: string; // should be enum | ||
STATE_CODE: string; // should be enum | ||
}; |
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,9 +1,9 @@ | ||
export interface CognitoUserAttributes { | ||
export type CognitoUserAttributes = { | ||
sub: string; | ||
"custom:roles": string[]; | ||
email_verified: boolean; | ||
"custom:state_codes": string[]; | ||
given_name: string; | ||
family_name: string; | ||
email: string; | ||
} | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { | ||
CognitoIdentityProviderClient, | ||
ListUsersCommand, | ||
UserType as CognitoUserType, | ||
} from "@aws-sdk/client-cognito-identity-provider"; | ||
import { CognitoUserAttributes } from "shared-types"; | ||
import { APIGatewayEvent } from "aws-lambda"; | ||
|
||
// Retrieve user authentication details from the APIGatewayEvent | ||
export function getAuthDetails(event: APIGatewayEvent) { | ||
const authProvider = | ||
event.requestContext.identity.cognitoAuthenticationProvider; | ||
const parts = authProvider.split(":"); | ||
const userPoolIdParts = parts[parts.length - 3].split("/"); | ||
const userPoolId = userPoolIdParts[userPoolIdParts.length - 1]; | ||
const userPoolUserId = parts[parts.length - 1]; | ||
|
||
return { userId: userPoolUserId, poolId: userPoolId }; | ||
} | ||
|
||
// Convert Cognito user attributes to a dictionary format | ||
function userAttrDict(cognitoUser: CognitoUserType): CognitoUserAttributes { | ||
const attributes = {}; | ||
|
||
if (cognitoUser.Attributes) { | ||
cognitoUser.Attributes.forEach((attribute) => { | ||
if (attribute.Value && attribute.Name) { | ||
attributes[attribute.Name] = attribute.Value; | ||
} | ||
}); | ||
} | ||
|
||
return attributes as CognitoUserAttributes; | ||
} | ||
|
||
// Parse object values as JSON if possible | ||
export const getParsedObject = (obj: CognitoUserAttributes) => | ||
Object.fromEntries( | ||
Object.entries(obj).map(([key, value]) => { | ||
try { | ||
return [key, JSON.parse(value as string)]; | ||
} catch (error) { | ||
return [key, value]; | ||
} | ||
}) | ||
); | ||
|
||
// Retrieve and parse user attributes from Cognito using the provided userId and poolId | ||
export async function lookupUserAttributes( | ||
userId: string, | ||
poolId: string | ||
): Promise<CognitoUserAttributes> { | ||
const fetchResult = await fetchUserFromCognito(userId, poolId); | ||
|
||
if (fetchResult instanceof Error) { | ||
throw fetchResult; | ||
} | ||
|
||
const currentUser = fetchResult as CognitoUserType; | ||
const attributes = userAttrDict(currentUser); | ||
|
||
return getParsedObject(attributes) as CognitoUserAttributes; | ||
} | ||
|
||
// Fetch user data from Cognito based on the provided userId and poolId | ||
async function fetchUserFromCognito( | ||
userID: string, | ||
poolID: string | ||
): Promise<CognitoUserType | Error> { | ||
const cognitoClient = new CognitoIdentityProviderClient({ | ||
region: process.env.region, | ||
}); | ||
|
||
const subFilter = `sub = "${userID}"`; | ||
|
||
const commandListUsers = new ListUsersCommand({ | ||
UserPoolId: poolID, | ||
Filter: subFilter, | ||
}); | ||
|
||
try { | ||
const listUsersResponse = await cognitoClient.send(commandListUsers); | ||
|
||
if ( | ||
listUsersResponse.Users === undefined || | ||
listUsersResponse.Users.length !== 1 | ||
) { | ||
throw new Error("No user found with this sub"); | ||
} | ||
|
||
const currentUser = listUsersResponse.Users[0]; | ||
return currentUser; | ||
} catch (error) { | ||
throw new Error("Error fetching user from Cognito"); | ||
} | ||
} |
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
Oops, something went wrong.
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.
@benjaminpaige I thought what you demo'd was using an authorizer, which would replace the aws_iam protection. Did I misunderstand? Either way this is great