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-11-03 09:38:58 -0800 (Fri, 03 Nov 2000) $'
11
 * '$Revision: 510 $'
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 = "Authentication successful for user: " + username;
62
        this.statusMessage = formatOutput("login", message);
63
        return true;
64
      } else {  
65
        message = "Authentication failed for user: " + username;
66
        this.statusMessage = formatOutput("unauth_login", message);
67
        return false;
68
      }    
69
    } catch ( ConnectException ce ) {
70
      message = "Connection to the authentication service failed. " 
71
                       + ce.getMessage();
72
    } catch ( IllegalStateException ise ) {
73
      message = ise.getMessage();
74
    }
75
 
76
    this.statusMessage = formatOutput("error_login", message);
77
    return false;
78
  }
79

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

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

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

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

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

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

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