public void TestGetAuthenticationHeaderValue()
        {
            var username1  = "username1";
            var password1  = "password1";
            var credParam1 = Convert.ToBase64String(
                Encoding.UTF8.GetBytes(string.Format("{0}:{1}", username1, password1)));

            var auth       = AuthenticatorFactory.CreateBasicAuthenticator(username1, password1);
            var authHeader = AuthUtils.GetAuthenticationHeaderValue(auth, null);

            Assert.IsNotNull(authHeader);
            Assert.AreEqual(credParam1, authHeader.Parameter);

            var username2  = "username2";
            var password2  = "password2";
            var credParam2 = Convert.ToBase64String(
                Encoding.UTF8.GetBytes(string.Format("{0}:{1}", username2, password2)));

            var userinfo = username2 + ":" + password2;
            var uri      = new Uri("http://" + userinfo + "@couchbase.com");
            var request  = new HttpRequestMessage(HttpMethod.Get, uri);

            authHeader = AuthUtils.GetAuthenticationHeaderValue(auth, request.RequestUri);
            Assert.IsNotNull(authHeader);
            Assert.AreEqual(credParam2, authHeader.Parameter);

            uri        = new Uri("http://www.couchbase.com");
            request    = new HttpRequestMessage(HttpMethod.Get, uri);
            authHeader = AuthUtils.GetAuthenticationHeaderValue(null, request.RequestUri);
            Assert.IsNull(authHeader);

            auth       = AuthenticatorFactory.CreateFacebookAuthenticator("1234");
            authHeader = AuthUtils.GetAuthenticationHeaderValue(auth, null);
            Assert.IsNull(authHeader);
        }
示例#2
0
        public void TestGetAuthenticationHeaderValue()
        {
            var username1  = "username1";
            var password1  = "password1";
            var credParam1 = Convert.ToBase64String(
                Encoding.UTF8.GetBytes(string.Format("{0}:{1}", username1, password1)));

            var auth       = AuthenticatorFactory.CreateBasicAuthenticator(username1, password1) as BasicAuthenticator;
            var authHeader = auth.AuthorizationHeaderValue;

            Assert.IsNotNull(authHeader);
            Assert.AreEqual(credParam1, authHeader.Split(' ')[1]);

            var username2  = "username2";
            var password2  = "password2";
            var credParam2 = Convert.ToBase64String(
                Encoding.UTF8.GetBytes(string.Format("{0}:{1}", username2, password2)));

            var userinfo = username2 + ":" + password2;
            var uri      = new Uri("http://" + userinfo + "@couchbase.com");

            auth       = (BasicAuthenticator)AuthenticatorFactory.CreateFromUri(uri);
            authHeader = auth.AuthorizationHeaderValue;
            Assert.IsNotNull(authHeader);
            Assert.AreEqual(credParam2, authHeader.Split(' ')[1]);

            uri  = new Uri("http://www.couchbase.com");
            auth = (BasicAuthenticator)AuthenticatorFactory.CreateFromUri(uri);
            Assert.IsNull(auth);

            var auth2 = AuthenticatorFactory.CreateFacebookAuthenticator("1234") as ICustomHeadersAuthorizer;

            Assert.IsNull(auth2);
        }
示例#3
0
        public void TestReplicatorErrorStatus()
        {
            var email       = "*****@*****.**";
            var accessToken = "fake_access_token";
            var remoteUrl   = GetReplicationURL().ToString();

            FacebookAuthorizer.RegisterAccessToken(accessToken, email, remoteUrl);

            var replicator = database.CreatePullReplication(GetReplicationURL());

            replicator.Authenticator = AuthenticatorFactory.CreateFacebookAuthenticator(accessToken);

            RunReplication(replicator);

            Assert.IsNotNull(replicator.LastError);
        }
