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          // TODO @alex This is a copy of TrustApplicationUtils.addRequestParameters
40          // I would propose instead a TrustApplicationUtils.getRequestHeaders list to use here.
41          request.addHeader("X-Seraph-Trusted-App-ID", certificate.getID());
42          request.addHeader("X-Seraph-Trusted-App-Cert", certificate.getCertificate());
43          request.addHeader("X-Seraph-Trusted-App-Key", certificate.getSecretKey());
44  
45          Integer version = certificate.getProtocolVersion();
46          if(version != null) {
47              request.addHeader("X-Seraph-Trusted-App-Version", version.toString());
48          }
49  
50          request.addHeader("X-Seraph-Trusted-App-Magic", certificate.getMagicNumber());
51          if(certificate.getSignature() != null)
52          {
53              request.addHeader("X-Seraph-Trusted-App-Signature", certificate.getSignature());
54          }
55  
56          /*
57            HttpClient just calls request.addHeader() with the value returned here.
58            Rather than arbitrarily returning just one of the headers we need, returning null which is safely ignored.
59            Have raised https://issues.apache.org/jira/browse/HTTPCLIENT-1607 to change this method to return a HeaderGroup
60          */
61          return null;
62      }
63  
64      @Override
65      public void processChallenge(final Header header) throws MalformedChallengeException
66      {
67          log.warn("Ignoring a call to processChallenge as TrustedTokenScheme is intended for preemptive authentication only.");
68      }
69  
70      @Override
71      public String getSchemeName()
72      {
73          return "trustedtoken";
74      }
75  
76      @Override
77      public String getParameter(final String name)
78      {
79          throw new UnsupportedOperationException("Not implemented");
80      }
81  
82      @Override
83      public String getRealm()
84      {
85          throw new UnsupportedOperationException("Not implemented");
86      }
87  
88      @Override
89      public boolean isConnectionBased()
90      {
91          return false;
92      }
93  
94      @Override
95      public boolean isComplete()
96      {
97          return true;
98      }
99  
100     @Override
101     public Header authenticate(final Credentials credentials, final HttpRequest request) throws AuthenticationException
102     {
103         return authenticate(credentials, request, new BasicHttpContext());
104     }
105 
106 }