| <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>LdapExpandedAuthenticator.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">jUDDI Core Services</a> > <a href="index.source.html" class="el_package">org.apache.juddi.v3.auth</a> > <span class="el_source">LdapExpandedAuthenticator.java</span></div><h1>LdapExpandedAuthenticator.java</h1><pre class="source lang-java linenums">/* |
| * Copyright 2001-2009 The Apache Software Foundation. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package org.apache.juddi.v3.auth; |
| |
| import java.util.HashMap; |
| import java.util.Hashtable; |
| import java.util.Map; |
| |
| import javax.naming.Context; |
| import javax.naming.NamingException; |
| import javax.naming.ldap.InitialLdapContext; |
| import javax.naming.ldap.LdapContext; |
| import javax.persistence.EntityManager; |
| import javax.persistence.EntityTransaction; |
| import javax.xml.ws.WebServiceContext; |
| |
| import org.apache.commons.logging.Log; |
| import org.apache.commons.logging.LogFactory; |
| |
| import org.apache.juddi.config.AppConfig; |
| import org.apache.juddi.config.PersistenceManager; |
| import org.apache.juddi.config.Property; |
| import org.apache.juddi.model.Publisher; |
| import org.apache.juddi.model.UddiEntityPublisher; |
| import org.apache.juddi.v3.error.AuthenticationException; |
| import org.apache.juddi.v3.error.ErrorMessage; |
| import org.apache.juddi.v3.error.FatalErrorException; |
| import org.apache.juddi.v3.error.UnknownUserException; |
| |
| import org.apache.commons.configuration.ConfigurationException; |
| |
| /** |
| * This is an expanded implementation of jUDDI's Authenticator interface, that uses the |
| * LDAP with string formatting to enable users to sign in as a common name instead of the full |
| * distinguished name. |
| * |
| * Usage: |
| * |
| * To use this class you must add the following properties to the |
| * juddiv3.xml file: |
| * |
| * # The LDAP Authenticator |
| * juddi/auth/class=org.apache.juddi.v3.auth.LdapSimpleAuthenticator |
| * |
| * # LDAP authentication URL |
| * juddi/auth/url=ldap://localhost:389 |
| * |
| * juddi/auth/ldapexp=CN=%s, OU=Users,DC=Domain, etc |
| * |
| * This authenticator assumes that the publisher username can be reformatted to a LDAP |
| * common name. This is common for Microsoft based LDAPs. The configuration item juddi.authenticator.ldapexp |
| * should contain exactly one instance of "%s", which is replaced by the requestor's username |
| * |
| * |
| * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a> |
| */ |
| public class LdapExpandedAuthenticator implements Authenticator { |
| <span class="nc" id="L71"> private Log logger = LogFactory.getLog(this.getClass());</span> |
| |
| <span class="nc" id="L73"> private LdapContext ctx = null;</span> |
| //this needs to be a Hashtable, HashMap won't work here |
| <span class="nc" id="L75"> private Hashtable<String, String> env = null;</span> |
| <span class="nc" id="L76"> private String url = null;</span> |
| |
| private static final String DEFAULT_URL = "ldap://localhost:389"; |
| |
| <span class="nc" id="L80"> public LdapExpandedAuthenticator() throws NamingException, ConfigurationException {</span> |
| <span class="nc" id="L81"> String authURL = null;</span> |
| try { |
| <span class="nc" id="L83"> authURL = AppConfig.getConfiguration().getString(Property.JUDDI_AUTHENTICATOR_URL, DEFAULT_URL);</span> |
| <span class="nc" id="L84"> } catch (ConfigurationException ce) {</span> |
| <span class="nc" id="L85"> logger.error("Configuration exception occurred retrieving: " + Property.JUDDI_AUTHENTICATOR_URL);</span> |
| <span class="nc" id="L86"> throw new NamingException(Property.JUDDI_AUTHENTICATOR_URL + " missing from config or config is not available.");</span> |
| <span class="nc" id="L87"> }</span> |
| <span class="nc" id="L88"> init(authURL);</span> |
| <span class="nc" id="L89"> }</span> |
| |
| <span class="nc" id="L91"> public LdapExpandedAuthenticator(String url) throws NamingException, ConfigurationException {</span> |
| <span class="nc" id="L92"> init(url);</span> |
| <span class="nc" id="L93"> }</span> |
| |
| public void init(String url) throws NamingException, ConfigurationException { |
| <span class="nc" id="L96"> env = new Hashtable<String, String>();</span> |
| <span class="nc" id="L97"> env.put(Context.INITIAL_CONTEXT_FACTORY, AppConfig.getConfiguration().getString(Property.JUDDI_AUTHENTICATOR_INITIAL_CONTEXT, "com.sun.jndi.ldap.LdapCtxFactory"));</span> |
| <span class="nc" id="L98"> env.put(Context.SECURITY_AUTHENTICATION, AppConfig.getConfiguration().getString(Property.JUDDI_AUTHENTICATOR_STYLE, "simple"));</span> |
| <span class="nc" id="L99"> env.put(Context.PROVIDER_URL, url); // organization ldap url, example ldap://localhost:389</span> |
| |
| <span class="nc" id="L101"> this.url = url;</span> |
| |
| try { |
| <span class="nc" id="L104"> ctx = new InitialLdapContext(env, null);</span> |
| <span class="nc" id="L105"> } catch (NamingException e) {</span> |
| <span class="nc" id="L106"> logger.error("Naming exception " + e);</span> |
| <span class="nc" id="L107"> throw e;</span> |
| <span class="nc" id="L108"> }</span> |
| <span class="nc" id="L109"> }</span> |
| |
| public String authenticate(String authorizedName, String cred) |
| throws AuthenticationException, FatalErrorException { |
| <span class="nc bnc" id="L113" title="All 4 branches missed."> if (authorizedName == null || "".equals(authorizedName)) {</span> |
| <span class="nc" id="L114"> throw new UnknownUserException(new ErrorMessage("errors.auth.NoPublisher", authorizedName));</span> |
| } |
| |
| <span class="nc" id="L117"> boolean isLdapUser = false;</span> |
| |
| <span class="nc" id="L119"> int MaxBindingsPerService = -1;</span> |
| <span class="nc" id="L120"> int MaxServicesPerBusiness = -1;</span> |
| <span class="nc" id="L121"> int MaxTmodels = -1;</span> |
| <span class="nc" id="L122"> int MaxBusinesses = -1;</span> |
| try { |
| <span class="nc" id="L124"> MaxBindingsPerService = AppConfig.getConfiguration().getInt(Property.JUDDI_MAX_BINDINGS_PER_SERVICE, -1);</span> |
| <span class="nc" id="L125"> MaxServicesPerBusiness = AppConfig.getConfiguration().getInt(Property.JUDDI_MAX_SERVICES_PER_BUSINESS, -1);</span> |
| <span class="nc" id="L126"> MaxTmodels = AppConfig.getConfiguration().getInt(Property.JUDDI_MAX_TMODELS_PER_PUBLISHER, -1);</span> |
| <span class="nc" id="L127"> MaxBusinesses = AppConfig.getConfiguration().getInt(Property.JUDDI_MAX_BUSINESSES_PER_PUBLISHER, -1);</span> |
| <span class="nc" id="L128"> } catch (Exception ex) {</span> |
| <span class="nc" id="L129"> MaxBindingsPerService = -1;</span> |
| <span class="nc" id="L130"> MaxServicesPerBusiness = -1;</span> |
| <span class="nc" id="L131"> MaxTmodels = -1;</span> |
| <span class="nc" id="L132"> MaxBusinesses = -1;</span> |
| <span class="nc" id="L133"> logger.error("config exception! " + authorizedName, ex);</span> |
| <span class="nc" id="L134"> }</span> |
| |
| try { |
| <span class="nc" id="L137"> env = new Hashtable<String, String>();</span> |
| <span class="nc" id="L138"> env.put(Context.INITIAL_CONTEXT_FACTORY, AppConfig.getConfiguration().getString(Property.JUDDI_AUTHENTICATOR_INITIAL_CONTEXT, "com.sun.jndi.ldap.LdapCtxFactory"));</span> |
| <span class="nc" id="L139"> env.put(Context.SECURITY_AUTHENTICATION, AppConfig.getConfiguration().getString(Property.JUDDI_AUTHENTICATOR_STYLE, "simple"));</span> |
| |
| <span class="nc" id="L141"> env.put(Context.PROVIDER_URL, url); // organization ldap url, example ldap://localhost:389</span> |
| <span class="nc" id="L142"> String format = String.format(AppConfig.getConfiguration().getString(Property.JUDDI_AUTHENTICATOR_LDAP_EXPANDED_STR), authorizedName);</span> |
| |
| <span class="nc" id="L144"> env.put(Context.SECURITY_PRINCIPAL, format);</span> |
| <span class="nc" id="L145"> env.put(Context.SECURITY_CREDENTIALS, cred);</span> |
| <span class="nc" id="L146"> ctx = new InitialLdapContext(env, null);</span> |
| <span class="nc" id="L147"> isLdapUser = true;</span> |
| <span class="nc" id="L148"> logger.info(authorizedName + " is authenticated");</span> |
| |
| <span class="nc" id="L150"> } catch (ConfigurationException e) {</span> |
| <span class="nc" id="L151"> logger.error(authorizedName + " is not authenticated", e);</span> |
| <span class="nc" id="L152"> throw new UnknownUserException(new ErrorMessage("errors.auth.NoPublisher", authorizedName));</span> |
| } |
| <span class="nc" id="L154"> catch (NamingException e) {</span> |
| <span class="nc" id="L155"> logger.error(authorizedName + " is not authenticated");</span> |
| <span class="nc" id="L156"> throw new UnknownUserException(new ErrorMessage("errors.auth.NoPublisher", authorizedName));</span> |
| }finally { |
| <span class="nc" id="L158"> try {</span> |
| <span class="nc" id="L159"> ctx.close();</span> |
| <span class="nc" id="L160"> } catch (NamingException e) {</span> |
| <span class="nc" id="L161"> logger.error("Context close failure " + e);</span> |
| <span class="nc" id="L162"> }</span> |
| <span class="nc" id="L163"> }</span> |
| |
| <span class="nc bnc" id="L165" title="All 2 branches missed."> if (isLdapUser) {</span> |
| <span class="nc" id="L166"> EntityManager em = PersistenceManager.getEntityManager();</span> |
| <span class="nc" id="L167"> EntityTransaction tx = em.getTransaction();</span> |
| try { |
| <span class="nc" id="L169"> tx.begin();</span> |
| <span class="nc" id="L170"> Publisher publisher = em.find(Publisher.class, authorizedName);</span> |
| <span class="nc bnc" id="L171" title="All 2 branches missed."> if (publisher == null) {</span> |
| <span class="nc" id="L172"> logger.warn("Publisher was not found in the database, adding the publisher in on the fly.");</span> |
| <span class="nc" id="L173"> publisher = new Publisher();</span> |
| <span class="nc" id="L174"> publisher.setAuthorizedName(authorizedName);</span> |
| <span class="nc" id="L175"> publisher.setIsAdmin("false");</span> |
| <span class="nc" id="L176"> publisher.setIsEnabled("true");</span> |
| <span class="nc" id="L177"> publisher.setMaxBindingsPerService(MaxBindingsPerService);</span> |
| <span class="nc" id="L178"> publisher.setMaxBusinesses(MaxBusinesses);</span> |
| <span class="nc" id="L179"> publisher.setMaxServicesPerBusiness(MaxServicesPerBusiness);</span> |
| <span class="nc" id="L180"> publisher.setMaxTmodels(MaxTmodels);</span> |
| <span class="nc" id="L181"> publisher.setPublisherName("Unknown");</span> |
| <span class="nc" id="L182"> em.persist(publisher);</span> |
| <span class="nc" id="L183"> tx.commit();</span> |
| } |
| } finally { |
| <span class="nc bnc" id="L186" title="All 4 branches missed."> if (tx.isActive()) {</span> |
| <span class="nc" id="L187"> tx.rollback();</span> |
| } |
| <span class="nc" id="L189"> em.close();</span> |
| <span class="nc" id="L190"> }</span> |
| <span class="nc" id="L191"> } else {</span> |
| <span class="nc" id="L192"> throw new UnknownUserException(new ErrorMessage("errors.auth.NoPublisher", authorizedName));</span> |
| } |
| <span class="nc" id="L194"> return authorizedName;</span> |
| } |
| |
| public UddiEntityPublisher identify(String authInfo, String authorizedName, WebServiceContext ctx) throws AuthenticationException, FatalErrorException { |
| <span class="nc" id="L198"> EntityManager em = PersistenceManager.getEntityManager();</span> |
| <span class="nc" id="L199"> EntityTransaction tx = em.getTransaction();</span> |
| try { |
| <span class="nc" id="L201"> tx.begin();</span> |
| <span class="nc" id="L202"> Publisher publisher = em.find(Publisher.class, authorizedName);</span> |
| <span class="nc bnc" id="L203" title="All 2 branches missed."> if (publisher == null)</span> |
| <span class="nc" id="L204"> throw new UnknownUserException(new ErrorMessage("errors.auth.NoPublisher", authorizedName));</span> |
| <span class="nc" id="L205"> return publisher;</span> |
| } finally { |
| <span class="nc bnc" id="L207" title="All 4 branches missed."> if (tx.isActive()) {</span> |
| <span class="nc" id="L208"> tx.rollback();</span> |
| } |
| <span class="nc" id="L210"> em.close();</span> |
| } |
| } |
| } |
| </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.7.9.201702052155</span></div></body></html> |