001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: ServerSideCallbackHandler.java 7546 2005-10-20 15:00:56Z sauthieg $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.wssample.security;
025:
026: import java.io.IOException;
027:
028: import javax.security.auth.callback.Callback;
029: import javax.security.auth.callback.CallbackHandler;
030: import javax.security.auth.callback.UnsupportedCallbackException;
031:
032: import org.objectweb.jonas.security.SecurityService;
033: import org.objectweb.jonas.security.realm.factory.JResource;
034: import org.objectweb.jonas.security.realm.principals.User;
035: import org.objectweb.jonas.service.ServiceManager;
036:
037: import org.apache.ws.security.WSPasswordCallback;
038:
039: /**
040: * WSS4J Sample CallbackHandler. It is intended to be used from a JOnAS server (servlet/ejb client & JAX-RPC/SSB endpoint).
041: * It will get the 'memrlm_1' instance and try to get the needed password from that JResource.
042: *
043: * @author Guillaume Sauthier
044: */
045: public class ServerSideCallbackHandler implements CallbackHandler {
046:
047: /**
048: * encryptionUser value (used to access keystore - look in crypto.properties)
049: */
050: private static final String ENCRYPTION_USER = "jonas-ws";
051:
052: /**
053: * encryptionUser password : password used to access keystore
054: */
055: private static final String ENCRYPTION_PASSWORD = "security";
056:
057: /**
058: * @see javax.security.auth.callback.CallbackHandler#handle(javax.security.auth.callback.Callback[])
059: */
060: public void handle(Callback[] callbacks) throws IOException,
061: UnsupportedCallbackException {
062: for (int i = 0; i < callbacks.length; i++) {
063: if (callbacks[i] instanceof WSPasswordCallback) {
064: WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];
065: // set the password given a username
066:
067: if (!ENCRYPTION_USER.equals(pc.getIdentifer())) {
068: // Get the Security Service
069: SecurityService securityService = null;
070: try {
071: securityService = (SecurityService) ServiceManager
072: .getInstance().getSecurityService();
073: } catch (Exception e) {
074: // Can't retrieve Security service
075: throw new IOException(
076: "can't retrieve Security service");
077: }
078:
079: // Get the resource from the security service
080: JResource jResource = securityService
081: .getJResource("memrlm_1");
082: if (jResource == null) {
083: throw new IOException(
084: "Can't retrieve resource 'memrlm_1' from the security service");
085: }
086:
087: User user = jResource.findUser(pc.getIdentifer());
088:
089: if (user == null) {
090: throw new IOException("Cannot find user '"
091: + pc.getIdentifer() + "' in Realm.");
092: }
093: // if we're here, we found the user, so we can continue ...
094: pc
095: .setPassword(user.getHashPassword()
096: .getPassword());
097: } else {
098: // this is the ecryption username
099: // we must return the correct password (stored in crypto.properties)
100: pc.setPassword(ENCRYPTION_PASSWORD);
101: }
102:
103: } else {
104: throw new UnsupportedCallbackException(callbacks[i],
105: "Unrecognized Callback");
106: }
107: }
108: }
109:
110: }
|