// // Copyright (c) 2016 Couchbase, Inc. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file // except in compliance with the License. You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software distributed under the // License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, // either express or implied. See the License for the specific language governing permissions // and limitations under the License. // package com.couchbase.lite.auth; import com.couchbase.lite.Database; import com.couchbase.lite.LiteTestCase; import com.couchbase.lite.util.Log; import junit.framework.Assert; import java.net.URL; import java.util.HashMap; import java.util.List; import java.util.Map; public class AuthTest extends LiteTestCase { public void testParsePersonaAssertion() { try { Log.d(Database.TAG, "testParsePersonaAssertion"); String sampleAssertion = "eyJhbGciOiJSUzI1NiJ9.eyJwdWJsaWMta2V5Ijp7ImFsZ29yaXRobSI6IkRTIiwieSI6ImNhNWJiYTYzZmI4MDQ2OGE0MjFjZjgxYTIzN2VlMDcwYTJlOTM4NTY0ODhiYTYzNTM0ZTU4NzJjZjllMGUwMDk0ZWQ2NDBlOGNhYmEwMjNkYjc5ODU3YjkxMzBlZGNmZGZiNmJiNTUwMWNjNTk3MTI1Y2NiMWQ1ZWQzOTVjZTMyNThlYjEwN2FjZTM1ODRiOWIwN2I4MWU5MDQ4NzhhYzBhMjFlOWZkYmRjYzNhNzNjOTg3MDAwYjk4YWUwMmZmMDQ4ODFiZDNiOTBmNzllYzVlNDU1YzliZjM3NzFkYjEzMTcxYjNkMTA2ZjM1ZDQyZmZmZjQ2ZWZiZDcwNjgyNWQiLCJwIjoiZmY2MDA0ODNkYjZhYmZjNWI0NWVhYjc4NTk0YjM1MzNkNTUwZDlmMWJmMmE5OTJhN2E4ZGFhNmRjMzRmODA0NWFkNGU2ZTBjNDI5ZDMzNGVlZWFhZWZkN2UyM2Q0ODEwYmUwMGU0Y2MxNDkyY2JhMzI1YmE4MWZmMmQ1YTViMzA1YThkMTdlYjNiZjRhMDZhMzQ5ZDM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hbGciOiJEUzEyOCJ9.eyJleHAiOjEzNTgyOTY0Mzg0OTUsImF1ZCI6Imh0dHA6Ly9sb2NhbGhvc3Q6NDk4NC8ifQ.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.assertion()); } 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 PasswordAuthorizer); Authenticator facebookAuth = AuthenticatorFactory.createFacebookAuthenticator("DUMMY_TOKEN"); Assert.assertNotNull(facebookAuth); Assert.assertTrue(facebookAuth instanceof TokenAuthenticator); Authenticator personalAuth = AuthenticatorFactory.createPersonaAuthenticator("DUMMY_ASSERTION"); 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); List<Object> login = tokenAuth.loginRequest(); assertNotNull(login); assertEquals(3, login.size()); assertEquals("POST", login.get(0)); assertEquals(loginPath, login.get(1)); assertNotNull(login.get(2)); assertTrue(login.get(2) instanceof Map); assertEquals(params.get("access_token"), ((Map) login.get(2)).get("access_token")); } public void testBasicAuthenticator() { String username = "username"; String password = "password"; PasswordAuthorizer basicAuth = new PasswordAuthorizer(username, password); Assert.assertEquals(username + ":" + password, basicAuth.authUserInfo()); // test deprecated class basicAuth = new BasicAuthenticator(username, password); assertEquals(username + ":" + password, basicAuth.authUserInfo()); } }