View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.jaas;
8   
9   import org.mule.api.EncryptionStrategy;
10  import org.mule.api.ExceptionPayload;
11  import org.mule.api.MuleMessage;
12  import org.mule.api.config.MuleProperties;
13  import org.mule.module.client.MuleClient;
14  import org.mule.security.MuleCredentials;
15  import org.mule.tck.junit4.FunctionalTestCase;
16  
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertTrue;
26  
27  public class JaasAutenticationWithJaasConfigFileTestCase extends FunctionalTestCase
28  {
29  
30      @Override
31      protected String getConfigResources()
32      {
33          return "mule-conf-for-jaas-conf-file.xml";
34      }
35  
36      @Test
37      public void testCaseGoodAuthentication() throws Exception
38      {
39          MuleClient client = new MuleClient(muleContext);
40  
41          Map props = new HashMap();
42          EncryptionStrategy strategy = muleContext
43              .getSecurityManager()
44              .getEncryptionStrategy("PBE");
45          String header = MuleCredentials.createHeader("Marie.Rizzo", "dragon", "PBE", strategy);
46          props.put(MuleProperties.MULE_USER_PROPERTY, header);
47          MuleMessage m = client.send("vm://test", "Test", props);
48  
49          assertNotNull(m);
50          assertTrue(m.getPayload() instanceof String);
51          assertEquals("Test Received", m.getPayloadAsString());
52      }
53  
54      @Test
55      public void testCaseDifferentGoodAuthentication() throws Exception
56      {
57          MuleClient client = new MuleClient(muleContext);
58  
59          Map props = new HashMap();
60          EncryptionStrategy strategy = muleContext
61              .getSecurityManager()
62              .getEncryptionStrategy("PBE");
63          String header = MuleCredentials.createHeader("anon", "anon", "PBE", strategy);
64          props.put(MuleProperties.MULE_USER_PROPERTY, header);
65          MuleMessage m = client.send("vm://test", "Test", props);
66  
67          assertNotNull(m);
68          assertTrue(m.getPayload() instanceof String);
69          assertEquals("Test Received", m.getPayloadAsString());
70      }
71  
72      @Test
73      public void testCaseWrongCombinationOfCorrectUsernameAndPassword() throws Exception
74      {
75          MuleClient client = new MuleClient(muleContext);
76  
77          Map props = new HashMap();
78          EncryptionStrategy strategy = muleContext
79              .getSecurityManager()
80              .getEncryptionStrategy("PBE");
81          String header = MuleCredentials.createHeader("Marie.Rizzo", "anon", "PBE", strategy);
82          props.put(MuleProperties.MULE_USER_PROPERTY, header);
83          MuleMessage m = client.send("vm://test", "Test", props);
84  
85          assertNotNull(m);
86          assertTrue(m.getPayload() instanceof String);
87          assertFalse(m.getPayloadAsString().equals("Test Received"));
88  
89          //assert exception
90          ExceptionPayload exceptionPayload = m.getExceptionPayload();
91          assertNotNull(exceptionPayload);
92          assertEquals("Authentication failed for principal Marie.Rizzo. Message payload is of type: String", exceptionPayload.getMessage());
93      }
94  
95      @Test
96      public void testCaseBadUserName() throws Exception
97      {
98          MuleClient client = new MuleClient(muleContext);
99          Map props = new HashMap();
100         EncryptionStrategy strategy = muleContext
101             .getSecurityManager()
102             .getEncryptionStrategy("PBE");
103         String header = MuleCredentials.createHeader("Evil", "dragon", "PBE", strategy);
104         props.put(MuleProperties.MULE_USER_PROPERTY, header);
105         MuleMessage m = client.send("vm://test", "Test", props);
106 
107         assertNotNull(m);
108         assertTrue(m.getPayload() instanceof String);
109         assertFalse(m.getPayloadAsString().equals("Test Received"));
110 
111         //assert exception
112         ExceptionPayload exceptionPayload = m.getExceptionPayload();
113         assertNotNull(exceptionPayload);
114         assertEquals("Authentication failed for principal Evil. Message payload is of type: String", exceptionPayload.getMessage());
115 
116     }
117 
118     @Test
119     public void testCaseBadPassword() throws Exception
120     {
121         MuleClient client = new MuleClient(muleContext);
122         Map props = new HashMap();
123         EncryptionStrategy strategy = muleContext
124             .getSecurityManager()
125             .getEncryptionStrategy("PBE");
126         String header = MuleCredentials.createHeader("Marie.Rizzo", "evil", "PBE", strategy);
127         props.put(MuleProperties.MULE_USER_PROPERTY, header);
128         MuleMessage m = client.send("vm://test", "Test", props);
129 
130         assertNotNull(m);
131         assertTrue(m.getPayload() instanceof String);
132         assertFalse(m.getPayloadAsString().equals("Test Received"));
133 
134         //assert exception
135         ExceptionPayload exceptionPayload = m.getExceptionPayload();
136         assertNotNull(exceptionPayload);
137         assertEquals("Authentication failed for principal Marie.Rizzo. Message payload is of type: String", exceptionPayload.getMessage());
138     }
139 
140 }