View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.kuali.rice.ksb.security.httpinvoker;
17  
18  import org.apache.commons.codec.binary.Base64;
19  import org.apache.http.client.HttpClient;
20  import org.apache.http.client.methods.HttpPost;
21  import org.kuali.rice.core.api.security.credentials.CredentialsSource;
22  import org.kuali.rice.ksb.api.bus.ServiceConfiguration;
23  import org.kuali.rice.ksb.messaging.KSBHttpInvokerRequestExecutor;
24  import org.kuali.rice.ksb.security.credentials.UsernamePasswordCredentials;
25  import org.springframework.remoting.httpinvoker.HttpInvokerClientConfiguration;
26  import org.springframework.util.Assert;
27  
28  import java.io.ByteArrayOutputStream;
29  import java.io.IOException;
30  
31  
32  /**
33   * Extension to {@link KSBHttpInvokerRequestExecutor} that retrieves
34   * credentials from the CredentialsSource and places them in a BASIC HTTP
35   * Authorization header.
36   * 
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   * @since 0.9
39   */
40  public final class AuthenticationCommonsHttpInvokerRequestExecutor extends
41      KSBHttpInvokerRequestExecutor {
42  
43      /**
44       * Source of the credentials to pass via BASIC AUTH.
45       */
46      private final CredentialsSource credentialsSource;
47  
48      /**
49       * Details about the service that the CredentialsSource may need.
50       */
51      private final ServiceConfiguration serviceConfiguration;
52  
53      /**
54       * Constructor that accepts the CredentialsSource and Service Info.
55       *
56       * @param httpClient the http client
57       * @param credentialsSource the source of credentials.
58       * @param serviceConfiguration the service configuration.
59       */
60      public AuthenticationCommonsHttpInvokerRequestExecutor(final HttpClient httpClient,
61          final CredentialsSource credentialsSource, final ServiceConfiguration serviceConfiguration) {
62          super(httpClient);
63          Assert.notNull(credentialsSource, "credentialsSource cannot be null.");
64          Assert.notNull(serviceConfiguration, "serviceConfiguration cannot be null.");
65          this.credentialsSource = credentialsSource;
66          this.serviceConfiguration = serviceConfiguration;
67      }
68  
69      /**
70       * Overridden to obtain the Credentials from the CredentialsSource and pass
71       * them via HTTP BASIC Authorization.
72       */
73  
74      protected void setRequestBody(final HttpInvokerClientConfiguration config,
75          final HttpPost httpPost, final ByteArrayOutputStream baos) throws IOException {
76      	final UsernamePasswordCredentials credentials = (UsernamePasswordCredentials) this.credentialsSource.getCredentials(this.serviceConfiguration.getEndpointUrl().toExternalForm());
77  
78          final String base64 = credentials.getUsername() + ":"
79          + credentials.getPassword();
80          httpPost.addHeader("Authorization", "Basic " + new String(Base64.encodeBase64(base64.getBytes())));
81  
82          if (logger.isDebugEnabled()) {
83              logger
84                  .debug("HttpInvocation now presenting via BASIC authentication CredentialsSource-derived: "
85                      + credentials.getUsername());
86          }
87          
88          super.setRequestBody(config, httpPost, baos);
89      }
90  }