View Javadoc

1   package com.atlassian.sal.core.net.auth;
2   
3   import com.atlassian.sal.core.trusted.CertificateFactory;
4   import com.atlassian.security.auth.trustedapps.EncryptedCertificate;
5   import org.apache.http.Header;
6   import org.apache.http.HttpRequest;
7   import org.apache.http.auth.AuthenticationException;
8   import org.apache.http.auth.ContextAwareAuthScheme;
9   import org.apache.http.auth.Credentials;
10  import org.apache.http.auth.MalformedChallengeException;
11  import org.apache.http.client.methods.HttpRequestWrapper;
12  import org.apache.http.protocol.BasicHttpContext;
13  import org.apache.http.protocol.HttpContext;
14  import org.slf4j.Logger;
15  import org.slf4j.LoggerFactory;
16  
17  public class TrustedTokenScheme implements ContextAwareAuthScheme
18  {
19  
20      private static final Logger log = LoggerFactory.getLogger(TrustedTokenScheme.class);
21  
22      private final CertificateFactory certificateFactory;
23  
24      public TrustedTokenScheme(final CertificateFactory certificateFactory)
25      {
26          this.certificateFactory = certificateFactory;
27      }
28  
29      @Override
30      public Header authenticate(final Credentials credentials, final HttpRequest request, final HttpContext context)
31              throws AuthenticationException
32      {
33  
34          final String username = credentials.getUserPrincipal().getName();
35          final String url = ((HttpRequestWrapper) request).getOriginal().getRequestLine().getUri();
36  
37          EncryptedCertificate certificate = certificateFactory.createCertificate(username, url);
38  
39          /*
40          // TODO @alex This is a copy of TrustApplicationUtils.addRequestParameters
41          // I would propose instead a TrustApplicationUtils.getRequestHeaders list to use here.
42          request.addHeader("X-Seraph-Trusted-App-ID", certificate.getID());
43          request.addHeader("X-Seraph-Trusted-App-Cert", certificate.getCertificate());
44          request.addHeader("X-Seraph-Trusted-App-Key", certificate.getSecretKey());
45  
46          Integer version = certificate.getProtocolVersion();
47          if(version != null) {
48              request.addHeader("X-Seraph-Trusted-App-Version", version.toString());
49          }
50  
51          request.addHeader("X-Seraph-Trusted-App-Magic", certificate.getMagicNumber());
52          if(certificate.getSignature() != null)
53          {
54              request.addHeader("X-Seraph-Trusted-App-Signature", certificate.getSignature());
55          }
56          */
57  
58          /*
59            HttpClient just calls request.addHeader() with the value returned here.
60            Rather than arbitrarily returning just one of the headers we need, returning null which is safely ignored.
61            Have raised https://issues.apache.org/jira/browse/HTTPCLIENT-1607 to change this method to return a HeaderGroup
62          */
63          return null;
64      }
65  
66      @Override
67      public void processChallenge(final Header header) throws MalformedChallengeException
68      {
69          log.warn("Ignoring a call to processChallenge as TrustedTokenScheme is intended for preemptive authentication only.");
70      }
71  
72      @Override
73      public String getSchemeName()
74      {
75          return "trustedtoken";
76      }
77  
78      @Override
79      public String getParameter(final String name)
80      {
81          throw new UnsupportedOperationException("Not implemented");
82      }
83  
84      @Override
85      public String getRealm()
86      {
87          throw new UnsupportedOperationException("Not implemented");
88      }
89  
90      @Override
91      public boolean isConnectionBased()
92      {
93          return false;
94      }
95  
96      @Override
97      public boolean isComplete()
98      {
99          return true;
100     }
101 
102     @Override
103     public Header authenticate(final Credentials credentials, final HttpRequest request) throws AuthenticationException
104     {
105         return authenticate(credentials, request, new BasicHttpContext());
106     }
107 
108 }