Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/mitre/cvw/Authenticator.java


1   /*
2    * Copyright (c) 1996-2000. The MITRE Corporation (http://www.mitre.org/).
3    * All rights reserved.
4    * CVW comes with ABSOLUTELY NO WARRANTY. See license for details.
5    */
6   
7   package org.mitre.cvw;
8   
9   import java.util.Hashtable;
10  import java.util.StringTokenizer;
11  import java.util.Vector;
12  
13  
14  /**
15   * This class processes all cvw-auth MCP commands that are sent to and from
16   * the CVW server; it is a stub for future expansion of different more stringent 
17   * authentication methods.  Currently, this client only supports "basic".
18   * @version 1.0
19   * @author Deb Ercolini
20   */
21  class Authenticator extends Object {
22     public CVWCoordinator applet;
23     String currentAuthMethod;
24     Hashtable availAuthMethods;    // for each cvw-auth-name sent
25     Vector prefAuthMethods;     // for each name sent via list in cvw-auth-pref
26     Vector allowedAuthMethods;  // for each name sent via allows in cvw-auth-pref
27     Vector clientAuthMethods;  // the authMethods allowed by client
28     static String authKey;
29     int maxNumRetries;
30     int currentTry;
31  
32     public static String getAuthKey() {
33    return authKey;
34      }
35  
36     Authenticator(String authMethodList, int max)
37      {
38        super();
39        applet = CVWCoordinator.getInstance();
40        int key =  (int) (Math.random() * 10000);
41        authKey = String.valueOf(key);
42        maxNumRetries = max;
43        currentTry = 0;
44        currentAuthMethod = new String("");
45        availAuthMethods = new Hashtable();
46        prefAuthMethods = new Vector();
47        allowedAuthMethods = new Vector();
48        clientAuthMethods = new Vector();
49        parseClientMethods(authMethodList);
50     }
51  
52  /**
53   * Clears the current authorization key.
54   */
55   public void clearKey() {
56     authKey = new String();
57    }
58  
59  // this should choose from the pref and allowed list starting with 
60  // the max important from the server that the client understands
61  /**
62   * Sends the authentication method of "basic".  Eventually should
63   * choose from a list of available methods to have client and server
64   * most compatible at an appropriate level.
65   * <br> MCP send cvw-auth-method
66   */
67    public void chooseAuthMethod() {
68      if (currentTry < maxNumRetries)  
69      { currentTry ++;
70        CVWServerComm.sendRawCmdToServer("#$#cvw-auth-method " + authKey + " name: basic");
71      }
72      else
73        //applet.authFailed("authentication: too many retries");
74    System.err.println("authentication failed");
75    } 
76  
77  /* 2/27/97 dage -- when user pushes logout btn, reset currentTry
78   */
79  /**
80   * Resets the current try.
81   */
82    public void resetCurrentTry() {
83      currentTry = 0;
84     }
85  
86  // this is so that if the server refuses, the current method chosen is
87  // removed and then the client can send the next best auth protocol
88  /**
89   * Removes the current authenication method when the CVW server refuses it, 
90   * and sends the next best protocol.
91   */
92    public void removeCurrentAuthMethod() {
93    
94      // need to remove current
95    } 
96  
97  /**
98   * Stores an authentication protocol sent from CVW server.
99   * @param name the name of the protocol
100  * @param from the starting version supported
101  * @param to the ending version supported
102  */
103   public void storeAuthName(String name, String from, String to) {
104     CVWAuthMethod authMeth = new CVWAuthMethod(name, from, to);
105     availAuthMethods.put(name, authMeth);
106    }
107 
108 /**
109  * Stores the preferred and allowed authentication methods.
110  * @param nameOrder the name of authentication methods in preferred order
111  * @param allows the list of authentication methods allowed
112  */
113   public void orderAuthName(String nameOrder, String allows) {
114   
115     StringTokenizer st = new StringTokenizer(allows, ",");
116     while (st.hasMoreTokens())
117         allowedAuthMethods.addElement(st.nextToken());
118  
119     //System.err.println("allowedAuthMethods: " +  allowedAuthMethods.toString());
120 
121     st  = new StringTokenizer(nameOrder, ",");
122     while (st.hasMoreTokens())
123         prefAuthMethods.addElement(st.nextToken());
124  
125     //System.err.println("prefAuthMethods: "  + prefAuthMethods.toString());
126 
127 
128   } 
129   
130 //rather than string needs to be vector of CVWAuthMethods 
131 //sent by CVWCoordinator
132 /**
133  * Parses a string of authentication methods that the client allows
134  * @param clientAllows comma delimited list of authentication methods
135  */
136   public void parseClientMethods(String clientAllows) {
137   
138     //System.err.println("in parse clientAuthMethods: " + clientAllows);
139 
140     StringTokenizer st = new StringTokenizer(clientAllows, ",");
141     while (st.hasMoreTokens())
142   clientAuthMethods.addElement(st.nextToken());
143 
144     //System.err.println("clientAuthMethods: "  + clientAuthMethods.toString());
145   } 
146 
147 // this is the method that gets called from the applet
148 /**
149  * Process the different cvw-auth MCP commands.
150  * <br> MCP receive cvw-auth
151  * @param cmd the actual command part of the MCP
152  * @param mcpCmd the whole MCP command
153  */
154   public void processCVWAuth(String cmd, MCPCommand mcpCmd) {
155         //System.err.println("in  process auth: "  + cmd);
156  
157         if (cmd.equals("cvw-auth"))
158           storeAuthName(mcpCmd.get("name"), 
159     mcpCmd.get("from"),
160                 mcpCmd.get("to"));
161  
162         if (cmd.equals("cvw-auth-pref"))
163           orderAuthName(mcpCmd.get("list"), mcpCmd.get("allows"));
164  
165         if (cmd.equals("cvw-auth-end"))
166           chooseAuthMethod();
167  
168         if (cmd.equals("cvw-auth-method")) {
169            //System.err.println("auth cmd matched " );
170            if (mcpCmd.get("pass").equals("1")) {
171               //System.err.println("auth accepted: " );
172               CVWServerComm.sendRawCmdToServer("#$#cvw-auth-method-end " + authKey );
173   // temp until user info is sent automatically by server
174         //applet.sendUserLookup();
175         applet.startProtocolExchange();
176   // soon will have to initiate the protocol trade
177       }
178            if (mcpCmd.get("pass").equals("0"))
179             { removeCurrentAuthMethod();
180               chooseAuthMethod();
181               System.err.println("not accepted: " + mcpCmd.get("message"));
182             }
183           }
184    }
185 
186 }
187