Ejemplo n.º 1
0
        public Authenticator(OAuth1Credentials credentials)
        {
            _consumerKey    = credentials.ConsumerKey;
            _consumerSecret = credentials.ConsumerSecret;
            _callbackUrl    = credentials.CallbackUrl;

            OAuthUtility.ComputeHash = (key, buffer) => { using (var hmac = new HMACSHA1(key)) { return(hmac.ComputeHash(buffer)); } };
        }
Ejemplo n.º 2
0
 public OAuthRequester(OAuth1Credentials credentials)
 {
     _requester = new OAuthProtectedResource(
         credentials.ConsumerKey,
         credentials.ConsumerSecret,
         credentials.OAuthToken,
         credentials.OAuthSecret,
         credentials.ParameterHandling);
     _userId = credentials.UserId;
 }
 public async Task AuthenticateWithTwitterAsyncTest()
 {
     var cred = new OAuth1Credentials(
         "consumerKey", "consumer secret",
         "oauthtoken", "oauthtokensecret",
         "twitter"
         );
     cred.CreateUserIfNotExists = true;
     var session = await cred.AuthenticateAsync();
     Assert.IsTrue(string.IsNullOrWhiteSpace(session.UserToken) == false);
     Assert.IsNotNull(session.LoggedInUser);
 }
Ejemplo n.º 4
0
        public async Task AuthenticateWithTwitterAsyncTest()
        {
            var cred = new OAuth1Credentials(
                "consumerKey", "consumer secret",
                "oauthtoken", "oauthtokensecret",
                "twitter"
                );

            cred.CreateUserIfNotExists = true;
            var session = await cred.AuthenticateAsync();

            Assert.IsTrue(string.IsNullOrWhiteSpace(session.UserToken) == false);
            Assert.IsNotNull(session.LoggedInUser);
        }
Ejemplo n.º 5
0
 private bool IsValidOAuth1Token(
     OAuth1Credentials token,
     bool shouldContainUserId = false)
 {
     if (shouldContainUserId)
     {
         Assert.NotNull(token.UserId);
         Assert.NotEmpty(token.UserId);
     }
     return(token != null &&
            token.ConsumerKey != string.Empty &&
            token.ConsumerSecret != string.Empty &&
            token.OAuthToken != string.Empty &&
            token.OAuthSecret != string.Empty);
 }
