001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.resource.adapter.jms;
023:
024: import java.util.Set;
025: import java.util.Iterator;
026: import java.security.AccessController;
027: import java.security.PrivilegedAction;
028:
029: import javax.security.auth.Subject;
030:
031: import javax.resource.spi.ManagedConnectionFactory;
032: import javax.resource.spi.SecurityException;
033: import javax.resource.spi.ConnectionRequestInfo;
034:
035: import javax.resource.spi.security.PasswordCredential;
036:
037: /**
038: * Credential information
039: *
040: * @author <a href="mailto:peter.antman@tim.se">Peter Antman </a>.
041: * @author <a href="mailto:adrian@jboss.com">Adrian Brock</a>
042: * @version $Revision: 57189 $
043: */
044: public class JmsCred {
045: public String name;
046:
047: public String pwd;
048:
049: public JmsCred() {
050: // empty
051: }
052:
053: /**
054: * Get our own simple cred
055: */
056: public static JmsCred getJmsCred(ManagedConnectionFactory mcf,
057: Subject subject, ConnectionRequestInfo info)
058: throws SecurityException {
059: JmsCred jc = new JmsCred();
060: if (subject == null && info != null) {
061: // Credentials specifyed on connection request
062: jc.name = ((JmsConnectionRequestInfo) info).getUserName();
063: jc.pwd = ((JmsConnectionRequestInfo) info).getPassword();
064: } else if (subject != null) {
065: // Credentials from appserver
066: PasswordCredential pwdc = GetCredentialAction
067: .getCredential(subject, mcf);
068: if (pwdc == null) {
069: // No hit - we do need creds
070: throw new SecurityException(
071: "No Password credentials found");
072: }
073: jc.name = pwdc.getUserName();
074: jc.pwd = new String(pwdc.getPassword());
075: } else {
076: throw new SecurityException(
077: "No Subject or ConnectionRequestInfo set, could not get credentials");
078: }
079: return jc;
080: }
081:
082: public String toString() {
083: return super .toString() + "{ username=" + name
084: + ", password=**** }";
085: }
086:
087: private static class GetCredentialAction implements
088: PrivilegedAction {
089: Subject subject;
090: ManagedConnectionFactory mcf;
091:
092: GetCredentialAction(Subject subject,
093: ManagedConnectionFactory mcf) {
094: this .subject = subject;
095: this .mcf = mcf;
096: }
097:
098: public Object run() {
099: Set creds = subject
100: .getPrivateCredentials(PasswordCredential.class);
101: PasswordCredential pwdc = null;
102: Iterator credentials = creds.iterator();
103: while (credentials.hasNext()) {
104: PasswordCredential curCred = (PasswordCredential) credentials
105: .next();
106: if (curCred.getManagedConnectionFactory().equals(mcf)) {
107: pwdc = curCred;
108: break;
109: }
110: }
111: return pwdc;
112: }
113:
114: static PasswordCredential getCredential(Subject subject,
115: ManagedConnectionFactory mcf) {
116: GetCredentialAction action = new GetCredentialAction(
117: subject, mcf);
118: PasswordCredential pc = (PasswordCredential) AccessController
119: .doPrivileged(action);
120: return pc;
121: }
122: }
123: }
|