-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
MyFileTokenProvider.java
45 lines (39 loc) · 1.5 KB
/
MyFileTokenProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package io.confluent.kafka.schemaregistry.client.security.bearerauth.Resources;
import io.confluent.kafka.schemaregistry.client.SchemaRegistryClientConfig;
import io.confluent.kafka.schemaregistry.client.security.bearerauth.BearerAuthCredentialProvider;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.kafka.common.config.ConfigException;
/**
* <code>MyFileTokenProvider</code> is a <code>BearerAuthCredentialProvider</code>
* Indented only for testing purpose. The <code>MyFileTokenProvider</code> can loaded using
* <code>CustomTokenCredentialProvider<code/>
*/
public class MyFileTokenProvider implements BearerAuthCredentialProvider {
private String token;
@Override
public String getBearerToken(URL url) {
// Ideally this might be fetching a cache. And cache should hold the mechanism to refresh.
return this.token;
}
@Override
public void configure(Map<String, ?> map) {
Path path = Paths.get(
(String) map.get(SchemaRegistryClientConfig.BEARER_AUTH_ISSUER_ENDPOINT_URL));
try {
this.token = Files
.lines(path, StandardCharsets.UTF_8)
.collect(Collectors.joining(System.lineSeparator()));
} catch (IOException e) {
throw new ConfigException(String.format(
"Not to read file at given location %s", path.toString()
));
}
}
}