00001
00025 package org.objectweb.jonas.security.iiop;
00026
00027 import java.io.UnsupportedEncodingException;
00028
00029 import org.omg.GSSUP.InitialContextToken;
00030
00031 import org.objectweb.carol.util.csiv2.gss.GSSHelper;
00032
00033 import org.objectweb.jonas.common.Log;
00034 import org.objectweb.jonas.security.AbsSecurityContextHelper;
00035
00036 import org.objectweb.security.context.SecurityContext;
00037 import org.objectweb.security.context.SecurityCurrent;
00038
00039 import org.objectweb.util.monolog.api.Logger;
00040
00041
00048 public class SecurityContextHelper extends AbsSecurityContextHelper {
00049
00053 private static SecurityContextHelper instance = null;
00054
00058 private static final String CSIV2_REALM_KEY = "jonas.service.security.csiv2.realm";
00059
00063 private static final String DEFAULT_CSIV2_REALM = "memrlm_1";
00064
00065
00069 private static final String DOMAIN_SEPARATOR = "@";
00070
00074 private static final String DEFAULT_DOMAIN_NAME = "default";
00075
00079 private static final String ENCODING = "UTF-8";
00080
00084 private static Logger logger = Log.getLogger(Log.JONAS_CSIV2_SECURITY_PREFIX);
00085
00089 private SecurityContextHelper() {
00090 }
00091
00095 public static SecurityContextHelper getInstance() {
00096 if (instance == null) {
00097 instance = new SecurityContextHelper();
00098 }
00099 return instance;
00100 }
00101
00105 protected Logger getLogger() {
00106 return logger;
00107 }
00108
00112 protected String getRealmKey() {
00113 return CSIV2_REALM_KEY;
00114 }
00115
00119 protected String getRealmDefault() {
00120 return DEFAULT_CSIV2_REALM;
00121 }
00122
00128 protected void loginAuthenticationToken(String userName, String password) {
00129
00130 String principalName = userName.split(DOMAIN_SEPARATOR)[0];
00131 String credential = password;
00132 login(principalName, credential);
00133 }
00134
00139 protected void loginIdentiyToken(String principalName) {
00140 String credential = principalName;
00141 login(principalName, credential);
00142 }
00143
00148 public String getIdentityToken() {
00149 SecurityCurrent current = SecurityCurrent.getCurrent();
00150 SecurityContext securityContext = current.getSecurityContext();
00151
00152 if (securityContext.peekRunAsPrincipal() != null) {
00153 return securityContext.peekRunAsPrincipal();
00154 } else {
00155 return securityContext.getCallerPrincipal(false).getName();
00156 }
00157 }
00158
00164 public InitialContextToken getInitialContextToken() throws UnsupportedEncodingException {
00165 SecurityCurrent current = SecurityCurrent.getCurrent();
00166 SecurityContext securityContext = current.getSecurityContext();
00167 String principalName = securityContext.getPrincipalName();
00168 String userName = principalName + DOMAIN_SEPARATOR + DEFAULT_DOMAIN_NAME;
00169 String password = principalName;
00170 byte[] user = userName.getBytes(ENCODING);
00171 byte[] pass = password.getBytes(ENCODING);
00172 byte[] domain = GSSHelper.encodeExported(DEFAULT_DOMAIN_NAME);
00173 return new InitialContextToken(user, pass, domain);
00174
00175 }
00176
00177
00178 }