1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.client.util;
20
21 import java.net.URI;
22 import java.nio.charset.StandardCharsets;
23
24 import org.eclipse.jetty.client.HttpClient;
25 import org.eclipse.jetty.client.api.AuthenticationStore;
26 import org.eclipse.jetty.client.api.ContentResponse;
27 import org.eclipse.jetty.client.api.Request;
28 import org.eclipse.jetty.http.HttpHeader;
29 import org.eclipse.jetty.util.Attributes;
30 import org.eclipse.jetty.util.B64Code;
31
32
33
34
35
36
37
38
39 public class BasicAuthentication extends AbstractAuthentication
40 {
41 private final String user;
42 private final String password;
43
44
45
46
47
48
49
50 public BasicAuthentication(URI uri, String realm, String user, String password)
51 {
52 super(uri, realm);
53 this.user = user;
54 this.password = password;
55 }
56
57 @Override
58 public String getType()
59 {
60 return "Basic";
61 }
62
63 @Override
64 public Result authenticate(Request request, ContentResponse response, HeaderInfo headerInfo, Attributes context)
65 {
66 String value = "Basic " + B64Code.encode(user + ":" + password, StandardCharsets.ISO_8859_1);
67 return new BasicResult(headerInfo.getHeader(), value);
68 }
69
70 private class BasicResult implements Result
71 {
72 private final HttpHeader header;
73 private final String value;
74
75 public BasicResult(HttpHeader header, String value)
76 {
77 this.header = header;
78 this.value = value;
79 }
80
81 @Override
82 public URI getURI()
83 {
84 return BasicAuthentication.this.getURI();
85 }
86
87 @Override
88 public void apply(Request request)
89 {
90 request.header(header, value);
91 }
92
93 @Override
94 public String toString()
95 {
96 return String.format("Basic authentication result for %s", getURI());
97 }
98 }
99 }