* fix SA1505 and SA1508 * fix SA1116 * fix SA1009 * fix SA1019 * fix SA1127 * fix SA1128 * fix SA1134 * fix indent * allow CA2227 * fix CA1810 * using clean up * fix naming * fix CA1806 * fix await * Revert "fix CA1806" This reverts commit a3b465087fdaf26ec461272373ee9810a90de2cc. * fix dotnet format * allow SA1009
39 lines
1.3 KiB
C#
39 lines
1.3 KiB
C#
using System;
|
|
using System.Net.Http.Headers;
|
|
using System.IO;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Rest;
|
|
|
|
namespace k8s.Authentication
|
|
{
|
|
public class TokenFileAuth : ITokenProvider
|
|
{
|
|
private string token;
|
|
internal string TokenFile { get; set; }
|
|
internal DateTime TokenExpiresAt { get; set; }
|
|
|
|
public TokenFileAuth(string tokenFile)
|
|
{
|
|
TokenFile = tokenFile;
|
|
}
|
|
|
|
public Task<AuthenticationHeaderValue> GetAuthenticationHeaderAsync(CancellationToken cancellationToken)
|
|
{
|
|
if (TokenExpiresAt < DateTime.UtcNow)
|
|
{
|
|
token = File.ReadAllText(TokenFile).Trim();
|
|
// in fact, the token has a expiry of 10 minutes and kubelet
|
|
// refreshes it at 8 minutes of its lifetime. setting the expiry
|
|
// of 1 minute makes sure the token is reloaded regularly so
|
|
// that its actual expiry is after the declared expiry here,
|
|
// which is as suffciently true as the time of reading a token
|
|
// < 10-8-1 minute.
|
|
TokenExpiresAt = DateTime.UtcNow.AddMinutes(1);
|
|
}
|
|
|
|
return Task.FromResult(new AuthenticationHeaderValue("Bearer", token));
|
|
}
|
|
}
|
|
}
|