Allow unauthenticated guest access to your application using Amazon Cognito
You can use Amazon Cognito authentication as an alternative to directly using AWS Identity and Access Management (IAM) with both front end SDKs and direct HTTPS requests.
You may want to use this form of authentication for the following reasons:
-
Unauthenticated users – If you have a website with anonymous users, you can use Amazon Cognito identity pools. For more information, see the section on Allow unauthenticated guest access to your application using Amazon Cognito.
-
Your own authentication – If you would like to use your own authentication process, or combine multiple authentication methods, you can use Amazon Cognito Federated Identities. For more information, see Getting Started with Federated Identities in the Amazon Cognito Developer Guide.
Amazon Cognito provides authentication, authorization, and user management for web and mobile apps. You can use Amazon Cognito unauthenticated identity pools with Amazon Location as a way for applications to retrieve temporary, scoped-down AWS credentials.
For more information, see Getting Started with User Pools in the Amazon Cognito Developer Guide.
Note
For mobile developers, Amazon Location provides mobile authentication SDKs for both iOS and Android, see the following GitHub repositories for more information:
Create an Amazon Cognito identity pool
You can create Amazon Cognito identity pools to allow unauthenticated guest access to your application through the Amazon Cognito console, the AWS CLI, or the Amazon Cognito APIs.
Important
The pool that you create must be in the same AWS account and AWS Region as the Amazon Location Service resources that you're using.
You can use IAM policies associated with unauthenticated identity roles with the following actions:
-
geo:GetMap*
-
geo:SearchPlaceIndex*
-
geo:GetPlace
-
geo:CalculateRoute*
-
geo:GetGeofence
-
geo:ListGeofences
-
geo:PutGeofence
-
geo:BatchDeleteGeofence
-
geo:BatchPutGeofence
-
geo:BatchEvaluateGeofences
-
geo:GetDevicePosition*
-
geo:ListDevicePositions
-
geo:BatchDeleteDevicePositionHistory
-
geo:BatchGetDevicePosition
-
geo:BatchUpdateDevicePosition
Including other Amazon Location actions will have no effect, and unauthenticated identities will be unable to call them.
To create an identity pool using the Amazon Cognito console
-
Go to the Amazon Cognito console
. -
Choose Manage Identity Pools.
-
Choose Create new identity pool, then enter a name for your identity pool.
-
From the Unauthenticated identities collapsible section, choose Enable access to unauthenticated identities.
-
Choose Create Pool.
-
Choose which IAM roles you want to use with your identity pool.
-
Expand View Details.
-
Under Unauthenticated identities, enter a role name.
-
Expand the View Policy Document section, then choose Edit to add your policy.
-
Add your policy to grant access to your resources.
The following are policy examples for Maps, Places, Trackers, and Routes. To use the examples for your own policy, replace the
region
andaccountID
placeholders:Note
While unauthenticated identity pools are intended for exposure on unsecured internet sites, note that they will be exchanged for standard, time-limited AWS credentials.
It's important to scope the IAM roles associated with unauthenticated identity pools appropriately.
-
Choose Allow to create your identity pools.
The resulting identity pool follows the syntax
<
region
>:<GUID
>.
For example:
us-east-1:1sample4-5678-90ef-aaaa-1234abcd56ef
For more policy examples specific to Amazon Location, see Identity-based policy examples for Amazon Location Service.
Use the Amazon Cognito identity pools in JavaScript
The following example exchanges the unauthenticated identity pool that you've created
for credentials that are then used to fetch the style descriptor for your map resource
ExampleMap
.
const AWS = require("aws-sdk"); const credentials = new AWS.CognitoIdentityCredentials({ IdentityPoolId: "<identity pool ID>" // for example, us-east-1:1sample4-5678-90ef-aaaa-1234abcd56ef }); const client = new AWS.Location({ credentials, region: AWS.config.region || "<region>" }); console.log(await client.getMapStyleDescriptor("
ExampleMap
").promise());
Note
Retrieved credentials from unauthenticated identities are valid for one hour.
The following is an example of a function that automatically renews credentials before they expire.
async function refreshCredentials() { await credentials.refreshPromise(); // schedule the next credential refresh when they're about to expire setTimeout(refreshCredentials, credentials.expireTime - new Date()); }
To simplify this work, you can use the Amazon Location JavaScript Authentication helper. This is in place of both getting the credentials, and refreshing them. This example uses the AWS SDK for JavaScript v3.
import { LocationClient, GetMapStyleDescriptorCommand } from "@aws-sdk/client-location"; import { withIdentityPoolId } from "@aws/amazon-location-utilities-auth-helper"; const identityPoolId = "<identity pool ID>"; // for example, us-east-1:1sample4-5678-90ef-aaaa-1234abcd56ef // Create an authentication helper instance using credentials from Cognito const authHelper = await withIdentityPoolId(identityPoolId); const client = new LocationClient({ region: "<region>", // The region containing both the identity pool and tracker resource ...authHelper.getLocationClientConfig(), // Provides configuration required to make requests to Amazon Location }); const input = { MapName: "ExampleMap", }; const command = new GetMapStyleDescriptorCommand(input); console.log(await client.send(command));
Next steps
-
To modify your roles, go to the IAM console
. -
To manage your identity pools, go to the Amazon Cognito console
.