Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2011-2015 Amazon Technologies, Inc.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at:
   *
   *    http://aws.amazon.com/apache2.0
   *
  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
  * OR CONDITIONS OF ANY KIND, either express or implied. See the
  * License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.amazonaws.auth;
 
 import java.util.Date;
 
AWSCredentialsProvider implementation that uses the AWS Security Token Service to create temporary, short-lived sessions to use for authentication.
 
 public class STSSessionCredentialsProvider implements AWSSessionCredentialsProvider {

    
Default duration for started sessions
 
     public static final int DEFAULT_DURATION_SECONDS = 3600;

    
The client for starting STS sessions
 
     private final AWSSecurityTokenService securityTokenService;

    
The current session credentials
 
     private AWSSessionCredentials sessionCredentials;

    
The expiration time for the current session credentials
 
     private Date sessionCredentialsExpiration;


    
Constructs a new STSSessionCredentialsProvider, which will use the specified long lived AWS credentials to make a request to the AWS Security Token Service (STS) to request short lived session credentials, which will then be returned by this class's getCredentials() method.

Parameters:
longLivedCredentials The main AWS credentials for a user's account.
 
     public STSSessionCredentialsProvider(AWSCredentials longLivedCredentials) {
         this(longLivedCredentialsnew ClientConfiguration());
     }

    
Constructs a new STSSessionCredentialsProvider, which will use the specified long lived AWS credentials to make a request to the AWS Security Token Service (STS) to request short lived session credentials, which will then be returned by this class's getCredentials() method.

Parameters:
longLivedCredentials The main AWS credentials for a user's account.
clientConfiguration Client configuration connection parameters.
 
     public STSSessionCredentialsProvider(AWSCredentials longLivedCredentialsClientConfiguration clientConfiguration) {
          = new AWSSecurityTokenServiceClient(longLivedCredentialsclientConfiguration);
     }

    
Constructs a new STSSessionCredentialsProvider, which will use the specified credentials provider (which vends long lived AWS credentials) to make a request to the AWS Security Token Service (STS) to request short lived session credentials, which will then be returned by this class's getCredentials() method.

Parameters:
longLivedCredentialsProvider Credentials provider for the main AWS credentials for a user's account.
 
     public STSSessionCredentialsProvider(AWSCredentialsProvider longLivedCredentialsProvider) {
          = new AWSSecurityTokenServiceClient(longLivedCredentialsProvider);
     }

    
Constructs a new STSSessionCredentialsProvider, which will use the specified credentials provider (which vends long lived AWS credentials) to make a request to the AWS Security Token Service (STS) to request short lived session credentials, which will then be returned by this class's getCredentials() method.

Parameters:
longLivedCredentialsProvider Credentials provider for the main AWS credentials for a user's account.
clientConfiguration Client configuration connection parameters.
    public STSSessionCredentialsProvider(AWSCredentialsProvider longLivedCredentialsProviderClientConfiguration clientConfiguration) {
         = new AWSSecurityTokenServiceClient(longLivedCredentialsProviderclientConfiguration);
    }

    
Sets the AWS Security Token Service (STS) endpoint where session credentials are retrieved from.

The default AWS Security Token Service (STS) endpoint ("sts.amazonaws.com") works for all accounts that are not for China (Beijing) region or GovCloud. You only need to change the endpoint to "sts.cn-north-1.amazonaws.com.cn" when you are requesting session credentials for services in China(Beijing) region or "sts.us-gov-west-1.amazonaws.com" for GovCloud.

Setting this invalidates existing session credentials.
    public void setSTSClientEndpoint(String endpoint) {
        .setEndpoint(endpoint);
         = null;
    }
    @Override
        if (needsNewSession()) startSession();
        return ;
    }
    @Override
    public void refresh() {
        startSession();
    }

    
Starts a new session by sending a request to the AWS Security Token Service (STS) with the long lived AWS credentials. This class then vends the short lived session credentials sent back from STS.
    private void startSession() {
        GetSessionTokenResult sessionTokenResult = 
        Credentials stsCredentials = sessionTokenResult.getCredentials();
                stsCredentials.getAccessKeyId(),
                stsCredentials.getSecretAccessKey(),
                stsCredentials.getSessionToken());
         = stsCredentials.getExpiration();
    }

    
Returns true if a new STS session needs to be started. A new STS session is needed when no session has been started yet, or if the last session is within 60 seconds of expiring.

Returns:
True if a new STS session needs to be started.
    private boolean needsNewSession() {
        if ( == nullreturn true;
        long timeRemaining = .getTime() - System.currentTimeMillis();
        return timeRemaining < (60 * 1000);
    }
New to GrepCode? Check out our FAQ X