1 23 package com.sun.enterprise.appclient; 24 25 import java.net.*; 26 import java.io.*; 27 import java.util.*; 28 import java.security.*; 29 30 import javax.security.auth.*; 31 import javax.security.auth.login.*; 32 33 import com.sun.enterprise.security.auth.LoginContextDriver; 34 import com.sun.enterprise.security.ClientSecurityContext; 35 import com.sun.enterprise.security.auth.login.PasswordCredential; 36 37 import java.util.logging.Logger ; 38 import java.util.logging.Level ; 39 import com.sun.logging.LogDomains; 40 45 public class HttpAuthenticator extends Authenticator 46 { 47 public static final boolean debug = false; 48 private AppContainer container = null; 49 private static Logger _logger = LogDomains.getLogger(LogDomains.ACC_LOGGER); 50 51 54 public HttpAuthenticator(AppContainer container) { 55 this.container = container; 56 } 57 58 63 protected PasswordAuthentication getPasswordAuthentication() 64 { 65 String user = null; 66 String password = null; 67 Subject subject = null; 68 69 String scheme = getRequestingScheme(); 70 if (_logger.isLoggable(Level.FINE)) { 71 _logger.fine("scheme=" + scheme); 72 _logger.fine("requesting prompt=" + getRequestingPrompt()); 73 _logger.fine("requesting protocol=" + getRequestingProtocol()); 74 } 75 76 ClientSecurityContext cont = ClientSecurityContext.getCurrent(); 77 subject = (cont != null) ? cont.getSubject() : null; 78 user = getUserName(subject); 79 password = getPassword(subject); 80 if(user == null || password == null) { 81 try { 82 if (_logger.isLoggable(Level.FINE)) { 83 _logger.fine("Initiating login again..."); 84 } 85 86 LoginContextDriver.doClientLogin(AppContainer.USERNAME_PASSWORD, 87 AppContainer.getCallbackHandler()); 88 cont = ClientSecurityContext.getCurrent(); 89 subject = cont.getSubject(); 90 user = getUserName(subject); 91 password = getPassword(subject); 92 } catch(Exception e) { 93 _logger.log(Level.FINE, "Exception " + e.toString(), e); 94 return null; 95 } 96 } 97 if (_logger.isLoggable(Level.FINE)) { 98 _logger.fine("Username:" + user); 99 } 100 return new PasswordAuthentication(user, password.toCharArray()); 101 } 102 103 106 private String getUserName(Subject s) { 107 String user = null; 108 if(s == null) 109 return null; 110 Set principalSet = s.getPrincipals(); 111 Iterator itr = principalSet.iterator(); 112 if(itr.hasNext()) { 113 Principal p = (Principal) itr.next(); 114 user = p.getName(); 115 } 116 return user; 117 } 118 119 122 private String getPassword(Subject s) { 123 String password = null; 124 if(s == null) 125 return null; 126 Set credentials = s.getPrivateCredentials(); 127 Iterator credIter = credentials.iterator(); 128 if(credIter.hasNext()) { 129 Object o = credIter.next(); 130 if(o instanceof PasswordCredential) { 131 PasswordCredential pc = (PasswordCredential) o; 132 password = pc.getPassword(); 134 } 135 } 136 return password; 137 } 138 } 139 140 | Popular Tags |