using System;
#if AWS_ASYNC_API
using System.Threading;
using System.Threading.Tasks;
#endif
using Amazon.Util;
namespace Amazon.Runtime
{
///
public class StaticTokenProvider : IAWSTokenProvider
{
private readonly string _token;
private readonly DateTime? _expiration;
///
/// Creates a new that can be assigned to
/// or added to a .
///
///
/// Bearer token to use to authorize AWS SDK requests.
///
///
/// An optional in UTC for which will no longer be
/// valid. Attempting to retrieve after will cause
/// a to be thrown.
///
public StaticTokenProvider(string token, DateTime? expiration = null)
{
_token = token;
_expiration = expiration;
}
#if BCL
public bool TryResolveToken(out AWSToken token)
{
if (IsTokenUnexpired())
{
token = new AWSToken {Token = _token};
return true;
}
else
{
token = null;
return false;
}
}
#endif
#if AWS_ASYNC_API
public Task> TryResolveTokenAsync(CancellationToken cancellationToken = default)
{
var isTokenUnexpired = IsTokenUnexpired();
return Task.FromResult(
new TryResponse
{
Success = isTokenUnexpired,
Value = isTokenUnexpired ? new AWSToken { Token = _token } : null
});
}
#endif
private bool IsTokenUnexpired()
{
#pragma warning disable CS0618 // Type or member is obsolete
return (!_expiration.HasValue || _expiration.Value < AWSSDKUtils.CorrectedUtcNow);
#pragma warning restore CS0618 // Type or member is obsolete
}
}
}