Ejemplo n.º 6
0
        public IndexModule()
        {
            var _credentials = new OAuth1Credentials();

            _credentials.SetCallbackUrl(ConfigurationManager.AppSettings["WithingsCallbackUrl"]);
            _credentials.SetConsumerProperties(ConfigurationManager.AppSettings["WithingsConsumerKey"],
                                               ConfigurationManager.AppSettings["WithingsConsumerSecret"]);
            _credentials.SetParameterHandling(HttpParameterType.Querystring);

            var authenticator = new Authenticator(_credentials);

            Get["/"] = nothing => new RedirectResponse("api/oauth/authorize", RedirectResponse.RedirectType.Permanent);

            Get["api/oauth/authorize", true] = async(nothing, ct) =>
            {
                var requestToken = await authenticator.GetRequestToken();

                ConfigurationManager.AppSettings["RequestToken"] = JsonConvert.SerializeObject(requestToken);
                return(new RedirectResponse(authenticator.UserRequestUrl(requestToken)));
            };

            Get["api/oauth/callback", true] = async(parameters, ct) =>
            {
                var ps = HttpUtility.ParseQueryString(Request.Url.Query);
                ConfigurationManager.AppSettings["UserId"] = ps["userid"];
                var verifier    = ps["oauth_verifier"];
                var credentials = await authenticator.ExchangeRequestTokenForAccessToken(JsonConvert.DeserializeObject <RequestToken>(ConfigurationManager.AppSettings["RequestToken"]), verifier);

                ConfigurationManager.AppSettings["OAuthToken"]  = credentials.Key;
                ConfigurationManager.AppSettings["OAuthSecret"] = credentials.Secret;
                return(new JsonResponse(credentials, new DefaultJsonSerializer()));
            };

            Get["api/withings/activity"] = nothing =>
            {
                var client   = new WithingsClient(_credentials);
                var activity = client.GetActivityMeasures
                               (
                    DateTime.Parse("2017-01-01"),
                    DateTime.Parse("2017-03-30"),
                    ConfigurationManager.AppSettings["UserId"],
                    ConfigurationManager.AppSettings["OAuthToken"],
                    ConfigurationManager.AppSettings["OAuthSecret"]
                               );
                return(new JsonResponse <WithingsWeighInResponse>(activity, new DefaultJsonSerializer()));
            };

            Get["api/withings/dailyactivity"] = nothing =>
            {
                var client   = new WithingsClient(_credentials);
                var activity = client.GetActivityMeasures
                               (
                    DateTime.Today.AddDays(-30),
                    ConfigurationManager.AppSettings["UserId"],
                    ConfigurationManager.AppSettings["OAuthToken"],
                    ConfigurationManager.AppSettings["OAuthSecret"]
                               );
                return(new JsonResponse <WithingsWeighInResponse>(activity, new DefaultJsonSerializer()));
            };

            Get["api/withings/sleepsummary"] = nothing =>
            {
                var client   = new WithingsClient(_credentials);
                var activity = client.GetSleepSummary
                               (
                    "2017-01-01",
                    "2017-03-30",
                    ConfigurationManager.AppSettings["OAuthToken"],
                    ConfigurationManager.AppSettings["OAuthSecret"]
                               );
                return(new JsonResponse(JsonConvert.SerializeObject(activity), new DefaultJsonSerializer()));
            };

            Get["api/withings/workouts"] = nothing =>
            {
                var client   = new WithingsClient(_credentials);
                var activity = client.GetWorkouts
                               (
                    "2017-01-01",
                    "2017-03-30",
                    ConfigurationManager.AppSettings["OAuthToken"],
                    ConfigurationManager.AppSettings["OAuthSecret"]
                               );
                return(new JsonResponse(JsonConvert.SerializeObject(activity), new DefaultJsonSerializer()));
            };

            Get["api/withings/sleepmeasures"] = nothing =>
            {
                var client   = new WithingsClient(_credentials);
                var activity = client.GetSleepMeasures
                               (
                    ConfigurationManager.AppSettings["UserId"],
                    DateTime.Now.AddDays(-90),
                    DateTime.Now.AddDays(-1),
                    ConfigurationManager.AppSettings["OAuthToken"],
                    ConfigurationManager.AppSettings["OAuthSecret"]
                               );
                return(new JsonResponse(JsonConvert.SerializeObject(activity), new DefaultJsonSerializer()));
            };

            Get["api/withings/body"] = nothing =>
            {
                var client   = new WithingsClient(_credentials);
                var activity = client.GetBodyMeasures
                               (
                    ConfigurationManager.AppSettings["UserId"],
                    DateTime.Now.AddDays(-90),
                    DateTime.Now.AddDays(-1),
                    ConfigurationManager.AppSettings["OAuthToken"],
                    ConfigurationManager.AppSettings["OAuthSecret"]
                               );
                return(new JsonResponse <WithingsBody>(activity, new DefaultJsonSerializer()));
            };

            Get["api/withings/bodysince"] = nothing =>
            {
                var client   = new WithingsClient(_credentials);
                var activity = client.GetBodyMeasures
                               (
                    ConfigurationManager.AppSettings["UserId"],
                    DateTime.Now.AddDays(-90),
                    ConfigurationManager.AppSettings["OAuthToken"],
                    ConfigurationManager.AppSettings["OAuthSecret"]
                               );
                return(new JsonResponse <WithingsBody>(activity, new DefaultJsonSerializer()));
            };

            Get["api/oauth/requesttoken", true] = async(nothing, ct) =>
            {
                var token = await authenticator.GetRequestToken();

                return(new JsonResponse <RequestToken>(token, new DefaultJsonSerializer()));
            };
        }
 public WithingsClient(OAuth1Credentials credentials)
 {
     Client       = new RestClient(BaseUri);
     _credentials = credentials;
 }