Skip to content

aws sso login should respect credential expiration #6948

Open

Description

@benkehoe

Describe the feature

Currently, aws sso login goes through the log in process whenever it's called. So if I have two profiles foo and bar that share the same AWS SSO configuration (i.e., sso_start_url and sso_region), when I call aws sso login --profile foo, it will pop up a browser window, log me in, etc., resulting in a valid token being cached in ~/.aws/sso/cache. If I then call aws sso login --profile bar (or aws sso login --profile foo again, for that matter), despite having a valid token for that config, it will go through the entire login process again, resulting in a new token.

Use Case

It's unnecessary work, and it's exacerbated by the argument to aws sso login being --profile, which leads people to think they need to call it whenever they're using a particular profile (I wrote an explainer to help people understand they don't need to do that).

Proposed Solution

aws sso login should check the expiration of the cached token, and exit out successfully if the token is not expired. There should be a --force-refresh option to override this.

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CLI version used

N/A

Environment details (OS name and version, etc.)

N/A

Activity

  1. added
    feature-request
    needs-triage
    on May 13, 2022
  2. lorengordon commented on May 14, 2022

    @lorengordon
    Contributor

    So much this, yes, please!

  3. removed
    needs-triage
    on May 16, 2022
  4. moved this to Contribution Ready in AWS CLI Contribution Boardon May 16, 2022
  5. gondalez commented on May 17, 2022

    @gondalez

    I came here to report/request this also 🙏 💯

    My team and I use aws sso login during development and found having to log in breaks our concentration multiple times a day.

    We use this fairly crude workaround script, but it is messy to make available to all of our usages.
    I would much rather delete it and use aws sso login directly 😌 !

    if aws sts get-caller-identity > /dev/null ; then
      echo '🔑 AWS Credentials AOK ✅'
    else
      echo '🔑 AWS Credential NOK ❎. Logging in via SSO...'
      aws sso login
    fi
  6. nmiekley commented on May 18, 2022

    @nmiekley

    Thanks for working on this! This would also solve #5972, wouldn't it?

  7. benkehoe commented on May 18, 2022

    @benkehoe
    Author

    It looks like it's the same issue

  8. benkehoe commented on May 18, 2022

    @benkehoe
    Author

    I should note that aws-sso-util login does this already. https://github.com/benkehoe/aws-sso-util/blob/master/docs/login.md

  9. added
    p2
    on Nov 10, 2022
  10. ixti commented on Feb 10, 2023

    @ixti

    @gondalez that's awesome. I've modified your solution for myself a bit:

    message="$(aws sts get-caller-identity 2>&1)"
    exit_code="$?"
    
    case "$exit_code" in
      0)
        echo -e "\e[32m🔑 AWS SSO credentials are valid.\e[0m"
        ;;
    
      255)
        echo -e "\e[33m🔑 AWS SSO credentials are expired. Logging in...\e[0m"
        aws sso login
        ;;
    
      *)
        echo -e "\e[31m🔑 Something went wrong...\e[0m" >&2
        echo "$message" >&2
        exit $exit_code
    esac
  11. TheJosh commented on Jul 7, 2023

    @TheJosh

    One-liner that's also quiet:

    aws sts get-caller-identity >/dev/null 2>&1 || aws sso login
  12. 5 remaining items

  13. kjenney commented on Nov 21, 2023

    @kjenney

    I have dozens of AWS SSO profiles that I use every day. I use aws-sso-creds to get credentials for local use. After the initial login I check for a valid session in the cache and if there's already one there I skip aws sso login and go straight to aws-sso-creds export. This works for me multiple times a day across multiple AWS orgs.

    In other words: don't use aws sso login more than once against the same SSO domain. The credentials are already there.

  14. moved this from Ready (Internal) to Audit in AWS CLI Contribution Boardon Nov 24, 2025
  15. added
    needs-review
    and removed on Apr 6, 2026
  16. removed
    needs-review
    on Apr 22, 2026
  17. ashovlin commented on Apr 23, 2026

    @ashovlin
    Member

    Hey all, revisiting this as we evaluate open feature requests. We are open to a contribution if somebody gets here before we do.

    We don't want to change the default behavior of aws sso login for backwards compatibility. Rather than newly asking users to provide --force to match existing behavior, I'd rather gate the new behavior behind a new --skip-if-valid option (open to naming suggestions there).

    I prefer to check if the access and refresh token validity, not whether the temporary credentials from GetRoleCredentials are still valid. As pointed out above: aws sso login is operating on the sso-session as opposed to a specific profile/role, and the ~/.aws/cli/cache directory can muddy the waters.

    So for --skip-if-valid, I would:

    1. Locate the right token cache file
    2. If the access token's expiresAt is still valid, return success.
    3. If the refresh token can successfully still be exchanged, do so, and return success.
    4. Otherwise proceed with the full, current workflow.
  18. gunzy83 commented on Apr 24, 2026

    @gunzy83

    Hey all, revisiting this as we evaluate open feature requests. We are open to a contribution if somebody gets here before we do.

    We don't want to change the default behavior of aws sso login for backwards compatibility. Rather than newly asking users to provide --force to match existing behavior, I'd rather gate the new behavior behind a new --skip-if-valid option (open to naming suggestions there).

    I prefer to check if the access and refresh token validity, not whether the temporary credentials from GetRoleCredentials are still valid. As pointed out above: aws sso login is operating on the sso-session as opposed to a specific profile/role, and the ~/.aws/cli/cache directory can muddy the waters.

    So for --skip-if-valid, I would:

    1. Locate the right token cache file
    2. If the access token's expiresAt is still valid, return success.
    3. If the refresh token can successfully still be exchanged, do so, and return success.
    4. Otherwise proceed with the full, current workflow.

    This looks great to me. Most of us are just looking for a way to automatically check the validity of the session rather than any specific role that can be assumed from that session and login again if it is not valid. Doing this in one command just makes our existing scripts/taskfiles etc more simple and reliable.

  19. jorhett commented on Apr 24, 2026

    @jorhett

    A command to just return 0 or 1 for is-valid (without a login) would also be useful. So steps 1-3 without step 4?

  20. benkehoe commented on Apr 24, 2026

    @benkehoe
    Author
    1. Locate the right token cache file
    2. If the access token's expiresAt is still valid, return success.
    3. If the refresh token can successfully still be exchanged, do so, and return success.
    4. Otherwise proceed with the full, current workflow.

    Agreed on this being the right process, but I'd like to push back a little bit on --skip-if-valid, which requires that people learn about it. If we went the --force-refresh route, so executing the above process by default, and if a valid token was found or retrieved the CLI printed:

    Identity Center session appears to still be valid and login not needed. To force a fresh login, use --force-refresh.

    what are the use cases that we think would break? I would argue the vast majority of users would be happy not to have to go through the login flow and their use cases would not break. The ones where something still wasn't working, they would get instructions for it directly.

    Maybe an alternative is to run the check every time anyway, and if a valid token is found, print "You appear to have a valid session. You can use --skip-if-valid to avoid a fresh login when it is not needed." before still proceeding with the current login flow. But I still think it's worth thinking hard if we can make skip-if-valid behavior the default.

  21. vurumalla92 commented on Nov 18, 2026

    @vurumalla92

    Hi, I use aws sso login daily across several profiles sharing one SSO session, so this bites me too. I'd like to work on a fix: in LoginCommand, check the cached token in ~/.aws/sso/cache before starting the browser flow, and if a token for the same start URL is still valid, print that the session is already active and exit without re-authenticating. I'd also add a --force flag to force a fresh login when needed, and update the command description that currently says a new token is retrieved every time. Does that shape sound right — cache-validity check by default with an opt-out --force — or would you prefer the check to live elsewhere?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    communitycontribution-readyfeature-requestp2sso

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions