1 25 package org.objectweb.jonas.security.iiop; 26 27 import java.io.UnsupportedEncodingException ; 28 29 import org.omg.GSSUP.InitialContextToken; 30 31 import org.objectweb.carol.util.csiv2.gss.GSSHelper; 32 33 import org.objectweb.jonas.common.Log; 34 import org.objectweb.jonas.security.AbsSecurityContextHelper; 35 36 import org.objectweb.security.context.SecurityContext; 37 import org.objectweb.security.context.SecurityCurrent; 38 39 import org.objectweb.util.monolog.api.Logger; 40 41 42 48 public class SecurityContextHelper extends AbsSecurityContextHelper { 49 50 53 private static SecurityContextHelper instance = null; 54 55 58 private static final String CSIV2_REALM_KEY = "jonas.service.security.csiv2.realm"; 59 60 63 private static final String DEFAULT_CSIV2_REALM = "memrlm_1"; 64 65 66 69 private static final String DOMAIN_SEPARATOR = "@"; 70 71 74 private static final String DEFAULT_DOMAIN_NAME = "default"; 75 76 79 private static final String ENCODING = "UTF-8"; 80 81 84 private static Logger logger = Log.getLogger(Log.JONAS_CSIV2_SECURITY_PREFIX); 85 86 89 private SecurityContextHelper() { 90 } 91 92 95 public static SecurityContextHelper getInstance() { 96 if (instance == null) { 97 instance = new SecurityContextHelper(); 98 } 99 return instance; 100 } 101 102 105 protected Logger getLogger() { 106 return logger; 107 } 108 109 112 protected String getRealmKey() { 113 return CSIV2_REALM_KEY; 114 } 115 116 119 protected String getRealmDefault() { 120 return DEFAULT_CSIV2_REALM; 121 } 122 123 128 protected void loginAuthenticationToken(String userName, String password) { 129 String principalName = userName.split(DOMAIN_SEPARATOR)[0]; 131 String credential = password; 132 login(principalName, credential); 133 } 134 135 139 protected void loginIdentiyToken(String principalName) { 140 String credential = principalName; 141 login(principalName, credential); 142 } 143 144 148 public String getIdentityToken() { 149 SecurityCurrent current = SecurityCurrent.getCurrent(); 150 SecurityContext securityContext = current.getSecurityContext(); 151 152 if (securityContext.peekRunAsPrincipal() != null) { 153 return securityContext.peekRunAsPrincipal(); 154 } else { 155 return securityContext.getCallerPrincipal(false).getName(); 156 } 157 } 158 159 164 public InitialContextToken getInitialContextToken() throws UnsupportedEncodingException { 165 SecurityCurrent current = SecurityCurrent.getCurrent(); 166 SecurityContext securityContext = current.getSecurityContext(); 167 String principalName = securityContext.getPrincipalName(); 168 String userName = principalName + DOMAIN_SEPARATOR + DEFAULT_DOMAIN_NAME; 169 String password = principalName; 170 byte[] user = userName.getBytes(ENCODING); 171 byte[] pass = password.getBytes(ENCODING); 172 byte[] domain = GSSHelper.encodeExported(DEFAULT_DOMAIN_NAME); 173 return new InitialContextToken(user, pass, domain); 174 175 } 176 177 178 } 179 | Popular Tags |