This demo brings together Amazon Cognito authentication with S3 resource access, showing how a Delphi app can serve different content to guest users and authenticated users. It's the most involved sample here, with a CloudFormation stack, WebView2-based sign-in, and identity pool credential resolution.
TCognitoHostedUIcomponent handles the OAuth sign-in/sign-out flow via Cognito's Hosted UI, rendered in an embedded WebView2 browserTCognitoAWSCredentialsresolves AWS credentials from Cognito identity tokens, supporting both authenticated and unauthenticated (guest) flowsTS3Clientwith Cognito credentials shows how to pass Cognito-resolved credentials to any AWS service client
┌─────────────────────┐
│ Cognito User Pool │
│ (Hosted UI) │
└────────┬────────────┘
│ Identity Token
v
┌──────────────┐ ┌─────────────────────┐ ┌──────────┐
│ Guest User │───>│ TCognitoAWSCreds │───>│ S3Client │──> Guest Bucket
│ (no token) │ │ (Identity Pool) │ │ │
└──────────────┘ └─────────────────────┘ └──────────┘
│
┌──────────────┐ │ ┌──────────┐
│ Signed-in │─── AddLogin ──────────────────>│ S3Client │──> Known User Bucket
│ User │ │ │
└──────────────┘ └──────────┘
Without a token, TCognitoAWSCredentials resolves unauthenticated (guest)
credentials, scoped to whatever the guest IAM role allows. After sign-in, the
identity token is added via Credentials.AddLogin, and the credentials switch
to the authenticated IAM role with access to different resources.
// Create Cognito-based AWS credentials
Credentials := TCognitoAWSCredentials.Create(IdentityPoolId, Region);
// Pass them to any AWS service client
LS3Options := TS3Options.Create;
LS3Options.Credentials := Credentials;
S3Client := TS3Client.Create(LS3Options);
// After authentication, upgrade to authenticated credentials
Credentials.AddLogin(ProviderName, AuthForm.IdToken);- AWS Account
- Embarcadero RAD Studio / Delphi
- Appercept AWS SDK for Delphi
- EdgeView2 SDK
AWS resources need to be deployed before running the sample.
- Sign in to the AWS Console.
- Navigate to the AWS CloudFormation Console.
- Create a stack based on the
CloudFormation/IdentityProvider.ymltemplate. Specify a "Stack name" and accept all defaults. - Upload
Templates/guests.htmlto theGuestBucketcreated by the CloudFormation template and rename it toindex.html. - Upload
Templates/known_users.htmlto theKnownUserBucketcreated by the CloudFormation template and rename it toindex.html.
Once the AWS resources are deployed:
- Open
Source\CognitoDemo.dproj. - The constants defined in
Settings.pascorrespond to the outputs published by the CloudFormation stack. Copy the values from the CloudFormation console. - Build the project (don't run it yet).
- Copy
WebView2Loader.dllinto the build directory. - Run the project.
The resources used in this demo have costs but are minimal. Some services have a "free tier" so this demonstration would not incur fees for some of the resources used. For detailed explanation of potential costs refer to Amazon's pricing guides for the relevant services:
After running the demo you will want to clean up the resources created to save any potential ongoing costs:
- Navigate to the Amazon S3 console.
- Empty the buckets created as a part of the CloudFormation stack. If you're unsure of the buckets created, refer to the "Resources" tab on the CloudFormation stack details.
- Delete the CloudFormation stack.