示例#4
0
        public virtual void StartReplicationSyncWithFacebookLogin(string accessToken, string
                                                                  email)
        {
            Authenticator facebookAuthenticator = AuthenticatorFactory.CreateFacebookAuthenticator
                                                      (accessToken);

            Replication[] replications = CreateReplications();
            Replication   pullRep      = replications[0];
            Replication   pushRep      = replications[1];

            pullRep.SetAuthenticator(facebookAuthenticator);
            pushRep.SetAuthenticator(facebookAuthenticator);
            pullRep.Start();
            pushRep.Start();
            Log.V(Tag, "Start Replication Sync ...");
        }
        public void TestAuthenticationFactory()
        {
            var basicAuth = AuthenticatorFactory.CreateBasicAuthenticator("username", "password");

            Assert.IsNotNull(basicAuth);
            Assert.IsTrue(basicAuth is BasicAuthenticator);

            var facebookAuth = AuthenticatorFactory.CreateFacebookAuthenticator("DUMMY_TOKEN");

            Assert.IsNotNull(facebookAuth);
            Assert.IsTrue(facebookAuth is TokenAuthenticator);

            var personalAuth = AuthenticatorFactory.CreatePersonaAuthenticator("DUMMY_ASSERTION", null);

            Assert.IsNotNull(personalAuth);
            Assert.IsTrue(personalAuth is TokenAuthenticator);
        }
示例#6
0
        public void TestGetReplicatorWithAuth()
        {
            var email       = "*****@*****.**";
            var accessToken = "fake_access_token";
            var remoteUrl   = GetReplicationURL().ToString();

            FacebookAuthorizer.RegisterAccessToken(accessToken, email, remoteUrl);

            var         url        = GetReplicationURLWithoutCredentials();
            Replication replicator = database.CreatePushReplication(url);

            replicator.Authenticator = AuthenticatorFactory.CreateFacebookAuthenticator(accessToken);

            Assert.IsNotNull(replicator);
            Assert.IsNotNull(replicator.Authenticator);
            Assert.IsTrue(replicator.Authenticator is TokenAuthenticator);
        }
示例#7
0
        public virtual void TestAuthenticatorFactory()
        {
            Authenticator basicAuth = AuthenticatorFactory.CreateBasicAuthenticator("username"
                                                                                    , "password");

            NUnit.Framework.Assert.IsNotNull(basicAuth);
            NUnit.Framework.Assert.IsTrue(basicAuth is BasicAuthenticator);
            Authenticator facebookAuth = AuthenticatorFactory.CreateFacebookAuthenticator("DUMMY_TOKEN"
                                                                                          );

            NUnit.Framework.Assert.IsNotNull(facebookAuth);
            NUnit.Framework.Assert.IsTrue(facebookAuth is TokenAuthenticator);
            Authenticator personalAuth = AuthenticatorFactory.CreatePersonaAuthenticator("DUMMY_ASSERTION"
                                                                                         , null);

            NUnit.Framework.Assert.IsNotNull(personalAuth);
            NUnit.Framework.Assert.IsTrue(personalAuth is TokenAuthenticator);
        }
        public void TestFacebookAuth()
        {
            var doneEvent = new ManualResetEvent(false);

            var accountStore = new ACAccountStore();
            var accountType  = accountStore.FindAccountType(ACAccountType.Facebook);

            var options = new AccountStoreOptions();

            options.FacebookAppId = FacebookAppId;
            options.SetPermissions(ACFacebookAudience.Friends, new [] { "email" });

            var       success = true;
            ACAccount account = null;

            accountStore.RequestAccess(accountType, options, (result, error) =>
            {
                success = result;
                if (success)
                {
                    var accounts = accountStore.FindAccounts(accountType);
                    account      = accounts != null && accounts.Length > 0 ? accounts[0] : null;
                }
                else
                {
                    Log.W(Tag, "Facebook Login needed. Go to Settings > Facebook and login.");
                    Log.E(Tag, "Facebook Request Access Error : " + error);
                }
                doneEvent.Set();
            });

            doneEvent.WaitOne(TimeSpan.FromSeconds(30));

            Assert.IsTrue(success);
            Assert.IsNotNull(account);

            var token = account.Credential.OAuthToken;

            Assert.IsNotNull(token);
            Assert.IsTrue(token.Length > 0);

            var url = GetReplicationURLWithoutCredentials();

            var cookieStore       = new CookieStore();
            var httpClientFactory = new CouchbaseLiteHttpClientFactory(cookieStore);

            manager.DefaultHttpClientFactory = httpClientFactory;
            Replication replicator = database.CreatePushReplication(url);

            replicator.Authenticator = AuthenticatorFactory.CreateFacebookAuthenticator(token);

            Assert.IsNotNull(replicator);
            Assert.IsNotNull(replicator.Authenticator);
            Assert.IsTrue(replicator.Authenticator is TokenAuthenticator);

            replicator.Start();

            doneEvent.Reset();
            Task.Factory.StartNew(() =>
            {
                var timeout = DateTime.UtcNow + TimeSpan.FromSeconds(30);
                while (DateTime.UtcNow < timeout)
                {
                    if (!replicator.active)
                    {
                        break;
                    }
                    System.Threading.Thread.Sleep(TimeSpan.FromMilliseconds(10));
                }
                doneEvent.Set();
            });
            doneEvent.WaitOne(TimeSpan.FromSeconds(35));

            var urlStr = url.ToString();

            urlStr = urlStr.EndsWith("/") ? urlStr : urlStr + "/";
            var cookies = httpClientFactory.GetCookieContainer().GetCookies(new Uri(urlStr));

            Assert.IsTrue(cookies.Count == 1);
            Assert.AreEqual("SyncGatewaySession", cookies[0].Name);
        }
