This commit is contained in:
2019-10-02 18:01:41 +00:00
parent 82ed32a52f
commit 853a5e82f6

View File

@@ -69,7 +69,10 @@ namespace qbridge.Controllers
public OAuthRedirectController(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
TOKEN_STORE = new Dictionary<string, QBToken>();
if (TOKEN_STORE == null)
{
TOKEN_STORE = new Dictionary<string, QBToken>();
}
}
@@ -83,11 +86,11 @@ namespace qbridge.Controllers
{
return BadRequest("state value is required");
}
//Job one is to clean out the old entries in the token store if necessary
//rather than bothering with some kind of recurring task just do it on every fetch for now
SweepTokenStore();
//GET THE DISCOVERY DOCUMENT
//Discovery document contains the actual current endpoints to use for various ops
await GetQBDiscoveryDocument();
@@ -192,12 +195,9 @@ namespace qbridge.Controllers
var x_refresh_token_expires_in = AccessTokenObject["x_refresh_token_expires_in"].Value<long>();
var access_token_expires_in = AccessTokenObject["expires_in"].Value<long>();
//TODO: Instead of returning the token here, store it in memory so QBOI can fetch it via the session id token in the "state" variable here
//return instead that user is successfully logged in and QBOI is ready to access
//Store the token!!
TOKEN_STORE.Add(state,new QBToken(){realmId=realmId,access_token=access_token,refresh_token=refresh_token,TokenBirthday=DateTime.Now});
//Store the token!!
TOKEN_STORE.Add(state, new QBToken() { realmId = realmId, access_token = access_token, refresh_token = refresh_token, TokenBirthday = DateTime.Now });
return Ok(new
{
@@ -232,13 +232,25 @@ TOKEN_STORE.Add(state,new QBToken(){realmId=realmId,access_token=access_token,re
[HttpGet("fetch/{state}")]
public IActionResult FetchTokenAsync([FromRoute]string state)
{
//clear out any tokens older than 1 hour
SweepTokenStore();
if (string.IsNullOrWhiteSpace(state))
{
return BadRequest("state value is required");
}
return Ok();
var token = TOKEN_STORE.FirstOrDefault(pair => pair.Key == state);
if (token.Key == null)
{
Task.WaitAll( Task.Delay( 10000 ) );
return NotFound();
}
else
{
return Ok(token);
}
}
@@ -259,14 +271,14 @@ TOKEN_STORE.Add(state,new QBToken(){realmId=realmId,access_token=access_token,re
//Remove stale tokens
public static void SweepTokenStore()
{
//ditch tokens older than 7 days
//this works because our system in place is intended to be re-authed every session
DateTime dtExpireAfter=DateTime.Now.AddDays(-7);
//ditch tokens older than 1 hour
//QBOI2 will refresh the token so here we store only the original access token
DateTime dtExpireAfter = DateTime.Now.AddDays(-7);
//if the token birthday is newer than 7 days ago then select it to remain
TOKEN_STORE = TOKEN_STORE.Where(pair => pair.Value.TokenBirthday < dtExpireAfter)
TOKEN_STORE = TOKEN_STORE.Where(pair => pair.Value.TokenBirthday > dtExpireAfter)
.ToDictionary(pair => pair.Key,
pair => pair.Value);
}