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.security;
023:
024: import java.security.AccessController;
025: import java.security.PrivilegedAction;
026: import java.security.Principal;
027: import java.io.UnsupportedEncodingException;
028:
029: import org.jboss.security.SecurityAssociation;
030: import org.jboss.security.RunAsIdentity;
031:
032: /** PrivilegedActions used by this package
033: *
034: * @author Scott.Stark@jboss.org
035: * @version $Revision: 57189 $
036: */
037: class GetPrincipalInfoAction {
038: /* Obtain the password credential by trying char[], byte[],
039: and toString()
040: */
041: private static char[] getPassword() {
042: Object credential = SecurityAssociation.getCredential();
043: char[] password = null;
044: if (credential instanceof char[]) {
045: password = (char[]) credential;
046: } else if (credential instanceof byte[]) {
047: try {
048: String tmp = new String((byte[]) credential, "UTF-8");
049: password = tmp.toCharArray();
050: } catch (UnsupportedEncodingException e) {
051: throw new SecurityException(e.getMessage());
052: }
053: } else if (credential != null) {
054: String tmp = credential.toString();
055: password = tmp.toCharArray();
056: }
057: return password;
058: }
059:
060: interface PrincipalActions {
061: PrincipalActions PRIVILEGED = new PrincipalActions() {
062: private final PrivilegedAction peekAction = new PrivilegedAction() {
063: public Object run() {
064: return SecurityAssociation.peekRunAsIdentity();
065: }
066: };
067:
068: private final PrivilegedAction getPrincipalAction = new PrivilegedAction() {
069: public Object run() {
070: return SecurityAssociation.getPrincipal();
071: }
072: };
073:
074: private final PrivilegedAction getCredentialAction = new PrivilegedAction() {
075: public Object run() {
076: return getPassword();
077: }
078: };
079:
080: public RunAsIdentity peek() {
081: return (RunAsIdentity) AccessController
082: .doPrivileged(peekAction);
083: }
084:
085: public Principal getPrincipal() {
086: return (Principal) AccessController
087: .doPrivileged(getPrincipalAction);
088: }
089:
090: public char[] getCredential() {
091: return (char[]) AccessController
092: .doPrivileged(getCredentialAction);
093: }
094: };
095:
096: PrincipalActions NON_PRIVILEGED = new PrincipalActions() {
097: public RunAsIdentity peek() {
098: return SecurityAssociation.peekRunAsIdentity();
099: }
100:
101: public Principal getPrincipal() {
102: return SecurityAssociation.getPrincipal();
103: }
104:
105: public char[] getCredential() {
106: return getPassword();
107: }
108: };
109:
110: Principal getPrincipal();
111:
112: char[] getCredential();
113:
114: RunAsIdentity peek();
115: }
116:
117: static Principal getPrincipal() {
118: Principal principal;
119: if (System.getSecurityManager() == null) {
120: principal = PrincipalActions.NON_PRIVILEGED.getPrincipal();
121: } else {
122: principal = PrincipalActions.PRIVILEGED.getPrincipal();
123: }
124: return principal;
125: }
126:
127: static char[] getCredential() {
128: char[] credential;
129: if (System.getSecurityManager() == null) {
130: credential = PrincipalActions.NON_PRIVILEGED
131: .getCredential();
132: } else {
133: credential = PrincipalActions.PRIVILEGED.getCredential();
134: }
135: return credential;
136: }
137:
138: static RunAsIdentity peekRunAsIdentity() {
139: if (System.getSecurityManager() == null) {
140: return PrincipalActions.NON_PRIVILEGED.peek();
141: } else {
142: return PrincipalActions.PRIVILEGED.peek();
143: }
144: }
145:
146: }
|