public async Task <RedirectResult> ConnectToPexaCallback(string code, string returnUrl = null) { var(successResult, receivedUtc, payload) = await ExchangeCodeAsync( $"{_pEXAService.AuthUrlBase.AbsoluteUri}as/token.oauth2", code, CreatePexaCallbackUrl(returnUrl), _pexaSettings.ClientId, _pexaSettings.ClientSecret); if (!successResult) { throw new NotImplementedException("Not sure how to handle invalid response from OAuth2 token endpoint"); } // Store token await _mediator.Send(new StorePexaApiTokenCommand() { AuthenticatedUser = await _userManager.GetUserAsync(User), PexaApiToken = PexaApiToken.Success(payload, receivedUtc) }); if (string.IsNullOrEmpty(returnUrl)) { return(new RedirectResult(Url.Page("/PopupConnectionSuccessful"))); } else { return(new RedirectResult(returnUrl)); } }
public async void PexaApiTokenQueryRetrieveFromCache() { var query = new PexaApiTokenQuery() { AuthenticatedUser = new WCAUser(), BypassAndUpdateCache = false }; var credentialId = $"PEXA-API-token-for-user-{query.AuthenticatedUser.Id}"; var testTokenInCache = "{\"access_token\": \"Test Access Token in Cache\"}"; var testPexaApiTokenInCache = PexaApiToken.Success(JObject.Parse(testTokenInCache), Instant.FromUtc(2019, 7, 14, 2, 5)); var testTokenInVault = "{\"access_token\": \"Test Access Token in Vault\"}"; var testPexaApiTokenInVault = PexaApiToken.Success(JObject.Parse(testTokenInVault), Instant.FromUtc(2019, 7, 14, 2, 5)); using (var pexaTestKeyVaultClient = new PexaTestKeyVaultClient(credentialId, testPexaApiTokenInVault)) using (var testMemoryCache = new MemoryCache(Options.Create(new MemoryCacheOptions()))) { testMemoryCache.Set(credentialId, testPexaApiTokenInCache); var handlerUnderTest = new PexaApiTokenQueryHandler( pexaTestKeyVaultClient, new PexaApiTokenQuery.Validator(), testMemoryCache, Options.Create(new WCACoreSettings() { CredentialAzureKeyVaultUrl = "https://dummy" })); var result = await handlerUnderTest.Handle(query, new CancellationToken()); Assert.Equal("Test Access Token in Cache", result.AccessToken); } }