コード例 #1
0
ファイル: SplashVM.cs プロジェクト: steamypassion/X
        public void ParseAuthenticationResult(WebAuthenticationResult result)
        {
            switch (result.ResponseStatus)
            {
            case WebAuthenticationStatus.ErrorHttp:
                //Debug.WriteLine("Error");
                break;

            case WebAuthenticationStatus.Success:
                var pattern = string.Format("{0}#access_token={1}&expires_in={2}", WebAuthenticationBroker.GetCurrentApplicationCallbackUri(), "(?<access_token>.+)", "(?<expires_in>.+)");
                var match   = Regex.Match(result.ResponseData, pattern);

                var access_token = match.Groups["access_token"];
                var expires_in   = match.Groups["expires_in"];

                _accessToken = access_token.Value;
                _tokenExpiry = DateTime.Now.AddSeconds(double.Parse(expires_in.Value));

                var dm = new PassportDataModel();
                dm.Token       = _accessToken;
                dm.TokenExpiry = _tokenExpiry;
                //dm.TokenSecret = AccessToken.TokenSecret;
                //dm.Verifier = xoauth_verifier;
                dm.PassType = GroupingType;
                //dm.UserId = AccessToken.UserId;
                //dm.UserName = AccessToken.Username;
                //dm.FullName = AccessToken.FullName;
                //dm.ScreenName = AccessToken.ScreenName;

                dm.APIKeyFKID = apiKey.Id;

                StorageService.Instance.Storage.Insert(dm);

                IsLoggedInVisible  = Visibility.Visible;
                IsLoginVisible     = Visibility.Collapsed;
                IsAPIEditorVisible = Visibility.Collapsed;

                break;

            case WebAuthenticationStatus.UserCancel:
                //Debug.WriteLine("Operation aborted");
                break;

            default:
                break;
            }
        }
コード例 #2
0
        public async void AttemptTwitterLogin()
        {
            var authorizer = new UniversalAuthorizer
            {
                CredentialStore = new SingleUserInMemoryCredentialStore
                {
                    ConsumerKey    = apiKey.APIKey,
                    ConsumerSecret = apiKey.APISecret
                },
                SupportsCompression = true,
                Callback            = apiKey.APICallbackUrl
            };

            await authorizer.AuthorizeAsync();

            _twitterCtx = new TwitterContext(authorizer);

            var dm = new PassportDataModel();

            dm.Token       = authorizer.CredentialStore.OAuthToken;
            dm.TokenSecret = authorizer.CredentialStore.OAuthTokenSecret;
            dm.Verifier    = authorizer.Parameters["oauth_verifier"];
            dm.PassType    = GroupingType;

            dm.UserId     = authorizer.CredentialStore.UserID.ToString();
            dm.UserName   = authorizer.CredentialStore.ScreenName;
            dm.FullName   = authorizer.CredentialStore.ScreenName;
            dm.ScreenName = authorizer.CredentialStore.ScreenName;

            dm.APIKeyFKID = apiKey.Id;


            StorageService.Instance.Storage.Insert(dm);

            PopulatePassportData();
        }