示例#9
0
        public void TestFacebookAuth()
        {
            var sg = new SyncGateway("http", GetReplicationServer());

            using (var remoteDb = sg.CreateDatabase("facebook")) {
                remoteDb.DisableGuestAccess();
                var doneEvent = new ManualResetEvent(false);

                var accountStore = new ACAccountStore();
                var accountType  = accountStore.FindAccountType(ACAccountType.Facebook);

                var options = new AccountStoreOptions();
                options.FacebookAppId = FacebookAppId;
                options.SetPermissions(ACFacebookAudience.Friends, new [] { "email" });

                var       success = true;
                ACAccount account = null;
                accountStore.RequestAccess(accountType, options, (result, error) =>
                {
                    success = result;
                    if (success)
                    {
                        var accounts = accountStore.FindAccounts(accountType);
                        account      = accounts != null && accounts.Length > 0 ? accounts[0] : null;
                    }
                    else
                    {
                        Log.W(Tag, "Facebook Login needed. Go to Settings > Facebook and login.");
                        Log.E(Tag, "Facebook Request Access Error : " + error);
                    }
                    doneEvent.Set();
                });

                doneEvent.WaitOne(TimeSpan.FromSeconds(30));

                Assert.IsTrue(success);
                Assert.IsNotNull(account);

                var token = account.Credential.OAuthToken;
                Assert.IsNotNull(token);
                Assert.IsTrue(token.Length > 0);

                var url = remoteDb.RemoteUri;

                var cookieStore       = new CookieStore(manager.Directory);
                var httpClientFactory = new CouchbaseLiteHttpClientFactory(cookieStore);
                manager.DefaultHttpClientFactory = httpClientFactory;
                Replication replicator = database.CreatePushReplication(url);
                replicator.Authenticator = AuthenticatorFactory.CreateFacebookAuthenticator(token);

                Assert.IsNotNull(replicator);
                Assert.IsNotNull(replicator.Authenticator);
                Assert.IsTrue(replicator.Authenticator is TokenAuthenticator);

                CreateDocuments(database, 20);

                RunReplication(replicator);

                var urlStr = url.ToString();
                urlStr = urlStr.EndsWith("/") ? urlStr : urlStr + "/";
                //var cookies = httpClientFactory.GetCookieContainer().GetCookies(new Uri(urlStr));
                //Assert.IsTrue(cookies.Count == 1);
                //Assert.AreEqual("SyncGatewaySession", cookies[0].Name);
            }
        }