Allowing 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 frontend 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 Allowing 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.
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.
Using an
aws:referer
condition adds security by limiting browser access to a list of URLs or URL prefixes. Adding anaws:referer
condition may also limit testing when usinglocalhost:
URLs. -
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.
Using 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()); }
Next steps
-
To modify your roles, go to the IAM console
. -
To manage your identity pools, go to the Amazon Cognito console
.