Skip to content

How to store credentials from identity pool and refresh them

0

Hello, So I am trying to build a cognito-based login system for my next js app using aws-sdk for javascript v3. When I login to my app using the Cognito user pool I also fetch the access key and secret key from the identity pool. What is the best practice to store them in my app? Also, these keys last only for an hour, how do I refresh them?

asked 2 years ago835 views

2 Answers
0

Hi muramasa,

The best practice is to avoid storing access and secret keys in local storage, session storage, or cookies due to security risks. Instead, you can store these credentials in memory during the session, which reduces exposure to XSS (Cross-Site Scripting) attacks since the credentials aren't directly accessible to client-side scripts outside of the runtime scope. However, keep in mind that credentials stored in memory will be lost if the user refreshes the page or navigates away, which might lead to a less smooth user experience.

Alternatively, you could use secure cookies to store the credentials. Secure cookies marked as HttpOnly and Secure ensure that credentials are not accessible via JavaScript and are only transmitted over HTTPS, reducing XSS risks. This method allows credentials to persist across sessions, meaning users remain logged in even after a page refresh or reopening the browser. However, secure cookies come with their own risks, such as session hijacking and potential CSRF vulnerabilities, which require additional security measures like CSRF protection and consistent use of HTTPS.

AWS SDKs can automatically handle credential refreshing if you use the CognitoIdentityCredentials provider. When a session is about to expire, the SDK will refresh the credentials using the tokens provided by Cognito. Here’s how you can set it up:

import { CognitoIdentityProviderClient, GetCredentialsForIdentityCommand } from "@aws-sdk/client-cognito-identity-provider";

const client = new CognitoIdentityProviderClient({ region: "your-region" });

// Fetch credentials
const credentials = await client.send(new GetCredentialsForIdentityCommand({ 
  IdentityId: identityId, 
  Logins: { 
    "cognito-idp.your-region.amazonaws.com/your-user-pool-id": idToken 
  }
}));

// Use credentials
AWS.config.credentials = credentials;

For optimal security, store credentials in memory during the session to minimize exposure, but be aware of the trade-offs in terms of persistence across page reloads. If you require persistence, secure cookies are a viable alternative, provided you implement appropriate security measures to mitigate risks like session hijacking and CSRF. Regardless of the storage method, ensure your credentials are refreshed before they expire by leveraging AWS SDK’s automatic handling capabilities. I'm here to help.

EXPERT

answered 2 years ago

0

Hi,

Regarding -

Also, these keys last only for an hour, how do I refresh them?

You may want to use the API AWS.config.credentials.refresh, which "Refreshes credentials using AWS.CognitoIdentity.getCredentialsForIdentity(), or AWS.STS.assumeRoleWithWebIdentity()." (note! it will refresh your IAM credentials if your identity provider credentials are not yet expired).

AWS

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.