001: // ========================================================================
002: // Copyright 2003-2005 Mort Bay Consulting Pty. Ltd.
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: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: // ========================================================================
014:
015: package org.mortbay.jetty.security;
016:
017: import java.security.Principal;
018: import java.security.SecureRandom;
019: import java.util.HashMap;
020: import java.util.Random;
021:
022: import javax.servlet.http.Cookie;
023:
024: import org.mortbay.jetty.Request;
025: import org.mortbay.jetty.Response;
026: import org.mortbay.jetty.webapp.WebAppContext;
027: import org.mortbay.log.Log;
028:
029: public class HashSSORealm implements SSORealm {
030:
031: /* ------------------------------------------------------------ */
032: public static final String SSO_COOKIE_NAME = "SSO_ID";
033: private HashMap _ssoId2Principal = new HashMap();
034: private HashMap _ssoUsername2Id = new HashMap();
035: private HashMap _ssoPrincipal2Credential = new HashMap();
036: private transient Random _random = new SecureRandom();
037:
038: /* ------------------------------------------------------------ */
039: public Credential getSingleSignOn(Request request, Response response) {
040: String ssoID = null;
041: Cookie[] cookies = request.getCookies();
042: for (int i = 0; i < cookies.length; i++) {
043: if (cookies[i].getName().equals(SSO_COOKIE_NAME)) {
044: ssoID = cookies[i].getValue();
045: break;
046: }
047: }
048: if (Log.isDebugEnabled())
049: Log.debug("get ssoID=" + ssoID);
050:
051: Principal principal = null;
052: Credential credential = null;
053: synchronized (_ssoId2Principal) {
054: principal = (Principal) _ssoId2Principal.get(ssoID);
055: credential = (Credential) _ssoPrincipal2Credential
056: .get(principal);
057: }
058:
059: if (Log.isDebugEnabled())
060: Log.debug("SSO principal=" + principal);
061:
062: if (principal != null && credential != null) {
063: // TODO - make this work for non webapps
064: UserRealm realm = ((WebAppContext) (request.getContext()
065: .getContextHandler())).getSecurityHandler()
066: .getUserRealm();
067: if (realm.reauthenticate(principal)) {
068: request.setUserPrincipal(principal);
069: return credential;
070: } else {
071: synchronized (_ssoId2Principal) {
072: _ssoId2Principal.remove(ssoID);
073: _ssoPrincipal2Credential.remove(principal);
074: _ssoUsername2Id.remove(principal.getName());
075: }
076: }
077: }
078: return null;
079: }
080:
081: /* ------------------------------------------------------------ */
082: public void setSingleSignOn(Request request, Response response,
083: Principal principal, Credential credential) {
084:
085: String ssoID = null;
086:
087: synchronized (_ssoId2Principal) {
088: // Create new SSO ID
089: while (true) {
090: ssoID = Long.toString(Math.abs(_random.nextLong()),
091: 30 + (int) (System.currentTimeMillis() % 7));
092: if (!_ssoId2Principal.containsKey(ssoID))
093: break;
094: }
095:
096: if (Log.isDebugEnabled())
097: Log.debug("set ssoID=" + ssoID);
098: _ssoId2Principal.put(ssoID, principal);
099: _ssoPrincipal2Credential.put(principal, credential);
100: _ssoUsername2Id.put(principal.getName(), ssoID);
101: }
102:
103: Cookie cookie = new Cookie(SSO_COOKIE_NAME, ssoID);
104: cookie.setPath("/");
105: response.addCookie(cookie);
106: }
107:
108: /* ------------------------------------------------------------ */
109: public void clearSingleSignOn(String username) {
110: synchronized (_ssoId2Principal) {
111: Object ssoID = _ssoUsername2Id.remove(username);
112: Object principal = _ssoId2Principal.remove(ssoID);
113: _ssoPrincipal2Credential.remove(principal);
114: }
115: }
116: }
|