Skip to content

Commit 75a38a0

Browse files
authored
Merge pull request #222 from vechain/mike/fix-sam-deployment
Fix Get Embedded Wallet Details SAM deployment
2 parents c0d5fac + 524e202 commit 75a38a0

File tree

4 files changed

+53
-65
lines changed

4 files changed

+53
-65
lines changed

.github/workflows/deploy-api-lambda.yaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Deploy Get Embedded Wallet Details Lambda
1+
name: Deploy Embedded Wallet Details Stack
22

33
on:
44
push:
@@ -36,9 +36,8 @@ jobs:
3636
cd lambda
3737
sam build
3838
sam deploy \
39-
--stack-name GetEmbeddedWalletDetailsLambda \
39+
--stack-name EmbeddedWalletStack \
4040
--region ${{ env.AWS_REGION }} \
4141
--capabilities CAPABILITY_IAM \
42+
--no-fail-on-empty-changeset \
4243
--parameter-overrides "PrivyAppId=${{ secrets.NEXT_PUBLIC_PRIVY_APP_ID }} PrivyAppSecret=${{ secrets.NEXT_PUBLIC_PRIVY_APP_SECRET }}"
43-
44-

lambda/index.ts

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
22
import { PrivyClient, User } from '@privy-io/server-auth';
33

