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.
 
Default duration for started sessions
 
     public static final int DEFAULT_DURATION_SECONDS = 3600;
    
    
Default threshold for refreshing session credentials
 
     public static final int DEFAULT_THRESHOLD_SECONDS = 500;

    
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 final String wifToken;
     private final String wifProvider;
     private final String roleArn;
     private int sessionDuration;
     private int refreshThreshold;
     private String subjectFromWIF;

    
Constructs a new WebIdentityFederationSessionCredentialsProvider, which will use the specified 3rd-party web identity provider 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:
wifToken The OAuth/OpenID token from the the Identity Provider
wifProvider The name of the Identity Provider (null for OpenID providers)
roleArn The ARN of the IAM Role that will be assumed
 
     public WebIdentityFederationSessionCredentialsProvider(String wifTokenString wifProviderString roleArn) {
         this(wifTokenwifProviderroleArnnew ClientConfiguration());
     }

    
Constructs a new WebIdentityFederationSessionCredentialsProvider, which will use the specified 3rd-party web identity provider 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:
wifToken The OAuth/OpenID token from the the Identity Provider
wifProvider The name of the Identity Provider (null for OpenID providers)
roleArn The ARN of the IAM Role that will be assumed
clientConfiguation Configuration to apply to STS client created
 
     public WebIdentityFederationSessionCredentialsProvider(String wifTokenString wifProviderString roleArnClientConfiguration clientConfiguration) {
         this(wifTokenwifProviderroleArnnew AWSSecurityTokenServiceClient(new AnonymousAWSCredentials(), clientConfiguration));
     }

    
Constructs a new WebIdentityFederationSessionCredentialsProvider, which will use the specified 3rd-party web identity provider to make a request to the AWS Security Token Service (STS) using the provided client to request short lived session credentials, which will then be returned by this class's getCredentials() method.

Parameters:
wifToken The OAuth/OpenID token from the the Identity Provider
wifProvider The name of the Identity Provider (null for OpenID providers)
roleArn The ARN of the IAM Role that will be assumed
stsClient Preconfigured STS client to make requests with
    public WebIdentityFederationSessionCredentialsProvider(String wifTokenString wifProviderString roleArnAWSSecurityTokenService stsClient) {
        this. = stsClient
        this. = wifProvider;
        this. = wifToken;
        this. = roleArn;
    }
    @Override
    public AWSCredentials getCredentials() {
        if (needsNewSession()) startSession();
        return ;
    }
    @Override
    public void refresh() {
        startSession();
    }

   
Set the duration of the session credentials created by this client in seconds. Values must be supported by AssumeRoleWithWebIdentityRequest.

Parameters:
sessionDuration The new duration for session credentials created by this provider
See also:
com.amazonaws.services.securitytoken.model.AssumeRoleWithWebIdentityRequest
    public void setSessionDuration(int sessionDuration) {
        this. = sessionDuration;
    }

   
Set the duration of the session credentials created by this client in seconds. Values must be supported by AssumeRoleWithWebIdentityRequest. Returns refreence to object so methods can be chained together.

Parameters:
sessionDuration The new duration for session credentials created by this provider
Returns:
A reference to this updated object so that method calls can be chained together.
See also:
com.amazonaws.services.securitytoken.model.AssumeRoleWithWebIdentityRequest
        this.setSessionDuration(sessionDuration);
        return this;
    }

    
Get the duration of the session credentials created by this client in seconds. Values must be supported by AssumeRoleWithWebIdentityRequest.

Returns:
The duration for session credentials created by this provider
See also:
com.amazonaws.services.securitytoken.model.AssumeRoleWithWebIdentityRequest
    public int getSessionDuration() {
        return this.;
    }


   
Set the refresh threshold for the session credentials created by this client in seconds. This value will be used internally to determine if new credentials should be fetched from STS.

Parameters:
refreshThreshold The new refresh threshold for session credentials created by this provider
See also:
com.amazonaws.services.securitytoken.model.AssumeRoleWithWebIdentityRequest
    public void setRefreshThreshold(int refreshThreshold) {
        this. = refreshThreshold;
    }

   
Set the refresh threshold for the session credentials created by this client in seconds. This value will be used internally to determine if new credentials should be fetched from STS. Returns a refrence to the object so methods can be chained.

Parameters:
refreshThreshold The new refresh threshold for session credentials created by this provider
Returns:
A reference to this updated object so that method calls can be chained together.
See also:
com.amazonaws.services.securitytoken.model.AssumeRoleWithWebIdentityRequest
        this.setRefreshThreshold(refreshThreshold);
        return this;
    }

    
Get the refresh threshold for the session credentials created by this client in seconds. This value will be used internally to determine if new credentials should be fetched from STS.

Returns:
The refresh threshold for session credentials created by this provider
See also:
com.amazonaws.services.securitytoken.model.AssumeRoleWithWebIdentityRequest
    public int getRefreshThreshold() {
        return this.;
    }

    
Get the identifier returned from the Identity Provider for the authenticated user. This value is returned as part of the AssumeRoleWithIdentityResult

Returns:
The identifier returned from Identity Provider
See also:
com.amazonaws.services.securitytoken.model.AssumeRoleWithWebIdentityResult
    public String getSubjectFromWIF() {
        return this.;
    }

    
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() {
        AssumeRoleWithWebIdentityResult sessionTokenResult = 
                        .withProviderId()
                        .withRoleArn()
                        .withRoleSessionName("ProviderSession")
                        .withDurationSeconds(this.));
        Credentials stsCredentials = sessionTokenResult.getCredentials();
         = sessionTokenResult.getSubjectFromWebIdentityToken();
                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 the configured refresh threshold.

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