public void EnsuresArgumentsNotNull()
            {
                var authenticator = new TokenAuthenticator();

                Assert.Throws <ArgumentNullException>(() => authenticator.Authenticate(null, Credentials.Anonymous));
                Assert.Throws <ArgumentNullException>(() =>
                                                      authenticator.Authenticate(Substitute.For <IRequest>(), null));
            }
            public void EnsuresCredentialsAreOfTheRightType()
            {
                var authenticator = new TokenAuthenticator();
                var request       = new Request();

                Assert.Throws <InvalidOperationException>(() =>
                                                          authenticator.Authenticate(request, new Credentials("login", "password")));
            }
            public void SetsRequestHeaderForToken()
            {
                var authenticator = new TokenAuthenticator();
                var request       = new Request();

                authenticator.Authenticate(request, new Credentials("abcda1234a"));

                Assert.Contains("Authorization", request.Headers.Keys);
                Assert.Equal("Token abcda1234a", request.Headers["Authorization"]);
            }
Exemple #4
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="BuxferClient" /> class.
        /// </summary>
        /// <param name="userId">The user identifier.</param>
        /// <param name="password">The password.</param>
        /// <param name="logger">The logger. Could be ignored to disable any log output</param>
        public BuxferClient(string userName, string password, ILogger logger = null)
        {
            _logger = logger ?? new NullLogger <BuxferClient>();

            var authenticator = new TokenAuthenticator(userName, password,
                                                       (resource, method) => CreateRequestBuilder(resource, method).Request,
                                                       async r => await ExecuteRequestAsync <LoginResponse>(r), _logger);

            Init(authenticator);
        }
Exemple #5
0
        public void Apply_AddsCorrectHeader()
        {
            var authenticator = new TokenAuthenticator("MyToken", "Bearer");
            var request       = new Mock <IRequest>();
            var apiConnector  = new Mock <IAPIConnector>();

            request.SetupGet(r => r.Headers).Returns(new Dictionary <string, string>());

            authenticator.Apply(request.Object, apiConnector.Object);
            Assert.AreEqual(request.Object.Headers["Authorization"], "Bearer MyToken");
        }
        public bool LoginUser(String userName, String password)
        {
            bool loginOK = false;

            this.LoginData.LoginToken = TokenAuthenticator.AuthenticateUser(this.serviceURL, password, userName);
            this.webApiHelper         = new WebApiHelper(this.LoginData.LoginToken);
            if (this.LoginData.LoginToken != null && !String.IsNullOrEmpty(this.LoginData.LoginToken.AccessToken))
            {
                loginOK = true;
            }

            return(loginOK);
        }
        public void TestTokenAuthenticator()
        {
            var loginPath = "_facebook";

            var parameters = new Dictionary <string, string>();

            parameters["access_token"] = "facebookaccesstoken";
            TokenAuthenticator tokenAuth = new TokenAuthenticator(loginPath, parameters);

            var tokenAuthParams = tokenAuth.LoginParametersForSite(null);

            Assert.IsNotNull(tokenAuthParams);
            Assert.AreEqual(tokenAuthParams.Count, parameters.Count);
            Assert.AreEqual(tokenAuthParams["access_token"], parameters["access_token"]);
            Assert.AreEqual(tokenAuth.LoginPathForSite(null), "/_facebook");
            Assert.IsTrue(tokenAuth.UsesCookieBasedLogin);
            Assert.IsNull(tokenAuth.UserInfo);
        }
Exemple #8
0
        public virtual void TestTokenAuthenticator()
        {
            string loginPath = "_facebook";
            IDictionary <string, string> @params = new Dictionary <string, string>();

            @params.Put("access_token", "facebookaccesstoken");
            TokenAuthenticator           tokenAuth       = new TokenAuthenticator(loginPath, @params);
            IDictionary <string, string> tokenAuthParams = tokenAuth.LoginParametersForSite(null
                                                                                            );

            NUnit.Framework.Assert.IsNotNull(tokenAuthParams);
            NUnit.Framework.Assert.AreEqual(tokenAuthParams.Count, @params.Count);
            NUnit.Framework.Assert.AreEqual(tokenAuthParams.Get("access_token"), @params.Get(
                                                "access_token"));
            NUnit.Framework.Assert.AreEqual(tokenAuth.LoginPathForSite(null), "/_facebook");
            NUnit.Framework.Assert.IsTrue(tokenAuth.UsesCookieBasedLogin());
            NUnit.Framework.Assert.IsNull(tokenAuth.AuthUserInfo());
        }
        protected virtual void ReceiveAccountCredentials()
        {
            var wizardStateKey = this.Request.QueryString[Constants.QueryString.State];
            var index          = string.Format("WizardTokenParameters_{0}", wizardStateKey);
            var tokenindex     = string.Format("WizardAuthenticationToken_{0}", wizardStateKey);

            if (!string.IsNullOrEmpty(this.Request.QueryString[Constants.QueryString.Error]))
            {
                var collection = new NameValueCollection {
                    { "ErrorMessage", this.Request.QueryString[Constants.QueryString.Error] }
                };
                this.Session[tokenindex] = collection;
            }
            else if (!string.IsNullOrEmpty(this.Request.QueryString[Constants.QueryString.Code]))
            {
                if (this.Session[index] is SafeDictionary <string> )
                {
                    var parameters = this.Session[index] as SafeDictionary <string>;
                    parameters.Add("code", this.Request.QueryString[Constants.QueryString.Code]);
                    parameters.Add("redirect_uri", WebUtil.GetFullUrl(Constants.Authentication.RedirectUri));

                    var authenticator = new TokenAuthenticator();
                    var response      = authenticator.Authenticate(parameters);

                    if (response != null && response.StatusCode == HttpStatusCode.OK)
                    {
                        var token = response.Data;
                        this.Session[tokenindex] = token;
                    }
                    else
                    {
                        var message    = response == null ? "Unauthorized" : response.StatusDescription;
                        var collection = new NameValueCollection {
                            { "ErrorMessage", message }
                        };
                        this.Session[tokenindex] = collection;
                    }
                }
            }

            this.CloseWindow();
        }