1 16 package org.outerj.daisy.authentication.impl; 17 18 import org.outerj.daisy.authentication.AuthenticationScheme; 19 import org.outerj.daisy.authentication.AuthenticationException; 20 import org.outerj.daisy.authentication.UserCreator; 21 import org.outerj.daisy.repository.Credentials; 22 import org.outerj.daisy.repository.user.User; 23 import org.outerj.daisy.repository.user.UserManager; 24 import jcifs.smb.SmbException; 25 import jcifs.smb.SmbAuthException; 26 import jcifs.smb.SmbSession; 27 import jcifs.smb.NtlmPasswordAuthentication; 28 import jcifs.UniAddress; 29 30 import java.net.UnknownHostException ; 31 32 public class NtlmAuthenticationScheme implements AuthenticationScheme { 33 private final String name; 34 private final String description; 35 private final String domainControllerAddress; 36 private final String domain; 37 private final UserCreator userCreator; 38 39 public NtlmAuthenticationScheme(String name, String description, String domainControllerAddress, String domain, UserCreator userCreator) { 40 this.name = name; 41 this.description = description; 42 this.domainControllerAddress = domainControllerAddress; 43 this.domain = domain; 44 this.userCreator = userCreator; 45 } 46 47 public String getName() { 48 return name; 49 } 50 51 public String getDescription() { 52 return description; 53 } 54 55 public boolean check(Credentials credentials) throws AuthenticationException { 56 UniAddress mydomaincontroller = null; 57 try { 58 mydomaincontroller = UniAddress.getByName(domainControllerAddress); 59 } catch (UnknownHostException e) { 60 throw new AuthenticationException("Error authenticating using NTLM.", e); 61 } 62 NtlmPasswordAuthentication mycreds = new NtlmPasswordAuthentication(domain, credentials.getLogin(), credentials.getPassword()); 63 try { 64 SmbSession.logon( mydomaincontroller, mycreds ); 65 return true; 67 } catch( SmbAuthException sae ) { 68 return false; 70 } catch( SmbException se ) { 71 throw new AuthenticationException("Error authenticating using NTLM.", se); 73 } 74 } 75 76 public void clearCaches() { 77 } 79 80 public User createUser(Credentials crendentials, UserManager userManager) throws AuthenticationException { 81 if (userCreator != null) { 82 if (check(crendentials)) { 83 return userCreator.create(crendentials.getLogin(), userManager); 84 } 85 } 86 return null; 87 } 88 } 89 | Popular Tags |