1 package com.atlassian.sal.core.net.auth;
2
3 import com.atlassian.security.auth.trustedapps.EncryptedCertificate;
4 import org.apache.http.Header;
5 import org.apache.http.HttpRequest;
6 import org.apache.http.auth.AuthenticationException;
7 import org.apache.http.auth.ContextAwareAuthScheme;
8 import org.apache.http.auth.Credentials;
9 import org.apache.http.auth.MalformedChallengeException;
10 import org.apache.http.protocol.BasicHttpContext;
11 import org.apache.http.protocol.HttpContext;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 import java.io.Serializable;
16
17 public class TrustedTokenScheme implements ContextAwareAuthScheme, Serializable {
18 private static final Logger log = LoggerFactory.getLogger(TrustedTokenScheme.class);
19 private final CertificateCopy certificate;
20
21 public TrustedTokenScheme(final EncryptedCertificate certificate) {
22 this.certificate = new CertificateCopy(certificate);
23 }
24
25 @Override
26 public Header authenticate(final Credentials credentials, final HttpRequest request, final HttpContext context)
27 throws AuthenticationException {
28
29
30 request.addHeader("X-Seraph-Trusted-App-ID", certificate.getID());
31 request.addHeader("X-Seraph-Trusted-App-Cert", certificate.getCertificate());
32 request.addHeader("X-Seraph-Trusted-App-Key", certificate.getSecretKey());
33
34 Integer version = certificate.getProtocolVersion();
35 if (version != null) {
36 request.addHeader("X-Seraph-Trusted-App-Version", version.toString());
37 }
38
39 request.addHeader("X-Seraph-Trusted-App-Magic", certificate.getMagicNumber());
40 if (certificate.getSignature() != null) {
41 request.addHeader("X-Seraph-Trusted-App-Signature", certificate.getSignature());
42 }
43
44
45
46
47
48
49 return null;
50 }
51
52 @Override
53 public void processChallenge(final Header header) throws MalformedChallengeException {
54 log.warn("Ignoring a call to processChallenge as TrustedTokenScheme is intended for preemptive authentication only.");
55 }
56
57 @Override
58 public String getSchemeName() {
59 return "trustedtoken";
60 }
61
62 @Override
63 public String getParameter(final String name) {
64 throw new UnsupportedOperationException("Not implemented");
65 }
66
67 @Override
68 public String getRealm() {
69 throw new UnsupportedOperationException("Not implemented");
70 }
71
72 @Override
73 public boolean isConnectionBased() {
74 return false;
75 }
76
77 @Override
78 public boolean isComplete() {
79 return true;
80 }
81
82 @Override
83 public Header authenticate(final Credentials credentials, final HttpRequest request) throws AuthenticationException {
84 return authenticate(credentials, request, new BasicHttpContext());
85 }
86
87 private static final class CertificateCopy implements EncryptedCertificate, Serializable {
88 private final String id;
89 private final String certificate;
90 private final String secretKey;
91 private final Integer protocolVersion;
92 private final String magicNumber;
93 private final String signature;
94
95 public CertificateCopy(final EncryptedCertificate cert) {
96 this.id = cert.getID();
97 this.certificate = cert.getCertificate();
98 this.secretKey = cert.getSecretKey();
99 this.protocolVersion = cert.getProtocolVersion();
100 this.magicNumber = cert.getMagicNumber();
101 this.signature = cert.getSignature();
102 }
103
104 @Override
105 public String getID() {
106 return id;
107 }
108
109 @Override
110 public String getCertificate() {
111 return certificate;
112 }
113
114 @Override
115 public String getSecretKey() {
116 return secretKey;
117 }
118
119 @Override
120 public Integer getProtocolVersion() {
121 return protocolVersion;
122 }
123
124 @Override
125 public String getMagicNumber() {
126 return magicNumber;
127 }
128
129 @Override
130 public String getSignature() {
131 return signature;
132 }
133 }
134 }