001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.geronimo.corba.security.config.css;
018    
019    import java.util.Iterator;
020    import java.util.Set;
021    import javax.security.auth.Subject;
022    
023    import org.apache.geronimo.security.jaas.NamedUsernamePasswordCredential;
024    import org.apache.geronimo.security.ContextManager;
025    
026    import org.apache.geronimo.corba.security.config.tss.TSSASMechConfig;
027    import org.apache.geronimo.corba.security.config.tss.TSSGSSUPMechConfig;
028    import org.apache.geronimo.corba.util.Util;
029    
030    
031    /**
032     * This GSSUP mechanism obtains its username and password from a named username
033     * password credential that is stored in the subject associated w/ the call
034     * stack.
035     *
036     * @version $Revision: 503493 $ $Date: 2007-02-04 13:47:55 -0800 (Sun, 04 Feb 2007) $
037     */
038    public class CSSGSSUPMechConfigDynamic implements CSSASMechConfig {
039    
040        private final String domain;
041        private transient byte[] encoding;
042    
043        public CSSGSSUPMechConfigDynamic(String domain) {
044            this.domain = domain;
045        }
046    
047        public short getSupports() {
048            return 0;
049        }
050    
051        public short getRequires() {
052            return 0;
053        }
054    
055        public boolean canHandle(TSSASMechConfig asMech) {
056            if (asMech instanceof TSSGSSUPMechConfig) return true;
057            if (asMech.getRequires() == 0) return true;
058    
059            return false;
060        }
061    
062        public byte[] encode() {
063            if (encoding == null) {
064                NamedUsernamePasswordCredential credential = null;
065                Subject subject = ContextManager.getNextCaller();
066    
067                Set creds = subject.getPrivateCredentials(NamedUsernamePasswordCredential.class);
068    
069                if (creds.size() != 0) {
070                    for (Iterator iter = creds.iterator(); iter.hasNext();) {
071                        NamedUsernamePasswordCredential temp = (NamedUsernamePasswordCredential) iter.next();
072                        if (temp.getName().equals(domain)) {
073                            credential = temp;
074                            break;
075                        }
076                    }
077                    if(credential != null) {
078                        String extendedUserName = Util.buildScopedUserName(credential.getUsername(), domain);
079                        encoding = Util.encodeGSSUPToken(Util.getORB(), Util.getCodec(), extendedUserName, new String(credential.getPassword()), domain);
080                    }
081                }
082    
083                if (encoding == null) encoding = new byte[0];
084            }
085            return encoding;
086        }
087    
088        public String toString() {
089            StringBuffer buf = new StringBuffer();
090            toString("", buf);
091            return buf.toString();
092        }
093    
094        public void toString(String spaces, StringBuffer buf) {
095            String moreSpaces = spaces + "  ";
096            buf.append(spaces).append("CSSGSSUPMechConfigDynamic: [\n");
097            buf.append(moreSpaces).append("domain:   ").append(domain).append("\n");
098            buf.append(spaces).append("]\n");
099        }
100    
101    }