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

View File

@@ -69,8 +69,11 @@ namespace qbridge.Controllers
public OAuthRedirectController(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
if (TOKEN_STORE == null)
{
TOKEN_STORE = new Dictionary<string, QBToken>();
}
}
@@ -193,9 +196,6 @@ namespace qbridge.Controllers
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 });
@@ -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,11 +271,11 @@ 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
//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);