Example #1
0
        public Task <Credentials> GetCredentialsAsync()
        {
            string tokenInfoJson = VaultManager.GetPassword(VaultTokenInfoResourceKey, VaultDefaultUnusedUserName);

            if (String.IsNullOrWhiteSpace(tokenInfoJson))
            {
                // There has to be at least a JSON somewhere (even if invalid at this time) - otherwise the authz page was somehow not called
                throw new InvalidOperationException();
            }

            var tokenInfo = JsonConvert.DeserializeObject <TokenSecurityInfo>(tokenInfoJson);

            // TODO: do we need to refresh the access token?

            return(Task.FromResult(new Credentials(tokenInfo.AccessToken)));
        }
Example #2
0
        public async Task <bool> CompleteOAuthFlowAsync(string responseData)
        {
            // responseData value eg http://example.com/path?code=4cba03187d6cd6720217&state=6dfa60685ac94a29bdde2d9f3cbdce0e
            var responseUri = new Uri(responseData);
            var decoder     = new WwwFormUrlDecoder(responseUri.Query);
            var code        = decoder.GetFirstValueByName("code");
            var state       = decoder.GetFirstValueByName("state");

            string storedState = VaultManager.GetPassword(VaultStateResourceKey, VaultDefaultUnusedUserName);

            if (storedState != state)
            {
                return(false);
            }

            // We are storing the code to later be able to refresh the access token
            VaultManager.Save(VaultTokenCodeResourceKey, VaultDefaultUnusedUserName, code);

            bool ok = await AcquireAccessTokenAsync(code, state).ConfigureAwait(false);;

            return(ok);
        }