001: /*
002: * Copyright 1999-2001,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.catalina.authenticator;
017:
018: import java.security.Principal;
019:
020: import org.apache.catalina.Session;
021: import org.apache.catalina.authenticator.Constants;
022:
023: /**
024: * A class that represents entries in the cache of authenticated users.
025: * This is necessary to make it available to
026: * <code>AuthenticatorBase</code> subclasses that need it in order to perform
027: * reauthentications when SingleSignOn is in use.
028: *
029: * @author B Stansberry, based on work by Craig R. McClanahan
030: * @version $Revision: 1.5 $
031: *
032: * @see SingleSignOn
033: * @see AuthenticatorBase#reauthenticateFromSSO
034: */
035: public class SingleSignOnEntry {
036: // ------------------------------------------------------ Instance Fields
037:
038: protected String authType = null;
039:
040: protected String password = null;
041:
042: protected Principal principal = null;
043:
044: protected Session sessions[] = new Session[0];
045:
046: protected String username = null;
047:
048: protected boolean canReauthenticate = false;
049:
050: // --------------------------------------------------------- Constructors
051:
052: /**
053: * Creates a new SingleSignOnEntry
054: *
055: * @param principal the <code>Principal</code> returned by the latest
056: * call to <code>Realm.authenticate</code>.
057: * @param authType the type of authenticator used (BASIC, CLIENT-CERT,
058: * DIGEST or FORM)
059: * @param username the username (if any) used for the authentication
060: * @param password the password (if any) used for the authentication
061: */
062: public SingleSignOnEntry(Principal principal, String authType,
063: String username, String password) {
064: super ();
065: updateCredentials(principal, authType, username, password);
066: }
067:
068: public SingleSignOnEntry() {
069: }
070:
071: // ------------------------------------------------------- Package Methods
072:
073: /**
074: * Adds a <code>Session</code> to the list of those associated with
075: * this SSO.
076: *
077: * @param sso The <code>SingleSignOn</code> valve that is managing
078: * the SSO session.
079: * @param session The <code>Session</code> being associated with the SSO.
080: */
081: public synchronized void addSession(SingleSignOn sso,
082: Session session) {
083: for (int i = 0; i < sessions.length; i++) {
084: if (session == sessions[i])
085: return;
086: }
087: Session results[] = new Session[sessions.length + 1];
088: System.arraycopy(sessions, 0, results, 0, sessions.length);
089: results[sessions.length] = session;
090: sessions = results;
091: session.addSessionListener(sso);
092: }
093:
094: /**
095: * Removes the given <code>Session</code> from the list of those
096: * associated with this SSO.
097: *
098: * @param session the <code>Session</code> to remove.
099: */
100: public synchronized void removeSession(Session session) {
101: Session[] nsessions = new Session[sessions.length - 1];
102: for (int i = 0, j = 0; i < sessions.length; i++) {
103: if (session == sessions[i])
104: continue;
105: nsessions[j++] = sessions[i];
106: }
107: sessions = nsessions;
108: }
109:
110: /**
111: * Returns the <code>Session</code>s associated with this SSO.
112: */
113: public synchronized Session[] findSessions() {
114: return (this .sessions);
115: }
116:
117: /**
118: * Gets the name of the authentication type originally used to authenticate
119: * the user associated with the SSO.
120: *
121: * @return "BASIC", "CLIENT-CERT", "DIGEST", "FORM" or "NONE"
122: */
123: public String getAuthType() {
124: return (this .authType);
125: }
126:
127: /**
128: * Gets whether the authentication type associated with the original
129: * authentication supports reauthentication.
130: *
131: * @return <code>true</code> if <code>getAuthType</code> returns
132: * "BASIC" or "FORM", <code>false</code> otherwise.
133: */
134: public boolean getCanReauthenticate() {
135: return (this .canReauthenticate);
136: }
137:
138: /**
139: * Gets the password credential (if any) associated with the SSO.
140: *
141: * @return the password credential associated with the SSO, or
142: * <code>null</code> if the original authentication type
143: * does not involve a password.
144: */
145: public String getPassword() {
146: return (this .password);
147: }
148:
149: /**
150: * Gets the <code>Principal</code> that has been authenticated by
151: * the SSO.
152: */
153: public Principal getPrincipal() {
154: return (this .principal);
155: }
156:
157: /**
158: * Gets the username provided by the user as part of the authentication
159: * process.
160: */
161: public String getUsername() {
162: return (this .username);
163: }
164:
165: /**
166: * Updates the SingleSignOnEntry to reflect the latest security
167: * information associated with the caller.
168: *
169: * @param principal the <code>Principal</code> returned by the latest
170: * call to <code>Realm.authenticate</code>.
171: * @param authType the type of authenticator used (BASIC, CLIENT-CERT,
172: * DIGEST or FORM)
173: * @param username the username (if any) used for the authentication
174: * @param password the password (if any) used for the authentication
175: */
176: public void updateCredentials(Principal principal, String authType,
177: String username, String password) {
178:
179: this.principal = principal;
180: this.authType = authType;
181: this.username = username;
182: this.password = password;
183: this.canReauthenticate = (Constants.BASIC_METHOD
184: .equals(authType) || Constants.FORM_METHOD
185: .equals(authType));
186: }
187:
188: }
|