# AWS token delegation flow yields tokens that are already expired

**URL:** <https://community.auth0.com/t/aws-token-delegation-flow-yields-tokens-that-are-already-expired/6369>\
**Category:** Get Help\
**Tags:** tokens, delegation, aws, access-token, javascript\
**Created:** [April 26, 2017, 1:14pm UTC](https://community.auth0.com/t/aws-token-delegation-flow-yields-tokens-that-are-already-expired/6369 "2017-04-26T13:14:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vegard](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/vegard/32/6351_2.png) [@vegard](https://community.auth0.com/u/vegard)\
**Post date:** [April 26, 2017, 1:14pm UTC](https://community.auth0.com/t/aws-token-delegation-flow-yields-tokens-that-are-already-expired/6369/1 "2017-04-26T13:14:55Z")

</div>

Hi!

I’m trying to implement the approach documented here:

> **[Integrate with Amazon Web Services and Products - Auth0 Docs](https://auth0.com/docs/customize/integrations/aws)**
>
> List of Amazon products and services, including Amazon Web Services (AWS) for identity and access management (IAM) with Auth0.

And I have authentication set up and mostly working - I get AWS credentials back to my JS app. However, the credentials have an expiry set as the same time as when delegation call was made, which renders them unusable. I have tried setting the rule as follows, to try and control the lifetime, but it had no effect:

```
function (user, context, callback) {
  if (context.protocol === 'delegation') {
    // set AWS settings
    user.awsRoles = "arn:aws:iam::snip:saml-provider/our-auth0,arn:aws:iam::snip:role/some-default-role"];

  if(user.aws_roles) {
    for(var i=0; i<user.aws_roles.length; i++) {
      var role = user.aws_roles*;
      for(var j=0; j<role.roles.length; j++) {
        user.awsRoles.push("arn:aws:iam::" + role.account + ":saml-provider/our-auth0,arn:aws:iam::" + role.account + ":role/some-" + role.roles[j]);
      }
    }
  }
    
    
    context.addonConfiguration = context.addonConfiguration || {};
    context.addonConfiguration.aws = context.addonConfiguration.aws || {};
    context.addonConfiguration.aws.principal = 'arn:aws:iam::snip:saml-provider/our-auth0';
    context.addonConfiguration.aws.role = 'arn:aws:iam::snip:role/some-default-role';
    
    context.addonConfiguration.aws.mappings = {
    'https://aws.amazon.com/SAML/Attributes/Role': 'awsRoles',
    'https://aws.amazon.com/SAML/Attributes/RoleSessionName': 'email',
    'https://aws.amazon.com/SAML/Attributes/SessionDuration': '43200'
  };
  }

  callback(null, user, context);
}

```

We use a similar approach for AWS console login, and it works great. We have a list of permitted accounts and roles stored per user, and add those dynamically. In the console SAML approach AWS presents a list of roles when logging in, is it possible to specify which of the permitted roles I want to use via the Auth0 JS API? Like this?

```
var options = {
                    id_token: result.idToken,
                    api: 'aws',
                    role: ROLE_CHOSEN_BY_USER,
                    principal: ARN_TO_ACCOUNT_PRINCIPAL
                };

                auth0.getDelegationToken(options, function (err, delegationResult) {
                    console.log(err);
                    $('#result').append(JSON.stringify(delegationResult.Credentials, null, '\t'));
                });

```

My thanks in advance for any help you can provide!\*

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [March 2, 2018, 5:53am UTC](https://community.auth0.com/t/aws-token-delegation-flow-yields-tokens-that-are-already-expired/6369/2 "2018-03-02T05:53:30Z")

</div>


