Exemple #1
0
        public TokenGetter(NestConfig nestConfig)
        {
            this.oauth2     = new NestOauth2(nestConfig);
            this.nestConfig = nestConfig;

            this.nestToken = null;
        }
Exemple #2
0
        public void testEquals_shouldReturnFalseWithNonNestToken()
        {
            var o = new Object();
            var t = new NestToken();

            Assert.IsFalse(t.Equals(o));
        }
Exemple #3
0
        public void testNestToken_shouldReturnCorrectValuesWhenSet()
        {
            var token = new NestToken(TEST_TOKEN, TEST_EXPIRES_IN);

            Assert.AreEqual(TEST_TOKEN, token.Token);
            Assert.AreEqual(TEST_EXPIRES_IN, token.ExpiresInSecs);
        }
Exemple #4
0
        /// <summary>
        /// Start an <see cref="Activity"/> which will guide a user through the authentication process.
        /// </summary>
        /// <param name="activity">the <see cref="Activity"/> return the result. Typically the current <see cref="Activity"/>.</param>
        /// <param name="requestCode">the request code for which a result will be returned.</param>
        //public void launchAuthFlow(Activity activity, int requestCode)
        //{
        //    final Intent authFlowIntent = new Intent(activity, NestAuthActivity.class);
        //    authFlowIntent.putExtra(KEY_CLIENT_METADATA, oauth2Config);
        //    activity.startActivityForResult(authFlowIntent, requestCode);
        //}

        /// <summary>
        /// Returns a <see cref="NestToken"/> embedded in the <see cref="Intent"/> that is returned in the result
        /// from <see cref="#launchAuthFlow(Activity, int)"/>.
        /// </summary>
        /// <param name="intent">the intent to retrieve the NestToken from.</param>
        /// <value">the <see cref="NestToken"/>, if it was contained in the <see cref="Intent"/>, otherwise null.</value>
        //public NestToken getAccessTokenFromIntent(Intent intent)
        //{
        //    return intent.getParcelableExtra(KEY_ACCESS_TOKEN);
        //}

        /// <summary>
        /// Revokes a <see cref="NestToken"/> from the Nest API.
        /// </summary>
        /// <param name="token">The token to revoke.</param>
        /// <param name="callback">A callback for the result of the revocation.</param>
        public void RevokeToken(NestToken token, Callback callback)
        {
            var request = new HttpRequestMessage(HttpMethod.Delete,
                                                 requestUri: AUTHORIZATION_SERVER_URL + REVOKE_TOKEN_PATH + token.Token);
            HttpResponseMessage response = null;

            try
            {
                response = httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false).GetAwaiter().GetResult();
                //response.EnsureSuccessStatusCode();

                if (response.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    callback.OnFailure?.Invoke(
                        new ServerException("Token revocation failed: " + response.StatusCode));
                }
                else
                {
                    callback.OnSuccess();
                }
            }
            catch (Exception ex)
            {
                callback.OnFailure?.Invoke(new NestException("Request to revoke token failed.", ex));
            }
            finally
            {
                if (response != null)
                {
                    response.Dispose();
                }
            }
        }
Exemple #5
0
    public void testGetAccessTokenFromIntent_shouldReturnToken() {
        var intent = new Intent();
        var token = new NestToken("token", 123);
        intent.putExtra("access_token_key", token);

        var tokenFromIntent = new Oauth2FlowHandler(new OkHttpClient()).getAccessTokenFromIntent(intent);
        Assert.IsNotNull(tokenFromIntent);
        Assert.AreEqual(token, tokenFromIntent);
    }
Exemple #6
0
        public void testCameraToParcel()
        {
            string testToken     = "test-token";
            long   testExpiresIn = 123;

            NestToken token = new NestToken(testToken, testExpiresIn);

            //Parcel parcel = Parcel.obtain();
            //token.writeToParcel(parcel, 0);

            //parcel.setDataPosition(0);

            //NestToken tokenFromParcel = NestToken.CREATOR.createFromParcel(parcel);
            //Assert.AreEqual(token, tokenFromParcel);
        }
Exemple #7
0
        void StartHttpListener()
        {
            var uriPrefix = new Uri(nestConfig.RedirectUrl).GetLeftPart(UriPartial.Authority) + "/";

            httpListener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
            httpListener.Prefixes.Add(uriPrefix);
            httpListener.Start();
            new Thread(() =>
            {
                while (true)
                {
                    HttpListenerContext httpListenerContext = httpListener.GetContext();

                    var queryString = httpListenerContext.Request.QueryString;

                    try
                    {
                        var code       = oauth2.ParseAuthorizationCode(httpListenerContext.Request.Url.OriginalString);
                        this.nestToken = oauth2.CreateToken(code);

                        log.Info("AccessToken: " + nestToken?.Token);

                        httpListenerContext.Response.StatusCode = 200;
                        using (var writer = new StreamWriter(httpListenerContext.Response.OutputStream))
                        {
                            WriteHtml(writer,
                                      "Finished",
                                      "Well done, you now have an access token which allows you to call Web API on behalf of the user.<br />Please return to the application.");
                        }

                        resetEvent.Set();
                        break;
                    }
                    catch (Exception ex)
                    {
                        log.Error("Failed to create token.", ex);
                        using (var writer = new StreamWriter(httpListenerContext.Response.OutputStream))
                        {
                            WriteHtml(writer,
                                      "Failed",
                                      "Failed to create token.<br />" + ex.StackTrace);
                        }
                    }
                }
            }).Start();
        }
Exemple #8
0
 public void testDescribeContents_shouldReturnZero()
 {
     var t = new NestToken();
     //Assert.AreEqual(t.describeContents(), 0);
 }
Exemple #9
0
        public void testNestToken_shouldReturnZeroExpiresInWhenNotSet()
        {
            var token = new NestToken();

            Assert.AreEqual(0, token.ExpiresInSecs);
        }
Exemple #10
0
        public void testNestToken_shouldReturnNullTokenWhenNotSet()
        {
            var token = new NestToken();

            Assert.AreEqual(null, token.Token);
        }
Exemple #11
0
 /// <summary>
 /// Requests authentication with a <see cref="NestToken"/>.
 /// </summary>
 /// <param name="token">the NestToken to authenticate with</param>
 public void StartWithToken(NestToken token)
 {
     StartWithToken(token.Token);
 }