diff --git a/sdk/src/Core/Amazon.Runtime/Credentials/Internal/IdentityResolvers/DefaultAWSCredentialsIdentityResolver.cs b/sdk/src/Core/Amazon.Runtime/Credentials/Internal/IdentityResolvers/DefaultAWSCredentialsIdentityResolver.cs index 82c86b58d110..e982eb981fc8 100644 --- a/sdk/src/Core/Amazon.Runtime/Credentials/Internal/IdentityResolvers/DefaultAWSCredentialsIdentityResolver.cs +++ b/sdk/src/Core/Amazon.Runtime/Credentials/Internal/IdentityResolvers/DefaultAWSCredentialsIdentityResolver.cs @@ -48,7 +48,7 @@ public DefaultAWSCredentialsIdentityResolver() _credentialsGenerators = new List { #if BCL - () => new AppConfigAWSCredentials(), // Test explicit keys/profile name first. + () => new AppConfigAWSCredentials(), // Test explicit keys/profile name first. #endif () => AssumeRoleWithWebIdentityCredentials.FromEnvironmentVariables(), () => new EnvironmentVariablesAWSCredentials(), // Look for credentials set in environment vars. diff --git a/sdk/src/Core/Amazon.Runtime/Credentials/Internal/IdentityResolvers/DefaultAWSTokenIdentityResolver.cs b/sdk/src/Core/Amazon.Runtime/Credentials/Internal/IdentityResolvers/DefaultAWSTokenIdentityResolver.cs new file mode 100644 index 000000000000..d3fe53848707 --- /dev/null +++ b/sdk/src/Core/Amazon.Runtime/Credentials/Internal/IdentityResolvers/DefaultAWSTokenIdentityResolver.cs @@ -0,0 +1,60 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. 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. + */ + +using Smithy.Identity.Abstractions; +using System; + +namespace Amazon.Runtime.Credentials.Internal.IdentityResolvers +{ + /// + /// A resolver that provides an AWSToken identity. + /// + public class DefaultAWSTokenIdentityResolver : IIdentityResolver + { + /// + /// Gets or sets the AWSTokenProvider used to resolve the AWSToken. + /// By default, this is initialized with an AWSTokenProviderChain containing a ProfileTokenProvider. + /// + public IAWSTokenProvider AWSTokenProvider { get; set; } + + public DefaultAWSTokenIdentityResolver() + { + AWSTokenProvider = new AWSTokenProviderChain(new ProfileTokenProvider()); + } + + public BaseIdentity ResolveIdentity() + { + AWSToken token; +#if BCL + if (!AWSTokenProvider.TryResolveToken(out token)) + { + throw new AmazonClientException("Failed to resolve AWSToken using the configured AWSTokenProvider in DefaultAWSTokenIdentityResolver."); + } +#endif + +#if AWS_ASYNC_API + var tokenResponse = AWSTokenProvider.TryResolveTokenAsync().Result; + if (!tokenResponse.Success) + { + throw new AmazonClientException("Failed to resolve AWSToken using the configured AWSTokenProvider in DefaultAWSTokenIdentityResolver."); + } + + token = tokenResponse.Value; +#endif + + return token; + } + } +} diff --git a/sdk/src/Core/Amazon.Runtime/Credentials/Internal/IdentityResolvers/IdentityResolverConfiguration.cs b/sdk/src/Core/Amazon.Runtime/Credentials/Internal/IdentityResolvers/IdentityResolverConfiguration.cs index 50156f45a1ca..43c47fc6ecca 100644 --- a/sdk/src/Core/Amazon.Runtime/Credentials/Internal/IdentityResolvers/IdentityResolverConfiguration.cs +++ b/sdk/src/Core/Amazon.Runtime/Credentials/Internal/IdentityResolvers/IdentityResolverConfiguration.cs @@ -27,6 +27,7 @@ public class DefaultIdentityResolverConfiguration : IIdentityResolverConfigurati { { typeof(AnonymousAWSCredentials), new AnonymousIdentityResolver() }, { typeof(AWSCredentials), new DefaultAWSCredentialsIdentityResolver() }, + { typeof(AWSToken), new DefaultAWSTokenIdentityResolver() }, }; /// diff --git a/sdk/src/Core/Amazon.Runtime/Tokens/AWSToken.cs b/sdk/src/Core/Amazon.Runtime/Tokens/AWSToken.cs index aa59f789372d..d2d45688ec2b 100644 --- a/sdk/src/Core/Amazon.Runtime/Tokens/AWSToken.cs +++ b/sdk/src/Core/Amazon.Runtime/Tokens/AWSToken.cs @@ -15,6 +15,7 @@ using System; using System.Diagnostics; +using Smithy.Identity.Abstractions; namespace Amazon.Runtime { @@ -26,11 +27,19 @@ namespace Amazon.Runtime /// This class is the focused public projection of the internal class /// Amazon.Runtime.Credentials.Internal.SsoToken /// - [DebuggerDisplay("{"+ nameof(Token) + "}")] - public class AWSToken + [DebuggerDisplay("{" + nameof(Token) + "}")] + public class AWSToken : BaseIdentity { public string Token { get; set; } - public DateTime? ExpiresAt { get; set; } + + [Obsolete("This property is deprecated in favor of Expiration.")] + public DateTime? ExpiresAt + { + get { return Expiration; } + set { this.Expiration = value; } + } + + public override DateTime? Expiration { get; set; } public override string ToString() { diff --git a/sdk/src/Core/Amazon.Runtime/_bcl+netstandard/Tokens/SSOTokenProvider.cs b/sdk/src/Core/Amazon.Runtime/_bcl+netstandard/Tokens/SSOTokenProvider.cs index e1bf6783a5d8..c376c95d1fe8 100644 --- a/sdk/src/Core/Amazon.Runtime/_bcl+netstandard/Tokens/SSOTokenProvider.cs +++ b/sdk/src/Core/Amazon.Runtime/_bcl+netstandard/Tokens/SSOTokenProvider.cs @@ -137,7 +137,7 @@ private AWSToken MapSsoTokenToAwsToken(SsoToken token) return new AWSToken { Token = token.AccessToken, - ExpiresAt = token.ExpiresAt + Expiration = token.ExpiresAt }; } }