Added: 
qpid/java/trunk/client/src/main/java/org/apache/qpid/client/security/oauth2/OAuth2SaslClientFactory.java
URL: 
http://svn.apache.org/viewvc/qpid/java/trunk/client/src/main/java/org/apache/qpid/client/security/oauth2/OAuth2SaslClientFactory.java?rev=1729215&view=auto
==============================================================================
--- 
qpid/java/trunk/client/src/main/java/org/apache/qpid/client/security/oauth2/OAuth2SaslClientFactory.java
 (added)
+++ 
qpid/java/trunk/client/src/main/java/org/apache/qpid/client/security/oauth2/OAuth2SaslClientFactory.java
 Mon Feb  8 17:29:44 2016
@@ -0,0 +1,57 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.qpid.client.security.oauth2;
+
+
+import java.util.Map;
+
+import javax.security.auth.callback.CallbackHandler;
+import javax.security.sasl.SaslClient;
+import javax.security.sasl.SaslClientFactory;
+import javax.security.sasl.SaslException;
+
+public class OAuth2SaslClientFactory implements SaslClientFactory
+{
+    @Override
+    public SaslClient createSaslClient(String[] mechanisms, String 
authorizationId, String protocol, String serverName,
+                                       Map<String, ?> props, CallbackHandler 
callbackHandler) throws SaslException
+    {
+        for(String mech : mechanisms)
+        {
+            if (OAuth2SaslClient.MECHANISM.equals(mech))
+            {
+                if (callbackHandler == null)
+                {
+                    throw new SaslException("CallbackHandler must not be 
null");
+                }
+                return new OAuth2SaslClient(callbackHandler);
+            }
+        }
+        return null;
+    }
+
+    @Override
+    public String[] getMechanismNames(Map<String, ?> props)
+    {
+        return new String[] {OAuth2SaslClient.MECHANISM};
+    }
+}

Added: 
qpid/java/trunk/client/src/test/java/org/apache/qpid/client/security/oauth2/OAuth2AccessTokenCallbackHandlerTest.java
URL: 
http://svn.apache.org/viewvc/qpid/java/trunk/client/src/test/java/org/apache/qpid/client/security/oauth2/OAuth2AccessTokenCallbackHandlerTest.java?rev=1729215&view=auto
==============================================================================
--- 
qpid/java/trunk/client/src/test/java/org/apache/qpid/client/security/oauth2/OAuth2AccessTokenCallbackHandlerTest.java
 (added)
+++ 
qpid/java/trunk/client/src/test/java/org/apache/qpid/client/security/oauth2/OAuth2AccessTokenCallbackHandlerTest.java
 Mon Feb  8 17:29:44 2016
@@ -0,0 +1,54 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.qpid.client.security.oauth2;
+
+import javax.security.auth.callback.Callback;
+import javax.security.auth.callback.PasswordCallback;
+
+import org.junit.Assert;
+
+import org.apache.qpid.client.AMQConnectionURL;
+import org.apache.qpid.client.security.AMQCallbackHandler;
+import org.apache.qpid.test.utils.QpidTestCase;
+
+public class OAuth2AccessTokenCallbackHandlerTest extends QpidTestCase
+{
+    private static final String ACCESS_TOKEN = "accessToken";
+    private AMQCallbackHandler _callbackHandler = new 
OAuth2AccessTokenCallbackHandler(); // Class under test
+
+    @Override
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+        String url = 
String.format("amqp://:%s@client/test?brokerlist='tcp://localhost:1'", 
ACCESS_TOKEN);
+        _callbackHandler.initialise(new AMQConnectionURL(url));
+    }
+
+    public void testAccessTokenCallback() throws Exception
+    {
+        PasswordCallback callback = new PasswordCallback("prompt", false);
+
+        assertNull("Unexpected access token before test", 
callback.getPassword());
+        _callbackHandler.handle(new Callback[] {callback});
+        Assert.assertArrayEquals("Unexpected access token", 
ACCESS_TOKEN.toCharArray(), callback.getPassword());
+    }
+}

Added: 
qpid/java/trunk/client/src/test/java/org/apache/qpid/client/security/oauth2/OAuth2SaslClientTest.java
URL: 
http://svn.apache.org/viewvc/qpid/java/trunk/client/src/test/java/org/apache/qpid/client/security/oauth2/OAuth2SaslClientTest.java?rev=1729215&view=auto
==============================================================================
--- 
qpid/java/trunk/client/src/test/java/org/apache/qpid/client/security/oauth2/OAuth2SaslClientTest.java
 (added)
+++ 
qpid/java/trunk/client/src/test/java/org/apache/qpid/client/security/oauth2/OAuth2SaslClientTest.java
 Mon Feb  8 17:29:44 2016
@@ -0,0 +1,79 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.qpid.client.security.oauth2;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import javax.security.sasl.SaslException;
+
+import org.junit.Assert;
+
+import org.apache.qpid.client.AMQConnectionURL;
+import org.apache.qpid.test.utils.QpidTestCase;
+
+public class OAuth2SaslClientTest extends QpidTestCase
+{
+    private static final String URL_WITH_TOKEN = 
"amqp://:token@client/test?brokerlist='tcp://localhost:1'";
+    private static final String URL_WITHOUT_TOKEN = 
"amqp://:@client/test?brokerlist='tcp://localhost:1'";
+    private OAuth2SaslClient _client;
+
+    public void testEvaluateChallenge_WithToken() throws Exception
+    {
+        OAuth2AccessTokenCallbackHandler handler = new 
OAuth2AccessTokenCallbackHandler();
+        handler.initialise(new AMQConnectionURL(URL_WITH_TOKEN));
+
+        _client = new OAuth2SaslClient(handler);
+         byte[] actualChallenge = _client.evaluateChallenge(new byte[] {});
+
+        byte[] expectedChallenge = "auth=Bearer token\1\1".getBytes();
+        Assert.assertArrayEquals(expectedChallenge, actualChallenge);
+    }
+
+    public void testEvaluateChallenge_NoToken() throws Exception
+    {
+        OAuth2AccessTokenCallbackHandler handler = new 
OAuth2AccessTokenCallbackHandler();
+        handler.initialise(new AMQConnectionURL(URL_WITHOUT_TOKEN));
+
+        _client = new OAuth2SaslClient(handler);
+        try
+        {
+            _client.evaluateChallenge(new byte[]{});
+            fail("Exception not thrown");
+        }
+        catch (SaslException se)
+        {
+            // PASS
+        }
+    }
+
+    public void testHasInitialResponse() throws Exception
+    {
+        OAuth2AccessTokenCallbackHandler handler = new 
OAuth2AccessTokenCallbackHandler();
+        handler.initialise(new AMQConnectionURL(URL_WITH_TOKEN));
+
+        _client = new OAuth2SaslClient(handler);
+        assertTrue(_client.hasInitialResponse());
+    }
+
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to