コード例 #3
0
        private async void AttemptFlickrLogin()
        {
            //apiKey = ctlApiEditor.APIKey;

            try
            {
                _flickr.ApiKey    = apiKey.APIKey;
                _flickr.ApiSecret = apiKey.APISecret;


                // Acquiring a request token
                TimeSpan SinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1);
                Random   Rand       = new Random();
                String   FlickrUrl  = "https://secure.flickr.com/services/oauth/request_token";
                Int32    Nonce      = Rand.Next(1000000000);

                // Compute base signature string and sign it.
                // This is a common operation that is required for all requests even after the token is obtained.
                // Parameters need to be sorted in alphabetical order
                // Keys and values should be URL Encoded.
                String SigBaseStringParams = "oauth_callback=" + Uri.EscapeDataString(apiKey.APICallbackUrl);
                SigBaseStringParams += "&" + "oauth_consumer_key=" + apiKey.APIKey;
                SigBaseStringParams += "&" + "oauth_nonce=" + Nonce.ToString();
                SigBaseStringParams += "&" + "oauth_signature_method=HMAC-SHA1";
                SigBaseStringParams += "&" + "oauth_timestamp=" + Math.Round(SinceEpoch.TotalSeconds);
                SigBaseStringParams += "&" + "oauth_version=1.0";
                String SigBaseString = "GET&";
                SigBaseString += Uri.EscapeDataString(FlickrUrl) + "&" + Uri.EscapeDataString(SigBaseStringParams);

                IBuffer KeyMaterial = CryptographicBuffer.ConvertStringToBinary(apiKey.APISecret + "&", BinaryStringEncoding.Utf8);
                MacAlgorithmProvider HmacSha1Provider = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1");
                CryptographicKey     MacKey           = HmacSha1Provider.CreateKey(KeyMaterial);
                IBuffer DataToBeSigned  = CryptographicBuffer.ConvertStringToBinary(SigBaseString, BinaryStringEncoding.Utf8);
                IBuffer SignatureBuffer = CryptographicEngine.Sign(MacKey, DataToBeSigned);
                String  Signature       = CryptographicBuffer.EncodeToBase64String(SignatureBuffer);

                FlickrUrl += "?" + SigBaseStringParams + "&oauth_signature=" + Uri.EscapeDataString(Signature);
                string GetResponse = await SendDataAsync(FlickrUrl);

                //rootPage.NotifyUser("Received Data: " + GetResponse, NotifyType.StatusMessage);


                if (GetResponse != null)
                {
                    String   oauth_token        = null;
                    String   oauth_token_secret = null;
                    String[] keyValPairs        = GetResponse.Split('&');

                    for (int i = 0; i < keyValPairs.Length; i++)
                    {
                        String[] splits = keyValPairs[i].Split('=');
                        switch (splits[0])
                        {
                        case "oauth_token":
                            oauth_token = splits[1];
                            break;

                        case "oauth_token_secret":
                            oauth_token_secret = splits[1];
                            break;
                        }
                    }

                    RequestToken = new OAuthRequestToken()
                    {
                        Token = oauth_token, TokenSecret = oauth_token_secret
                    };

                    if (oauth_token != null)
                    {
                        FlickrUrl = "https://secure.flickr.com/services/oauth/authorize?oauth_token=" + oauth_token + "&perms=read";
                        System.Uri StartUri = new Uri(FlickrUrl);
                        System.Uri EndUri   = new Uri(apiKey.APICallbackUrl.Contains("http") ? apiKey.APICallbackUrl : $"http://{apiKey.APICallbackUrl}");

                        //rootPage.NotifyUser("Navigating to: " + FlickrUrl, NotifyType.StatusMessage);
                        WebAuthenticationResult WebAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(
                            WebAuthenticationOptions.None,
                            StartUri,
                            EndUri);

                        if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success)
                        {
                            OutputToken(WebAuthenticationResult.ResponseData.ToString());
                            String[] partsa = WebAuthenticationResult.ResponseData.ToString().Split('?');
                            String[] partsb = partsa[1].Split('&');

                            var xoauth_token    = "";
                            var xoauth_verifier = "";

                            for (int i = 0; i < partsb.Length; i++)
                            {
                                String[] partsc = partsb[i].Split('=');
                                switch (partsc[0])
                                {
                                case "oauth_token":
                                    xoauth_token = partsc[1];
                                    break;

                                case "oauth_verifier":
                                    xoauth_verifier = partsc[1];
                                    break;
                                }
                            }


                            var rat = await _flickr.OAuthGetAccessTokenAsync(RequestToken, xoauth_verifier);

                            if (!rat.HasError)
                            {
                                AccessToken = rat.Result;

                                var dm = new PassportDataModel();
                                dm.Token       = AccessToken.Token;
                                dm.TokenSecret = AccessToken.TokenSecret;
                                dm.Verifier    = xoauth_verifier;
                                dm.PassType    = GroupingType;

                                dm.UserId     = AccessToken.UserId;
                                dm.UserName   = AccessToken.Username;
                                dm.FullName   = AccessToken.FullName;
                                dm.ScreenName = AccessToken.ScreenName;

                                dm.APIKeyFKID = apiKey.Id;


                                StorageService.Instance.Storage.Insert(dm);

                                PopulatePassportData();
                            }
                        }
                        else if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.ErrorHttp)
                        {
                            OutputToken("HTTP Error returned by AuthenticateAsync() : " + WebAuthenticationResult.ResponseErrorDetail.ToString());
                        }
                        else
                        {
                            OutputToken("Error returned by AuthenticateAsync() : " + WebAuthenticationResult.ResponseStatus.ToString());
                        }
                    }
                }
            }
            catch //(Exception Error)
            {
                //rootPage.NotifyUser(Error.Message, NotifyType.ErrorMessage);
            }
        }