001: /* Copyright 2002 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.channels;
007:
008: import javax.servlet.http.HttpSession;
009:
010: import org.jasig.portal.ChannelCacheKey;
011: import org.jasig.portal.ChannelRuntimeData;
012: import org.jasig.portal.ChannelRuntimeProperties;
013: import org.jasig.portal.ChannelStaticData;
014: import org.jasig.portal.ICacheable;
015: import org.jasig.portal.IPrivilegedChannel;
016: import org.jasig.portal.PortalControlStructures;
017: import org.jasig.portal.PortalEvent;
018: import org.jasig.portal.PortalException;
019: import org.jasig.portal.i18n.LocaleManager;
020: import org.jasig.portal.security.ISecurityContext;
021: import org.jasig.portal.utils.DocumentFactory;
022: import org.jasig.portal.utils.ResourceLoader;
023: import org.jasig.portal.utils.XSLT;
024: import org.w3c.dom.Document;
025: import org.w3c.dom.Element;
026: import org.xml.sax.ContentHandler;
027:
028: /**
029: * <p>Allows a user to login to the portal. Login info is posted to
030: * <code>LoginServlet</code>. If user enters incorrect username and
031: * password, he/she is instructed to login again with a different
032: * password (the username of the previous attempt is preserved).</p>
033: * @author Ken Weiner, kweiner@unicon.net
034: * @version $Revision: 35144 $
035: */
036: public class CLogin implements IPrivilegedChannel, ICacheable {
037: private ChannelStaticData staticData;
038: private ChannelRuntimeData runtimeData;
039: private String attemptedUserName = "";
040: private static final String sslLocation = "CLogin/CLogin.ssl";
041: private boolean bAuthenticated = false;
042: private boolean bauthenticationAttemptFailed = false;
043: private boolean bSecurityError = false;
044: private String xslUriForKey = null;
045:
046: private static final String systemCacheId = "org.jasig.portal.CLogin:";
047:
048: private ISecurityContext ic;
049:
050: public CLogin() {
051: }
052:
053: public void setPortalControlStructures(PortalControlStructures pcs) {
054: HttpSession session = pcs.getHttpSession();
055: String authenticationAttempted = (String) session
056: .getAttribute("up_authenticationAttempted");
057: String authenticationError = (String) session
058: .getAttribute("up_authenticationError");
059: attemptedUserName = (String) session
060: .getAttribute("up_attemptedUserName");
061:
062: if (authenticationAttempted != null)
063: bauthenticationAttemptFailed = true;
064:
065: if (authenticationError != null)
066: bSecurityError = true;
067: }
068:
069: public ChannelRuntimeProperties getRuntimeProperties() {
070: return new ChannelRuntimeProperties();
071: }
072:
073: public void receiveEvent(PortalEvent ev) {
074: }
075:
076: public void setStaticData(ChannelStaticData sd) {
077: this .staticData = sd;
078: ic = staticData.getPerson().getSecurityContext();
079:
080: if (ic != null && ic.isAuthenticated())
081: bAuthenticated = true;
082: }
083:
084: public void setRuntimeData(ChannelRuntimeData rd) {
085: this .runtimeData = rd;
086: }
087:
088: public void renderXML(ContentHandler out) throws PortalException {
089: String fullName = (String) staticData.getPerson().getFullName();
090: Document doc = DocumentFactory.getNewDocument();
091:
092: // Create <login-status> element
093: Element loginStatusElement = doc.createElement("login-status");
094:
095: if (bSecurityError) {
096: // Create <error> element under <login-status>
097: Element errorElement = doc.createElement("error");
098: loginStatusElement.appendChild(errorElement);
099: } else if (bauthenticationAttemptFailed && !bAuthenticated) {
100: // Create <failure> element under <login-status>
101: Element failureElement = doc.createElement("failure");
102: failureElement.setAttribute("attemptedUserName",
103: attemptedUserName);
104: loginStatusElement.appendChild(failureElement);
105: } else if (fullName != null) {
106: // Create <full-name> element under <header>
107: Element fullNameElement = doc.createElement("full-name");
108: fullNameElement.appendChild(doc.createTextNode(fullName));
109: loginStatusElement.appendChild(fullNameElement);
110: }
111:
112: doc.appendChild(loginStatusElement);
113:
114: XSLT xslt = XSLT.getTransformer(this , runtimeData.getLocales());
115: xslt.setXML(doc);
116: xslt.setXSL(sslLocation, runtimeData.getBrowserInfo());
117: xslt.setTarget(out);
118: xslt.setStylesheetParameter("baseActionURL", runtimeData
119: .getBaseActionURL());
120: xslt.setStylesheetParameter("unauthenticated", String
121: .valueOf(!staticData.getPerson().getSecurityContext()
122: .isAuthenticated()));
123: xslt.transform();
124: }
125:
126: public ChannelCacheKey generateKey() {
127: ChannelCacheKey k = new ChannelCacheKey();
128: StringBuffer sbKey = new StringBuffer(1024);
129: // guest pages are cached system-wide
130: if (staticData.getPerson().isGuest()) {
131: k.setKeyScope(ChannelCacheKey.SYSTEM_KEY_SCOPE);
132: sbKey.append(systemCacheId);
133: } else {
134: k.setKeyScope(ChannelCacheKey.INSTANCE_KEY_SCOPE);
135: }
136: sbKey.append("userId:").append(staticData.getPerson().getID())
137: .append(", ");
138: sbKey.append("authenticated:").append(
139: staticData.getPerson().getSecurityContext()
140: .isAuthenticated()).append(", ");
141:
142: if (xslUriForKey == null) {
143: try {
144: String sslUri = ResourceLoader.getResourceAsURLString(
145: this .getClass(), sslLocation);
146: xslUriForKey = XSLT.getStylesheetURI(sslUri,
147: runtimeData.getBrowserInfo());
148: } catch (PortalException pe) {
149: xslUriForKey = "Not attainable!";
150: }
151: }
152: sbKey.append("xslUri:").append(xslUriForKey).append(", ");
153: sbKey.append("bAuthenticated:").append(bAuthenticated).append(
154: ", ");
155: sbKey.append("bauthenticationAttemptFailed:").append(
156: bauthenticationAttemptFailed).append(", ");
157: sbKey.append("attemptedUserName:").append(attemptedUserName)
158: .append(", ");
159: sbKey.append("bSecurityError:").append(bSecurityError).append(
160: ", ");
161: sbKey.append("locales:").append(
162: LocaleManager.stringValueOf(runtimeData.getLocales()));
163: k.setKey(sbKey.toString());
164: k.setKeyValidity(new Long(System.currentTimeMillis()));
165: return k;
166: }
167:
168: public boolean isCacheValid(Object validity) {
169: return true;
170: }
171:
172: public String toString() {
173: StringBuffer sb = new StringBuffer();
174: sb.append(getClass().getName());
175: sb.append(" authenticated:");
176: sb.append(this .bAuthenticated);
177: sb.append(" authenticationAttemptFailed:");
178: sb.append(this .bauthenticationAttemptFailed);
179: sb.append(" securityError:");
180: sb.append(this .bSecurityError);
181: sb.append(" attemptedUserName=[");
182: sb.append(this .attemptedUserName);
183: sb.append("]");
184: return sb.toString();
185: }
186: }
|