1 17 package org.alfresco.repo.security.authentication.jaas; 18 19 import java.io.IOException ; 20 21 import javax.security.auth.callback.Callback ; 22 import javax.security.auth.callback.CallbackHandler ; 23 import javax.security.auth.callback.LanguageCallback ; 24 import javax.security.auth.callback.NameCallback ; 25 import javax.security.auth.callback.PasswordCallback ; 26 import javax.security.auth.callback.UnsupportedCallbackException ; 27 import javax.security.auth.login.LoginContext ; 28 import javax.security.auth.login.LoginException ; 29 import javax.security.sasl.AuthorizeCallback ; 30 import javax.security.sasl.RealmCallback ; 31 32 import org.alfresco.i18n.I18NUtil; 33 import org.alfresco.repo.security.authentication.AbstractAuthenticationComponent; 34 import org.alfresco.repo.security.authentication.AuthenticationException; 35 36 78 public class JAASAuthenticationComponent extends AbstractAuthenticationComponent 79 { 80 81 84 private String jaasConfigEntryName = "Alfresco"; 85 86 89 private String realm = null; 90 91 public JAASAuthenticationComponent() 92 { 93 super(); 94 } 95 96 98 public void setJaasConfigEntryName(String jaasConfigEntryName) 99 { 100 this.jaasConfigEntryName = jaasConfigEntryName; 101 } 102 103 104 public void setRealm(String realm) 105 { 106 this.realm = realm; 107 } 108 109 112 @Override 113 protected boolean implementationAllowsGuestLogin() 114 { 115 return false; 116 } 117 118 121 public void authenticate(String userName, char[] password) throws AuthenticationException 122 { 123 124 LoginContext lc; 125 try 126 { 127 lc = new LoginContext (jaasConfigEntryName, new SimpleCallback(userName, realm, password)); 128 } 129 catch (LoginException e) 130 { 131 throw new AuthenticationException("Login Failed", e); 132 } 133 try 134 { 135 lc.login(); 136 setCurrentUser(userName); 138 } 139 catch (LoginException e) 140 { 141 throw new AuthenticationException("Login Failed", e); 142 } 143 144 } 145 146 151 private static class SimpleCallback implements CallbackHandler 152 { 153 String userName; 154 155 String realm; 156 157 char[] password; 158 159 SimpleCallback(String userName, String realm, char[] password) 160 { 161 this.userName = userName; 162 this.realm = realm; 163 this.password = password; 164 } 165 166 public void handle(Callback [] callbacks) throws IOException , UnsupportedCallbackException 167 { 168 for (int i = 0; i < callbacks.length; i++) 169 { 170 if (callbacks[i] instanceof AuthorizeCallback ) 171 { 172 AuthorizeCallback cb = (AuthorizeCallback ) callbacks[i]; 173 cb.setAuthorized(false); 174 } 175 else if (callbacks[i] instanceof LanguageCallback ) 176 { 177 LanguageCallback cb = (LanguageCallback ) callbacks[i]; 178 cb.setLocale(I18NUtil.getLocale()); 179 } 180 else if (callbacks[i] instanceof NameCallback ) 181 { 182 NameCallback cb = (NameCallback ) callbacks[i]; 183 cb.setName(userName); 184 } 185 else if (callbacks[i] instanceof PasswordCallback ) 186 { 187 PasswordCallback cb = (PasswordCallback ) callbacks[i]; 188 cb.setPassword(password); 189 } 190 else if (callbacks[i] instanceof RealmCallback ) 191 { 192 RealmCallback cb = (RealmCallback ) callbacks[i]; 193 cb.setText(realm); 194 } 195 else 196 { 197 throw new UnsupportedCallbackException (callbacks[i]); 198 } 199 } 200 } 201 } 202 } 203 | Popular Tags |