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) public OAuthRedirectController(IHttpClientFactory clientFactory)
{ {
_clientFactory = 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"); return BadRequest("state value is required");
} }
//Job one is to clean out the old entries in the token store if necessary //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 //rather than bothering with some kind of recurring task just do it on every fetch for now
SweepTokenStore(); SweepTokenStore();
//GET THE DISCOVERY DOCUMENT //GET THE DISCOVERY DOCUMENT
//Discovery document contains the actual current endpoints to use for various ops //Discovery document contains the actual current endpoints to use for various ops
await GetQBDiscoveryDocument(); await GetQBDiscoveryDocument();
@@ -192,12 +195,9 @@ namespace qbridge.Controllers
var x_refresh_token_expires_in = AccessTokenObject["x_refresh_token_expires_in"].Value<long>(); var x_refresh_token_expires_in = AccessTokenObject["x_refresh_token_expires_in"].Value<long>();
var access_token_expires_in = AccessTokenObject["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!! //Store the token!!
TOKEN_STORE.Add(state,new QBToken(){realmId=realmId,access_token=access_token,refresh_token=refresh_token,TokenBirthday=DateTime.Now}); TOKEN_STORE.Add(state, new QBToken() { realmId = realmId, access_token = access_token, refresh_token = refresh_token, TokenBirthday = DateTime.Now });
return Ok(new return Ok(new
{ {
@@ -232,13 +232,25 @@ TOKEN_STORE.Add(state,new QBToken(){realmId=realmId,access_token=access_token,re
[HttpGet("fetch/{state}")] [HttpGet("fetch/{state}")]
public IActionResult FetchTokenAsync([FromRoute]string state) public IActionResult FetchTokenAsync([FromRoute]string state)
{ {
//clear out any tokens older than 1 hour
SweepTokenStore();
if (string.IsNullOrWhiteSpace(state)) if (string.IsNullOrWhiteSpace(state))
{ {
return BadRequest("state value is required"); 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 //Remove stale tokens
public static void SweepTokenStore() public static void SweepTokenStore()
{ {
//ditch tokens older than 7 days //ditch tokens older than 1 hour
//this works because our system in place is intended to be re-authed every session //QBOI2 will refresh the token so here we store only the original access token
DateTime dtExpireAfter=DateTime.Now.AddDays(-7); DateTime dtExpireAfter = DateTime.Now.AddDays(-7);
//if the token birthday is newer than 7 days ago then select it to remain //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, .ToDictionary(pair => pair.Key,
pair => pair.Value); pair => pair.Value);
} }