View Javadoc
1   /*
2    * Copyright 2014 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.juddi.v3.auth;
17  
18  import javax.persistence.EntityManager;
19  import javax.persistence.EntityTransaction;
20  import javax.servlet.http.HttpServletRequest;
21  import javax.xml.ws.WebServiceContext;
22  import javax.xml.ws.handler.MessageContext;
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.juddi.config.AppConfig;
26  import org.apache.juddi.config.PersistenceManager;
27  import org.apache.juddi.config.Property;
28  import org.apache.juddi.model.Publisher;
29  import org.apache.juddi.model.UddiEntityPublisher;
30  import org.apache.juddi.v3.error.AuthenticationException;
31  import org.apache.juddi.v3.error.ErrorMessage;
32  import org.apache.juddi.v3.error.FatalErrorException;
33  import org.apache.juddi.v3.error.UnknownUserException;
34  
35  /**
36   * This authenticator trust's what's provided by the container, such as HTTP
37   * BASIC, DIGEST, or CLIENT CERT
38   *
39   * @author Alex O'Ree
40   */
41  public class HTTPContainerAuthenticator implements Authenticator {
42  
43          private Log log = LogFactory.getLog(this.getClass());
44          @Override
45          public String authenticate(String authorizedName, String cred) throws AuthenticationException, FatalErrorException {
46                  throw new UnknownUserException(new ErrorMessage("errros.UnsupportedAuthenticator"));
47          }
48  
49          @Override
50          public UddiEntityPublisher identify(String authInfoNotused, String authorizedNameNotused, WebServiceContext ctx) throws AuthenticationException, FatalErrorException {
51                  int MaxBindingsPerService = -1;
52                  int MaxServicesPerBusiness = -1;
53                  int MaxTmodels = -1;
54                  int MaxBusinesses = -1;
55                  try {
56                          MaxBindingsPerService = AppConfig.getConfiguration().getInt(Property.JUDDI_MAX_BINDINGS_PER_SERVICE, -1);
57                          MaxServicesPerBusiness = AppConfig.getConfiguration().getInt(Property.JUDDI_MAX_SERVICES_PER_BUSINESS, -1);
58                          MaxTmodels = AppConfig.getConfiguration().getInt(Property.JUDDI_MAX_TMODELS_PER_PUBLISHER, -1);
59                          MaxBusinesses = AppConfig.getConfiguration().getInt(Property.JUDDI_MAX_BUSINESSES_PER_PUBLISHER, -1);
60                  } catch (Exception ex) {
61                          MaxBindingsPerService = -1;
62                          MaxServicesPerBusiness = -1;
63                          MaxTmodels = -1;
64                          MaxBusinesses = -1;
65                          log.error("config exception! ", ex);
66                  }
67                  EntityManager em = PersistenceManager.getEntityManager();
68                  EntityTransaction tx = em.getTransaction();
69                  try {
70                          String user = null;
71                          if (ctx==null)
72                                  throw new UnknownUserException(new ErrorMessage("errors.auth.NoPublisher", "no web service context!"));
73                          if (ctx.getUserPrincipal() != null) {
74                                  user = ctx.getUserPrincipal().getName();
75                          }
76                          if (user == null) {
77                                  MessageContext mc = ctx.getMessageContext();
78                                  HttpServletRequest req = null;
79                                  if (mc != null) {
80                                          req = (HttpServletRequest) mc.get(MessageContext.SERVLET_REQUEST);
81                                  }
82                                  if (req != null && req.getUserPrincipal()!=null) {
83                                          user = req.getUserPrincipal().getName();
84                                  }
85                          }
86                          if (user==null || user.length()==0){
87                                  throw new UnknownUserException(new ErrorMessage("errors.auth.NoPublisher"));
88                          }
89                          tx.begin();
90                          Publisher publisher = em.find(Publisher.class, user);
91                          if (publisher == null) {
92                                  log.warn("Publisher \"" + user + "\" was not found in the database, adding the publisher in on the fly.");
93                                  publisher = new Publisher();
94                                  publisher.setAuthorizedName(user);
95                                  publisher.setIsAdmin(false);
96                                  publisher.setIsEnabled(true);
97                                  publisher.setMaxBindingsPerService(MaxBindingsPerService);
98                                  publisher.setMaxBusinesses(MaxBusinesses);
99                                  publisher.setMaxServicesPerBusiness(MaxServicesPerBusiness);
100                                 publisher.setMaxTmodels(MaxTmodels);
101                                 publisher.setPublisherName("Unknown");
102                                 em.persist(publisher);
103                                 tx.commit();
104                         }
105                         
106                         return publisher;
107                 } finally {
108                         if (tx.isActive()) {
109                                 tx.rollback();
110                         }
111                         em.close();
112                 }
113         }
114 
115 }