ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/filter/KeyManager.java
Revision: 1.1
Committed: Thu Mar 21 17:44:51 2002 UTC (22 years, 2 months ago) by tdb
Branch: MAIN
Log Message:
Initial work on host authentication for the server. Until I can get ihost
doing it's side of the host authentication I can't really test any further.
It seems to work, as in it filters data which isn't authenticated when told
to do so in the config :)

File Contents

# Content
1 //---PACKAGE DECLARATION---
2 package uk.org.iscream.cms.server.filter;
3
4 //---IMPORTS---
5 import uk.org.iscream.cms.server.util.*;
6 import uk.org.iscream.cms.server.core.*;
7 import uk.org.iscream.cms.server.componentmanager.*;
8 import java.util.Random;
9 import java.util.HashMap;
10
11 /**
12 * Acts as a store and checking mechanism for host keys.
13 *
14 * @author $Author$
15 * @version $Id$
16 */
17 public class KeyManager {
18
19 //---FINAL ATTRIBUTES---
20
21 /**
22 * The current CVS revision of this class
23 */
24 public final String REVISION = "$Revision: 1.1 $";
25
26 /**
27 * The set of characters to be used for our keys
28 */
29 private final char[] KEYSET = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
30
31 /**
32 * The length of our keys
33 */
34 public static final int KEYLEN = 15;
35
36 //---STATIC METHODS---
37
38 /**
39 * Return a reference to the single class.
40 * Construct it if it does not already exist, otherwise just return the reference.
41 */
42 public synchronized static KeyManager getInstance() {
43 if (_instance == null){
44 _instance = new KeyManager();
45 }
46 return _instance;
47 }
48
49 //---CONSTRUCTORS---
50
51 /**
52 * Private constructor to avoid external construction.
53 */
54 private KeyManager() {
55 // do nothing
56 }
57
58 //---PUBLIC METHODS---
59
60 /**
61 * Add or replace a key.
62 *
63 * @param host the hostname to add a key for
64 * @param key the key to add
65 */
66 public synchronized void addKey(String host, String key) {
67 _keys.put(host, key);
68 }
69
70 /**
71 * Check if a given key is currently valid.
72 * Will respond negatively if the key doesn't match,
73 * or if no key is found (and EnforceHostAuth is on).
74 *
75 * @param host the hostname to check the key for
76 * @param key the key to check
77 * @return whether the check was successful
78 */
79 public synchronized boolean checkKey(String host, String key) {
80 Object o = _keys.get(host);
81 if(o != null) {
82 // if our key isn't null, we want to make
83 // sure it matches that given
84 String ourKey = (String) o;
85 return ourKey.equals(key);
86 }
87 else {
88 // default to enforcing host auth
89 boolean enforceHostAuth = true;
90 try {
91 // try to get see what's in the config
92 String enforce = ConfigurationProxy.getInstance().getProperty("Filter." + FilterMain.NAME, "Filter.EnforceHostAuth");
93 enforceHostAuth = (Integer.parseInt(enforce) == 1);
94 }
95 catch(PropertyNotFoundException e) {
96 // if it's not set, not enforced
97 _logger.write(toString(), Logger.WARNING, "EnforceHostAuth property not found: " + e);
98 enforceHostAuth = false;
99 }
100 catch(NumberFormatException e) {
101 // if it's not a number, not enforced
102 _logger.write(toString(), Logger.WARNING, "EnforceHostAuth property malformed: " + e);
103 enforceHostAuth = false;
104 }
105 // if enforceHostAuth is true we want to fail the check
106 // and vice-versa
107 return !enforceHostAuth;
108 }
109 }
110
111 /**
112 * Generate a random key with length KEYLEN.
113 *
114 * @return a random key as a String
115 */
116 public String genKey() {
117 Random r = new Random();
118 StringBuffer s = new StringBuffer();
119 for(int i=0; i < KEYLEN; i++) {
120 s.append(KEYSET[r.nextInt(KEYSET.length)]);
121 }
122 return s.toString();
123 }
124
125 /**
126 * Overrides the {@link java.lang.Object#toString() Object.toString()}
127 * method to provide clean logging (every class should have this).
128 *
129 * This uses the uk.org.iscream.cms.server.util.NameFormat class
130 * to format the toString()
131 *
132 * @return the name of this class and its CVS revision
133 */
134 public String toString() {
135 return FormatName.getName(
136 _name,
137 getClass().getName(),
138 REVISION);
139 }
140
141 //---PRIVATE METHODS---
142
143 //---ACCESSOR/MUTATOR METHODS---
144
145 //---ATTRIBUTES---
146
147 /**
148 * This holds the current keys for
149 * the hosts we're authenticating
150 */
151 private HashMap _keys = new HashMap();
152
153 /**
154 * This holds a reference to the
155 * system logger that is being used.
156 */
157 private Logger _logger = ReferenceManager.getInstance().getLogger();
158
159 /**
160 * This is the friendly identifier of the
161 * component this class is running in.
162 * eg, a Filter may be called "filter1",
163 * If this class does not have an owning
164 * component, a name from the configuration
165 * can be placed here. This name could also
166 * be changed to null for utility classes.
167 */
168 private String _name = FilterMain.NAME;
169
170 //---STATIC ATTRIBUTES---
171
172 /**
173 * A reference to the single instance of this class
174 */
175 private static KeyManager _instance;
176
177 }