KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > impl > security > MuleCredentials


1 /*
2  * $Id: MuleCredentials.java 4259 2006-12-14 03:12:07Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.impl.security;
12
13 import java.util.StringTokenizer JavaDoc;
14
15 import org.mule.MuleManager;
16 import org.mule.config.MuleProperties;
17 import org.mule.config.i18n.Message;
18 import org.mule.config.i18n.Messages;
19 import org.mule.umo.UMOEncryptionStrategy;
20 import org.mule.umo.security.CryptoFailureException;
21 import org.mule.umo.security.EncryptionStrategyNotFoundException;
22 import org.mule.umo.security.UMOCredentials;
23 import org.mule.umo.security.UMOSecurityManager;
24 import org.mule.util.ArrayUtils;
25
26 /**
27  * <code>MuleCredentials</code> can be used to read and set Mule user information
28  * that can be stored in a message header.
29  */

30
31 public class MuleCredentials implements UMOCredentials
32 {
33     public static final String JavaDoc TOKEN_DELIM = "::";
34
35     private final String JavaDoc username;
36     private final char[] password;
37     private Object JavaDoc roles;
38
39     public MuleCredentials(String JavaDoc username, char[] password)
40     {
41         this.username = username;
42         this.password = ArrayUtils.clone(password);
43     }
44
45     public MuleCredentials(String JavaDoc username, char[] password, Object JavaDoc roles)
46     {
47         this.username = username;
48         this.password = ArrayUtils.clone(password);
49         this.roles = roles;
50     }
51
52     public MuleCredentials(String JavaDoc header) throws EncryptionStrategyNotFoundException, CryptoFailureException
53     {
54         String JavaDoc scheme = null;
55
56         int i = header.indexOf(' ');
57         if (i == -1)
58         {
59             throw new IllegalArgumentException JavaDoc(new Message(Messages.HEADER_X_MALFORMED_VALUE_IS_X,
60                 MuleProperties.MULE_USER_PROPERTY, header).toString());
61         }
62         else
63         {
64             scheme = header.substring(0, i);
65         }
66
67         String JavaDoc creds = header.substring(i + 1);
68
69         if (!scheme.equalsIgnoreCase("plain"))
70         {
71             UMOSecurityManager sm = MuleManager.getInstance().getSecurityManager();
72
73             UMOEncryptionStrategy es = sm.getEncryptionStrategy(scheme);
74             if (es == null)
75             {
76                 throw new EncryptionStrategyNotFoundException(scheme);
77             }
78             else
79             {
80                 creds = new String JavaDoc(es.decrypt(creds.getBytes(), null));
81             }
82         }
83
84         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(creds, TOKEN_DELIM);
85         username = st.nextToken();
86         password = st.nextToken().toCharArray();
87         if (st.hasMoreTokens())
88         {
89             roles = st.nextToken();
90         }
91     }
92
93     public String JavaDoc getToken()
94     {
95         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
96         buf.append(username).append(TOKEN_DELIM);
97         buf.append(password).append(TOKEN_DELIM);
98
99         if (roles != null)
100         {
101             buf.append(roles);
102         }
103
104         return buf.toString();
105     }
106
107     public String JavaDoc getUsername()
108     {
109         return username;
110     }
111
112     public char[] getPassword()
113     {
114         return ArrayUtils.clone(password);
115     }
116
117     public Object JavaDoc getRoles()
118     {
119         return roles;
120     }
121
122     public static String JavaDoc createHeader(String JavaDoc username, char[] password)
123     {
124         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(32);
125         buf.append("Plain ");
126         buf.append(username).append(TOKEN_DELIM);
127         buf.append(password).append(TOKEN_DELIM);
128         return buf.toString();
129     }
130
131     public static String JavaDoc createHeader(String JavaDoc username,
132                                       String JavaDoc password,
133                                       String JavaDoc encryptionName,
134                                       UMOEncryptionStrategy es) throws CryptoFailureException
135     {
136         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
137         buf.append(encryptionName).append(" ");
138         String JavaDoc creds = username + TOKEN_DELIM + password;
139         byte[] encrypted = es.encrypt(creds.getBytes(), null);
140         buf.append(new String JavaDoc(encrypted));
141         return buf.toString();
142     }
143 }
144
Popular Tags