Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that tracks sessions for MetaCatServlet users.
4
 *  Copyright: 2000 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Matt Jones
7
 *    Release: @release@
8
 *
9
 *   '$Author: bojilova $'
10
 *     '$Date: 2000-10-31 15:26:43 -0800 (Tue, 31 Oct 2000) $'
11
 * '$Revision: 509 $'
12
 */
13

    
14
package edu.ucsb.nceas.metacat;
15

    
16
import java.net.ConnectException;
17
import javax.servlet.http.HttpSession;
18
import javax.servlet.http.HttpServletRequest;
19

    
20
/**
21
 * A Class that implements session tracking for MetaCatServlet users.
22
 * User's login data are stored in the session object.
23
 * User authentication is done through a dynamically determined AuthInterface.
24
 */
25
public class AuthSession {
26

    
27
  private String authClass = null;
28
  private HttpSession session = null;
29
  private AuthInterface authService = null;
30
  private String statusMessage = null;
31
 
32
  /** 
33
   * Construct an AuthSession
34
   */
35
  public AuthSession() throws Exception {
36

    
37
    // Determine our session authentication method and
38
    // create an instance of the auth class
39
    MetaCatUtil util = new MetaCatUtil();
40
    authClass = util.getOption("authclass");
41
    authService = (AuthInterface)createObject(authClass);
42
    
43
  }
44

    
45
  /** 
46
   * determine if the credentials for this session are valid by 
47
   * authenticating them using the authService configured for this session.
48
   *
49
   * @param request the request made from the client
50
   * @param username the username entered when login
51
   * @param password the password entered when login
52
   */
53
  public boolean authenticate(HttpServletRequest request, 
54
                        String username, String password)  {
55
                          
56
    String message = null;
57
 
58
    try { 
59
      if ( authService.authenticate(username, password) ) {
60
        this.session = getSession(request, username, password);
61
        message = "User Authentication successful";
62
        this.statusMessage = formatOutput("success", message);
63
        return true;
64
      } else {  
65
        message = "Authentication failed for user: " + username;
66
      }    
67
    } catch ( ConnectException ce ) {
68
      message = "Connection to the authentication service failed. " 
69
                       + ce.getMessage();
70
    } catch ( IllegalStateException ise ) {
71
      message = ise.getMessage();
72
    }
73
 
74
    this.statusMessage = formatOutput("error", message);
75
    return false;
76
  }
77

    
78
  /** Get new HttpSession and store username & password in it */
79
  private HttpSession getSession(HttpServletRequest request, 
80
                            String username, String password)  
81
                                throws IllegalStateException {
82

    
83
    // get the current session object, create one if necessary
84
    HttpSession session = request.getSession(true);
85

    
86
    // if it is still in use invalidate and get a new one
87
    if ( !session.isNew() ) {
88
      session.invalidate();
89
      session = request.getSession(true);
90
    }
91
    // store username & password in the session for later use, especially by
92
    // the authenticate() method
93
    session.setMaxInactiveInterval(-1);
94
    session.setAttribute("username", username);
95
    session.setAttribute("password", password);
96
    
97
    return session;
98
  }
99

    
100
  /**
101
   * Get the message associated with authenticating this session. The
102
   * message is formatted in XML.
103
   */
104
  public String getMessage() 
105
  {
106
    return this.statusMessage;
107
  }
108

    
109
/* NOT NEEDED
110
  /**
111
   * Determine if the session has been successfully authenticated
112
   * @returns boolean true if authentication was successful, false otherwise
113
   */
114
/*
115
  public boolean isAuthenticated() 
116
  {
117
    return this.isAuthenticated;
118
  }
119
*/
120

    
121
/* NOT NEEDED
122
  /**
123
   * Invalidate this HTTPSession object. 
124
   * All objects stored in the session are unbound.
125
   */
126
/*
127
  private void invalidate(String message)
128
  {
129
    this.isAuthenticated = false;
130
    this.session.setAttribute("isAuthenticated", new Boolean(isAuthenticated));
131
    this.statusMessage = formatOutput("error", message);
132
    this.session.setAttribute("statusMessage", this.statusMessage);
133
    this.session.invalidate();
134
  }    
135
*/
136
  /* 
137
   * format the output in xml for processing from client applications
138
   *
139
   * @param tag the root element tag for the message (error or success)
140
   * @param message the message content of the root element
141
   */
142
  private String formatOutput(String tag, String message) {
143
    
144
    StringBuffer out = new StringBuffer();
145
      
146
    out.append("<?xml version=\"1.0\"?>\n");
147
    out.append("<" + tag + ">");
148
    if ( tag.equals("error") ) {
149
      out.append(message);
150
    } else {
151
      out.append("\n  <message>" + message + "</message>\n");
152
      String username = (String)this.session.getAttribute("username");
153
      out.append("  <username>" + username + "</username>\n");
154
    }  
155
    out.append("</" + tag + ">");
156
    
157
    return out.toString();
158
  }
159

    
160
  /**
161
   * Instantiate a class using the name of the class at runtime
162
   *
163
   * @param className the fully qualified name of the class to instantiate
164
   */
165
  private static Object createObject(String className) throws Exception {
166
 
167
    Object object = null;
168
    try {
169
      Class classDefinition = Class.forName(className);
170
      object = classDefinition.newInstance();
171
    } catch (InstantiationException e) {
172
      throw e;
173
    } catch (IllegalAccessException e) {
174
      throw e;
175
    } catch (ClassNotFoundException e) {
176
      throw e;
177
    }
178
    return object;
179
  }
180
}
(7-7/33)