}
public String authenticate(String authorizedName, String cred)
throws AuthenticationException, FatalErrorException {
if (authorizedName == null || "".equals(authorizedName)) {
throw new UnknownUserException(new ErrorMessage("errors.auth.NoPublisher", authorizedName));
}
int MaxBindingsPerService = -1;
int MaxServicesPerBusiness = -1;
int MaxTmodels = -1;
int MaxBusinesses = -1;
try {
MaxBindingsPerService = AppConfig.getConfiguration().getInt(Property.JUDDI_MAX_BINDINGS_PER_SERVICE, -1);
MaxServicesPerBusiness = AppConfig.getConfiguration().getInt(Property.JUDDI_MAX_SERVICES_PER_BUSINESS, -1);
MaxTmodels = AppConfig.getConfiguration().getInt(Property.JUDDI_MAX_TMODELS_PER_PUBLISHER, -1);
MaxBusinesses = AppConfig.getConfiguration().getInt(Property.JUDDI_MAX_BUSINESSES_PER_PUBLISHER, -1);
} catch (Exception ex) {
MaxBindingsPerService = -1;
MaxServicesPerBusiness = -1;
MaxTmodels = -1;
MaxBusinesses = -1;
logger.error("config exception! " + authorizedName, ex);
}
boolean isLdapUser = false;
try {
env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, AppConfig.getConfiguration().getString(Property.JUDDI_AUTHENTICATOR_INITIAL_CONTEXT, "com.sun.jndi.ldap.LdapCtxFactory"));
env.put(Context.SECURITY_AUTHENTICATION, AppConfig.getConfiguration().getString(Property.JUDDI_AUTHENTICATOR_STYLE, "simple"));
env.put(Context.PROVIDER_URL, url); // organization ldap url, example ldap://localhost:389
env.put(Context.SECURITY_PRINCIPAL, authorizedName);
env.put(Context.SECURITY_CREDENTIALS, cred);
ctx = new InitialLdapContext(env, null);
isLdapUser = true;
logger.info(authorizedName + " is authenticated");
} catch (ConfigurationException e) {
logger.error(authorizedName + " is not authenticated", e);
throw new UnknownUserException(new ErrorMessage("errors.auth.NoPublisher", authorizedName));
}
catch (NamingException e) {
logger.error(authorizedName + " is not authenticated");
throw new UnknownUserException(new ErrorMessage("errors.auth.NoPublisher", authorizedName));
}finally {
try {
ctx.close();
} catch (NamingException e) {
logger.error("Context close failure " + e);
}
}
if (isLdapUser) {
EntityManager em = PersistenceManager.getEntityManager();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
Publisher publisher = em.find(Publisher.class, authorizedName);
if (publisher == null) {
logger.warn("Publisher was not found, adding the publisher in on the fly.");
publisher = new Publisher();
publisher.setAuthorizedName(authorizedName);
publisher.setIsAdmin("false");
publisher.setIsEnabled("true");
publisher.setMaxBindingsPerService(MaxBindingsPerService);
publisher.setMaxBusinesses(MaxBusinesses);
publisher.setMaxServicesPerBusiness(MaxServicesPerBusiness);
publisher.setMaxTmodels(MaxTmodels);
publisher.setPublisherName("Unknown");
em.persist(publisher);
tx.commit();
}
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
} else {
throw new UnknownUserException(new ErrorMessage("errors.auth.NoPublisher", authorizedName));
}
return authorizedName;
}