1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.gbif.ws.client;
15
16 import java.nio.charset.Charset;
17 import java.nio.charset.StandardCharsets;
18 import java.util.Base64;
19
20 import feign.RequestInterceptor;
21 import feign.RequestTemplate;
22
23
24
25
26
27
28
29 public class SimpleUserAuthRequestInterceptor implements RequestInterceptor {
30
31 private final String authentication;
32 private static final Charset CHARACTER_SET = StandardCharsets.ISO_8859_1;
33
34
35
36
37
38
39 public SimpleUserAuthRequestInterceptor(final String username, final byte[] password) {
40 final byte[] prefix = (username + ":").getBytes(CHARACTER_SET);
41 final byte[] usernamePassword = new byte[prefix.length + password.length];
42
43 System.arraycopy(prefix, 0, usernamePassword, 0, prefix.length);
44 System.arraycopy(password, 0, usernamePassword, prefix.length, password.length);
45
46 authentication =
47 "Basic "
48 + new String(Base64.getEncoder().encode(usernamePassword), StandardCharsets.US_ASCII);
49 }
50
51
52
53
54
55 public SimpleUserAuthRequestInterceptor(final String username, final String password) {
56 this(username, password.getBytes(CHARACTER_SET));
57 }
58
59 @Override
60 public void apply(RequestTemplate template) {
61 template.header("Authorization", authentication);
62 }
63 }