4-
// Define types for social media usernames
5-
type SocialUsernames = {
4+
// Define types for all identifiers
5+
type UserIdentifiers = {
6+
email?: string;
67
google?: string;
78
apple?: string;
89
twitter?: string;
@@ -13,14 +14,9 @@ type SocialUsernames = {
1314
linkedin?: string;
1415
};
1516

16-
// Define the user identifiers interface
17-
interface UserIdentifiers {
18-
email?: string;
19-
usernames: SocialUsernames;
20-
}
21-
2217
// Define social account types for type safety
23-
type SocialAccountType =
18+
type SocialAccountType =
19+
| 'email'
2420
| 'google_oauth'
2521
| 'apple_oauth'
2622
| 'twitter_oauth'
@@ -30,49 +26,38 @@ type SocialAccountType =
3026
| 'instagram_oauth'
3127
| 'linkedin_oauth';
3228

33-
// Map social account types to their username field names
34-
const SOCIAL_ACCOUNT_MAPPINGS: Record<SocialAccountType, keyof SocialUsernames> = {
35-
'google_oauth': 'google',
36-
'apple_oauth': 'apple',
37-
'twitter_oauth': 'twitter',
38-
'discord_oauth': 'discord',
39-
'github_oauth': 'github',
40-
'telegram': 'telegram',
41-
'instagram_oauth': 'instagram',
42-
'linkedin_oauth': 'linkedin'
43-
};
44-
4529
/**
4630
* Extracts user identifiers (email and social media usernames) from a Privy user
4731
* @param user - The Privy user object
48-
* @returns UserIdentifiers object containing email and social media usernames
32+
* @returns UserIdentifiers object containing all identifiers
4933
*/
5034
function getUserIdentifiers(user: User): UserIdentifiers {
51-
const identifiers: UserIdentifiers = {
52-
usernames: {}
53-
};
54-
35+
const identifiers: UserIdentifiers = {};
36+
5537
// Get email if available
5638
if (user.email?.address) {
57-
identifiers.email = user.email.address;
39+
identifiers.email = user.email.address;
5840
}
59-
41+
6042
// Get usernames from linked accounts
6143
user.linkedAccounts.forEach(account => {
6244
const accountType = account.type as SocialAccountType;
63-
const usernameField = SOCIAL_ACCOUNT_MAPPINGS[accountType];
45+
// Remove '_oauth' suffix to get the field name
46+
const field = accountType.replace('_oauth', '') as keyof UserIdentifiers;
6447

65-
if (usernameField) {
66-
// Handle email-based accounts (google, apple)
67-
if (accountType === 'google_oauth' || accountType === 'apple_oauth' || accountType === 'linkedin_oauth') {
68-
const emailAccount = account as { email?: string };
69-
identifiers.usernames[usernameField] = emailAccount.email || undefined;
70-
}
71-
// Handle username-based accounts
72-
else {
73-
const usernameAccount = account as { username?: string };
74-
identifiers.usernames[usernameField] = usernameAccount.username || undefined;
48+
// Handle email-based accounts (email, google, apple, linkedin)
49+
if (accountType === 'email' || accountType === 'google_oauth' || accountType === 'apple_oauth' || accountType === 'linkedin_oauth') {
50+
const emailAccount = account as { email?: string } | { address?: string };
51+
if ('email' in emailAccount) {
52+
identifiers[field] = emailAccount.email;
53+
} else if ('address' in emailAccount) {
54+
identifiers[field] = emailAccount.address;
7555
}
56+
}
57+
// Handle username-based accounts
58+
else {
59+
const usernameAccount = account as { username?: string };
60+
identifiers[field] = usernameAccount.username || undefined;
7661
}
7762
});
7863
return identifiers;

lambda/samconfig.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
version = 0.1
44

55
[default.global.parameters]
6-
stack_name = "get-embedded-wallet-details"
6+
stack_name = "EmbeddedWalletStack"
77

88
[default.build.parameters]
99
cached = true
@@ -14,7 +14,7 @@ lint = true
1414

1515
[default.deploy.parameters]
1616
capabilities = "CAPABILITY_IAM"
17-
confirm_changeset = true
17+
confirm_changeset = false
1818
resolve_s3 = true
1919

2020
[default.package.parameters]

lambda/template.yaml

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Resources:
2929
GetEmbeddedWalletDetails:
3030
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
3131
Properties:
32-
Path: /embedded_wallet/{walletAddress}/details
32+
Path: /v1/embedded_wallet/{walletAddress}/details
3333
Method: get
3434
Auth:
3535
ApiKeyRequired: true
@@ -50,15 +50,28 @@ Resources:
5050
Enabled: true
5151
StageKeys:
5252
- RestApiId: !Ref ServerlessRestApi
53-
StageName: Prod
54-
UsagePlan:
55-
CreateUsagePlan: PER_REQUEST
56-
Quota:
57-
Limit: 5000
58-
Period: MONTH
59-
Throttle:
60-
BurstLimit: 200
61-
RateLimit: 100
53+
StageName: !Ref ServerlessRestApiProdStage
54+
55+
UsagePlan:
56+
Type: AWS::ApiGateway::UsagePlan
57+
Properties:
58+
UsagePlanName: !Sub ${AWS::StackName}-UsagePlan
59+
ApiStages:
60+
- ApiId: !Ref ServerlessRestApi
61+
Stage: !Ref ServerlessRestApiProdStage
62+
Quota:
63+
Limit: 5000
64+
Period: MONTH
65+
Throttle:
66+
BurstLimit: 200
67+
RateLimit: 100
68+
69+
UsagePlanKey:
70+
Type: AWS::ApiGateway::UsagePlanKey
71+
Properties:
72+
KeyId: !Ref ApiKey
73+
KeyType: API_KEY
74+
UsagePlanId: !Ref UsagePlan
6275

6376
Parameters:
6477
PrivyAppId:
@@ -76,13 +89,4 @@ Outputs:
7689
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
7790
GetEmbeddedWalletDetailsApi:
7891
Description: API Gateway endpoint URL for Prod stage for Get Embedded Wallet Details
79-
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/embedded_wallet/{walletAddress}/details"
80-
GetEmbeddedWalletDetailsFunction:
81-
Description: Get Embedded Wallet Details Lambda Function ARN
82-
Value: !GetAtt GetEmbeddedWalletDetailsFunction.Arn
83-
GetEmbeddedWalletDetailsFunctionIamRole:
84-
Description: Implicit IAM Role created for Get Embedded Wallet Details
85-
Value: !GetAtt GetEmbeddedWalletDetailsFunctionRole.Arn
86-
ApiKey:
87-
Description: API Key for accessing the API
88-
Value: !Ref ApiKey
92+
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/v1/embedded_wallet/{walletAddress}/details"

0 commit comments

Comments
 (0)