View Javadoc

1   /*
2    * $Id: MuleCredentials.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.security;
12  
13  import org.mule.api.EncryptionStrategy;
14  import org.mule.api.config.MuleProperties;
15  import org.mule.api.security.Credentials;
16  import org.mule.api.security.CryptoFailureException;
17  import org.mule.api.security.EncryptionStrategyNotFoundException;
18  import org.mule.api.security.SecurityManager;
19  import org.mule.config.i18n.CoreMessages;
20  import org.mule.util.ArrayUtils;
21  
22  import java.io.Serializable;
23  import java.util.StringTokenizer;
24  
25  /**
26   * <code>MuleCredentials</code> can be used to read and set Mule user information
27   * that can be stored in a message header.
28   */
29  
30  public class MuleCredentials implements Credentials, Serializable
31  {
32      public static final String TOKEN_DELIM = "::";
33  
34      private final String username;
35      private final char[] password;
36      private Object roles;
37  
38      public MuleCredentials(String username, char[] password)
39      {
40          this.username = username;
41          this.password = ArrayUtils.clone(password);
42      }
43  
44      public MuleCredentials(String username, char[] password, Object roles)
45      {
46          this.username = username;
47          this.password = ArrayUtils.clone(password);
48          this.roles = roles;
49      }
50  
51      public MuleCredentials(String header, SecurityManager sm) throws EncryptionStrategyNotFoundException, CryptoFailureException
52      {
53  
54          int i = header.indexOf(' ');
55          if (i == -1)
56          {
57              throw new IllegalArgumentException(
58                  CoreMessages.headerMalformedValueIs(MuleProperties.MULE_USER_PROPERTY, header).toString());
59          }
60  
61          String scheme = header.substring(0, i);
62          String creds = header.substring(i + 1);
63  
64          if (!scheme.equalsIgnoreCase("plain"))
65          {
66              EncryptionStrategy es = sm.getEncryptionStrategy(scheme);
67              if (es == null)
68              {
69                  throw new EncryptionStrategyNotFoundException(scheme);
70              }
71              else
72              {
73                  creds = new String(es.decrypt(creds.getBytes(), null));
74              }
75          }
76  
77          StringTokenizer st = new StringTokenizer(creds, TOKEN_DELIM);
78          username = st.nextToken();
79          password = st.nextToken().toCharArray();
80          if (st.hasMoreTokens())
81          {
82              roles = st.nextToken();
83          }
84      }
85  
86      public String getToken()
87      {
88          StringBuffer buf = new StringBuffer();
89          buf.append(username).append(TOKEN_DELIM);
90          buf.append(password).append(TOKEN_DELIM);
91  
92          if (roles != null)
93          {
94              buf.append(roles);
95          }
96  
97          return buf.toString();
98      }
99  
100     public String getUsername()
101     {
102         return username;
103     }
104 
105     public char[] getPassword()
106     {
107         return ArrayUtils.clone(password);
108     }
109 
110     public Object getRoles()
111     {
112         return roles;
113     }
114 
115     public static String createHeader(String username, char[] password)
116     {
117         StringBuffer buf = new StringBuffer(32);
118         buf.append("Plain ");
119         buf.append(username).append(TOKEN_DELIM);
120         buf.append(password).append(TOKEN_DELIM);
121         return buf.toString();
122     }
123 
124     public static String createHeader(String username,
125                                       String password,
126                                       String encryptionName,
127                                       EncryptionStrategy es) throws CryptoFailureException
128     {
129         StringBuffer buf = new StringBuffer();
130         buf.append(encryptionName).append(" ");
131         String creds = username + TOKEN_DELIM + password;
132         byte[] encrypted = es.encrypt(creds.getBytes(), null);
133         buf.append(new String(encrypted));
134         return buf.toString();
135     }
136 }