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.example.notifications;
8   
9   import org.mule.api.MuleEvent;
10  import org.mule.api.lifecycle.InitialisationException;
11  import org.mule.api.security.CryptoFailureException;
12  import org.mule.api.security.EncryptionStrategyNotFoundException;
13  import org.mule.api.security.SecurityException;
14  import org.mule.api.security.SecurityProviderNotFoundException;
15  import org.mule.api.security.UnauthorisedException;
16  import org.mule.api.security.UnknownAuthenticationTypeException;
17  import org.mule.config.i18n.CoreMessages;
18  import org.mule.security.AbstractAuthenticationFilter;
19  import org.mule.transformer.types.DataTypeFactory;
20  
21  import java.util.Map;
22  
23  /**
24   * TODO
25   */
26  public class DummySecurityFilter extends AbstractAuthenticationFilter
27  {
28      @Override
29      protected void authenticateInbound(MuleEvent event) throws org.mule.api.security.SecurityException, CryptoFailureException, SecurityProviderNotFoundException, EncryptionStrategyNotFoundException, UnknownAuthenticationTypeException
30      {
31          doAuthenticate(event);
32      }
33  
34      @Override
35      protected void authenticateOutbound(MuleEvent event) throws SecurityException, SecurityProviderNotFoundException, CryptoFailureException
36      {
37          doAuthenticate(event);
38      }
39  
40      protected void doAuthenticate(MuleEvent event) throws UnauthorisedException
41      {
42          try
43          {
44              Map<?, ?> payload = event.getMessage().getPayload(DataTypeFactory.create(Map.class));
45              String user = (String) payload.get("user");
46              if (user == null)
47              {
48                  throw new UnauthorisedException(CoreMessages.authNoCredentials());
49              }
50              if ("anonymous".equals(user))
51              {
52                  throw new UnauthorisedException(CoreMessages.authFailedForUser("anonymous"));
53              }
54          }
55          catch (Exception e)
56          {
57              throw new UnauthorisedException(CoreMessages.authFailedForUser("anonymous"), e);
58          }
59  
60      }
61  
62      @Override
63      protected void doInitialise() throws InitialisationException
64      {
65          // nothing to do
66      }
67  }