-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileCredentialRevocationStore.java
More file actions
99 lines (89 loc) · 3.1 KB
/
FileCredentialRevocationStore.java
File metadata and controls
99 lines (89 loc) · 3.1 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package dev.arcp.runtime.credentials;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import dev.arcp.core.credentials.CredentialId;
import dev.arcp.core.wire.ArcpMapper;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public final class FileCredentialRevocationStore implements CredentialRevocationStore {
private final Path path;
private final ObjectMapper mapper;
private final Map<CredentialId, String> outstanding = new LinkedHashMap<>();
public FileCredentialRevocationStore(Path path) {
this(path, ArcpMapper.shared());
}
public FileCredentialRevocationStore(Path path, ObjectMapper mapper) {
this.path = path;
this.mapper = mapper;
load();
}
@Override
public synchronized void record(CredentialId id, String providerHandle) {
outstanding.put(id, providerHandle);
append("record", id, providerHandle);
}
@Override
public synchronized void markRevoked(CredentialId id) {
outstanding.remove(id);
append("revoke", id, "");
}
@Override
public synchronized List<Outstanding> outstanding() {
return outstanding.entrySet().stream()
.map(entry -> new Outstanding(entry.getKey(), entry.getValue()))
.toList();
}
private void load() {
try {
Path parent = path.getParent();
if (parent != null) {
Files.createDirectories(parent);
}
if (!Files.exists(path)) {
Files.createFile(path);
return;
}
try (BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
String line;
while ((line = reader.readLine()) != null) {
if (!line.isBlank()) {
apply(mapper.readTree(line));
}
}
}
} catch (IOException e) {
throw new IllegalStateException("could not load credential revocation store " + path, e);
}
}
private void apply(JsonNode event) {
CredentialId id = CredentialId.of(event.path("id").asText());
if ("record".equals(event.path("op").asText())) {
outstanding.put(id, event.path("provider_handle").asText());
} else if ("revoke".equals(event.path("op").asText())) {
outstanding.remove(id);
}
}
private void append(String op, CredentialId id, String providerHandle) {
ObjectNode event = mapper.createObjectNode().put("op", op).put("id", id.value());
if (!providerHandle.isEmpty()) {
event.put("provider_handle", providerHandle);
}
try (BufferedWriter writer =
Files.newBufferedWriter(
path, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND)) {
writer.write(mapper.writeValueAsString(event));
writer.newLine();
} catch (IOException e) {
throw new IllegalStateException("could not append credential revocation event", e);
}
}
}