コード例 #1
0
        async Task <bool> AcquireAccessTokenAsync(string code, string state)
        {
            var contentParams = new Dictionary <string, string>
            {
                { "client_id", GhOAuthConfiguration.ClientId },
                { "client_secret", GhOAuthConfiguration.ClientSecret },
                { "code", code },
                { "state", state }
            };

            string content = String.Join("&", contentParams.Select(p => String.Format("{0}={1}", p.Key, p.Value)));

            try
            {
                SetLastError(null);

                var response = await _gitHubClient.Connection.Post <OAuthTokenResponse>(
                    new Uri("https://github.com/login/oauth/access_token", UriKind.Absolute),
                    content,
                    "application/json",
                    "application/x-www-form-urlencoded").ConfigureAwait(false);

                // TODO: save the entire json token (expiration time / renewal)
                string token = response.Body.access_token;

                var storedTokenInfo = new TokenSecurityInfo()
                {
                    AccessToken = token
                };

                VaultManager.Save(VaultTokenInfoResourceKey,
                                  VaultDefaultUnusedUserName,
                                  JsonConvert.SerializeObject(storedTokenInfo));

                return(true);
            }
            catch (Exception ex)
            {
                SetLastError(ex);
            }

            return(false);
        }
コード例 #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);
        }
コード例 #3
0
        public async Task StartOAuthFlow()
        {
            Exception exIfAny = null;

            try
            {
                string state = Guid.NewGuid().ToString("N");
                VaultManager.Save(VaultStateResourceKey, VaultDefaultUnusedUserName, state);

                // Delete the now "old" code and access token info record from the vault before proceeding
                VaultManager.Delete(VaultTokenCodeResourceKey, VaultDefaultUnusedUserName);
                VaultManager.Delete(VaultTokenInfoResourceKey, VaultDefaultUnusedUserName);
                _gitHubClient.Credentials = Credentials.Anonymous;

                var uri = FormatGitHubLoginUrl(GhOAuthConfiguration.ClientId,
                                               GhOAuthConfiguration.RedirectUri,
                                               DeclareScopes(),
                                               state);

                Uri requestUri  = new Uri(uri);
                Uri callbackUri = new Uri(GhOAuthConfiguration.RedirectUri);

                WebAuthenticationBroker.AuthenticateAndContinue(requestUri,
                                                                callbackUri,
                                                                null,
                                                                WebAuthenticationOptions.None);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
                exIfAny = ex;
            }

            if (null != exIfAny)
            {
                await _messageService.ShowAsync(exIfAny.Message);
            }
        }