View Javadoc

1   package com.atlassian.sal.core.net.auth;
2   
3   import org.apache.commons.httpclient.HttpClient;
4   import org.apache.commons.httpclient.HttpMethod;
5   
6   import com.atlassian.sal.core.trusted.CertificateFactory;
7   import com.atlassian.security.auth.trustedapps.EncryptedCertificate;
8   import com.atlassian.security.auth.trustedapps.TrustedApplicationUtils;
9   
10  public class TrustedTokenAuthenticator implements HttpClientAuthenticator
11  {
12      private final EncryptedCertificate userCertificate;
13  
14      public TrustedTokenAuthenticator(String username, CertificateFactory certificateFactory)
15      {
16          if (username != null && !username.equals(""))
17          {
18              this.userCertificate = certificateFactory.createCertificate(username);
19          } else
20          {
21              this.userCertificate = null;
22          }
23      }
24  
25      /**
26       * @param httpClient The client to process
27       * @param method The method type
28       */
29      public void process(HttpClient httpClient, HttpMethod method)
30      {
31          if (userCertificate!=null && userCertificate.getID() != null && !"".equals(userCertificate.getID().trim()))
32          {
33              method.setRequestHeader(TrustedApplicationUtils.Header.Request.ID, userCertificate.getID());
34              method.setRequestHeader(TrustedApplicationUtils.Header.Request.SECRET_KEY, userCertificate.getSecretKey());
35              method.setRequestHeader(TrustedApplicationUtils.Header.Request.CERTIFICATE, userCertificate.getCertificate());
36          }
37      }
38  
39  }