cloudfoundry-attic / cf-dotnet-sdk

Cloud Foundry .NET SDK
Other
18 stars 13 forks source link

Renew AuthToken before it expires #120

Open JohannesRudolph opened 8 years ago

JohannesRudolph commented 8 years ago

The current authentication scheme renews the auth token once it is expired and a new request is made via BuildAuthenticationHeader() and CloudFoundryClient.GenerateAuthorizationToken().

Unfortunately this does not account for the fact that the token may expire after the request is built but before it hits the remote CF Api. The java client uses a grace period of 50s for this, i.e. the token is renewed well before it expires: https://github.com/cloudfoundry/cf-java-client/pull/61/files#diff-36fcc6fd3cf665b5f3300efa12c78437R396

CloudFoundryClient.GenerateAuthorizationToken() should probably not use the Token.IsExpired property but instead determine expiry based on Token.Expires in combination with a grace period like:

var now = DateTime.Now;
var renewalMargin = TimeSpan.FromMinutes( 1 );
var renewAt = Token.Expires.Subtract( renewalMargin );

bool needsRenew = now > renewAt