Back to project page couchbase-lite-android.
The source code is released under:
Apache License
If you think the Android project couchbase-lite-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.couchbase.lite; //from w ww . j a v a2 s .c om import com.couchbase.lite.auth.Authenticator; import com.couchbase.lite.auth.AuthenticatorFactory; import com.couchbase.lite.auth.BasicAuthenticator; import com.couchbase.lite.auth.PersonaAuthorizer; import com.couchbase.lite.auth.TokenAuthenticator; import com.couchbase.lite.util.Log; import junit.framework.Assert; import junit.framework.TestCase; import java.net.URL; import java.util.HashMap; import java.util.Map; public class AuthTest extends TestCase { public void testParsePersonaAssertion() { try { Log.d(Database.TAG, "testParsePersonaAssertion"); String sampleAssertion = "eyJhbGciOiJSUzI1NiJ9.eyJwdWJsaWMta2V5Ijp7ImFsZ29yaXRobSI6IkRTIiwieSI6ImNhNWJiYTYzZmI4MDQ2OGE0MjFjZjgxYTIzN2VlMDcwYTJlOTM4NTY0ODhiYTYzNTM0ZTU4NzJjZjllMGUwMDk0ZWQ2NDBlOGNhYmEwMjNkYjc5ODU3YjkxMzBlZGNmZGZiNmJiNTUwMWNjNTk3MTI1Y2NiMWQ1ZWQzOTVjZTMyNThlYjEwN2FjZTM1ODRiOWIwN2I4MWU5MDQ4NzhhYzBhMjFlOWZkYmRjYzNhNzNjOTg3MDAwYjk4YWUwMmZmMDQ4ODFiZDNiOTBmNzllYzVlNDU1YzliZjM3NzFkYjEzMTcxYjNkMTA2ZjM1ZDQyZmZmZjQ2ZWZiZDcwNjgyNWQiLCJwIjoiZmY2MDA0ODNkYjZhYmZjNWI0NWVhYjc4NTk0YjM1MzNkNTUwZDlmMWJmMmE5OTJhN2E4ZGFhNmRjMzRmODA0NWFkNGU2ZTBjNDI5ZDMzNGVlZWFhZWZkN2UyM2Q0ODEwYmUwMGU0Y2MxNDkyY2JhMzI1YmE4MWZmMmQ1YTViMzA1YThkMTdlYjNiZjRhMDZhMzQ5ZDM5MmUwMGQzMjk3NDRhNTE3OT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AiOjEzNTgyOTY0Mzg0OTUsImF1ZCI6Imh0dHA6Ly9sb2NhbGhvc3Q6NDk4NC8ifQ.4FV2TrUQffDya0MOxOQlzJQbDNvCPF2sfTIJN7KOLvvlSFPknuIo5g"; Map<String, Object> result = PersonaAuthorizer.parseAssertion(sampleAssertion); String email = (String) result.get(PersonaAuthorizer.ASSERTION_FIELD_EMAIL); String origin = (String) result.get(PersonaAuthorizer.ASSERTION_FIELD_ORIGIN); Assert.assertEquals(email, "jens@mooseyard.com"); Assert.assertEquals(origin, "http://localhost:4984/"); Assert.assertEquals(PersonaAuthorizer.registerAssertion(sampleAssertion), email); URL originURL = new URL(origin); String gotAssertion = PersonaAuthorizer.assertionForEmailAndSite(email, originURL); Assert.assertEquals(gotAssertion, sampleAssertion); // variant form of URL originURL = new URL("Http://LocalHost:4984/"); gotAssertion = PersonaAuthorizer.assertionForEmailAndSite(email, originURL); Assert.assertEquals(sampleAssertion, gotAssertion); PersonaAuthorizer auth = new PersonaAuthorizer(email); Assert.assertEquals(email, auth.getEmailAddress()); Assert.assertEquals(null, auth.assertionForSite(originURL)); } catch (Exception e) { e.printStackTrace(); Assert.fail(e.getMessage()); } } public void testAuthenticatorFactory() { Authenticator basicAuth = AuthenticatorFactory.createBasicAuthenticator("username", "password"); Assert.assertNotNull(basicAuth); Assert.assertTrue(basicAuth instanceof BasicAuthenticator); Authenticator facebookAuth = AuthenticatorFactory.createFacebookAuthenticator("DUMMY_TOKEN"); Assert.assertNotNull(facebookAuth); Assert.assertTrue(facebookAuth instanceof TokenAuthenticator); Authenticator personalAuth = AuthenticatorFactory.createPersonaAuthenticator("DUMMY_ASSERTION", null); Assert.assertNotNull(personalAuth); Assert.assertTrue(personalAuth instanceof TokenAuthenticator); } public void testTokenAuthenticator() { String loginPath = "_facebook"; Map<String, String> params = new HashMap<String, String>(); params.put("access_token", "facebookaccesstoken"); TokenAuthenticator tokenAuth = new TokenAuthenticator(loginPath, params); Map<String, String> tokenAuthParams = tokenAuth.loginParametersForSite(null); Assert.assertNotNull(tokenAuthParams); Assert.assertEquals(tokenAuthParams.size(), params.size()); Assert.assertEquals(tokenAuthParams.get("access_token"), params.get("access_token")); Assert.assertEquals(tokenAuth.loginPathForSite(null), "/_facebook"); Assert.assertTrue(tokenAuth.usesCookieBasedLogin()); Assert.assertNull(tokenAuth.authUserInfo()); } public void testBasicAuthenticator() { String username = "username"; String password = "password"; BasicAuthenticator basicAuth = new BasicAuthenticator(username, password); Assert.assertNull(basicAuth.loginParametersForSite(null)); Assert.assertTrue(basicAuth.usesCookieBasedLogin()); Assert.assertEquals(basicAuth.authUserInfo(), username + ":" + password); } }