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 package org.eclipse.jgit.transport;
45
46 import static org.eclipse.jgit.transport.PushCertificateParser.NONCE;
47 import static org.eclipse.jgit.transport.PushCertificateParser.PUSHEE;
48 import static org.eclipse.jgit.transport.PushCertificateParser.PUSHER;
49 import static org.eclipse.jgit.transport.PushCertificateParser.VERSION;
50
51 import java.text.MessageFormat;
52 import java.util.List;
53 import java.util.Objects;
54
55 import org.eclipse.jgit.internal.JGitText;
56
57
58
59
60
61
62
63
64
65 public class PushCertificate {
66
67 public enum NonceStatus {
68
69 UNSOLICITED,
70
71 BAD,
72
73 MISSING,
74
75
76
77
78 OK,
79
80 SLOP
81 }
82
83 private final String version;
84 private final PushCertificateIdent pusher;
85 private final String pushee;
86 private final String nonce;
87 private final NonceStatus nonceStatus;
88 private final List<ReceiveCommand> commands;
89 private final String signature;
90
91 PushCertificate(String version, PushCertificateIdent pusher, String pushee,
92 String nonce, NonceStatus nonceStatus, List<ReceiveCommand> commands,
93 String signature) {
94 if (version == null || version.isEmpty()) {
95 throw new IllegalArgumentException(MessageFormat.format(
96 JGitText.get().pushCertificateInvalidField, VERSION));
97 }
98 if (pusher == null) {
99 throw new IllegalArgumentException(MessageFormat.format(
100 JGitText.get().pushCertificateInvalidField, PUSHER));
101 }
102 if (nonce == null || nonce.isEmpty()) {
103 throw new IllegalArgumentException(MessageFormat.format(
104 JGitText.get().pushCertificateInvalidField, NONCE));
105 }
106 if (nonceStatus == null) {
107 throw new IllegalArgumentException(MessageFormat.format(
108 JGitText.get().pushCertificateInvalidField,
109 "nonce status"));
110 }
111 if (commands == null || commands.isEmpty()) {
112 throw new IllegalArgumentException(MessageFormat.format(
113 JGitText.get().pushCertificateInvalidField,
114 "command"));
115 }
116 if (signature == null || signature.isEmpty()) {
117 throw new IllegalArgumentException(
118 JGitText.get().pushCertificateInvalidSignature);
119 }
120 if (!signature.startsWith(PushCertificateParser.BEGIN_SIGNATURE)
121 || !signature.endsWith(PushCertificateParser.END_SIGNATURE + '\n')) {
122 throw new IllegalArgumentException(
123 JGitText.get().pushCertificateInvalidSignature);
124 }
125 this.version = version;
126 this.pusher = pusher;
127 this.pushee = pushee;
128 this.nonce = nonce;
129 this.nonceStatus = nonceStatus;
130 this.commands = commands;
131 this.signature = signature;
132 }
133
134
135
136
137
138
139
140 public String getVersion() {
141 return version;
142 }
143
144
145
146
147
148
149
150 public String getPusher() {
151 return pusher.getRaw();
152 }
153
154
155
156
157
158
159
160 public PushCertificateIdent getPusherIdent() {
161 return pusher;
162 }
163
164
165
166
167
168
169
170 public String getPushee() {
171 return pushee;
172 }
173
174
175
176
177
178
179
180 public String getNonce() {
181 return nonce;
182 }
183
184
185
186
187
188
189
190 public NonceStatus getNonceStatus() {
191 return nonceStatus;
192 }
193
194
195
196
197
198
199
200
201
202 public List<ReceiveCommand> getCommands() {
203 return commands;
204 }
205
206
207
208
209
210
211
212
213
214 public String getSignature() {
215 return signature;
216 }
217
218
219
220
221
222
223
224 public String toText() {
225 return toStringBuilder().toString();
226 }
227
228
229
230
231
232
233
234
235
236 public String toTextWithSignature() {
237 return toStringBuilder().append(signature).toString();
238 }
239
240 private StringBuilder toStringBuilder() {
241 StringBuilder sb = new StringBuilder()
242 .append(VERSION).append(' ').append(version).append('\n')
243 .append(PUSHER).append(' ').append(getPusher())
244 .append('\n');
245 if (pushee != null) {
246 sb.append(PUSHEE).append(' ').append(pushee).append('\n');
247 }
248 sb.append(NONCE).append(' ').append(nonce).append('\n')
249 .append('\n');
250 for (ReceiveCommand cmd : commands) {
251 sb.append(cmd.getOldId().name())
252 .append(' ').append(cmd.getNewId().name())
253 .append(' ').append(cmd.getRefName()).append('\n');
254 }
255 return sb;
256 }
257
258
259 @Override
260 public int hashCode() {
261 return signature.hashCode();
262 }
263
264
265 @Override
266 public boolean equals(Object o) {
267 if (!(o instanceof PushCertificate)) {
268 return false;
269 }
270 PushCertificate p = (PushCertificate) o;
271 return version.equals(p.version)
272 && pusher.equals(p.pusher)
273 && Objects.equals(pushee, p.pushee)
274 && nonceStatus == p.nonceStatus
275 && signature.equals(p.signature)
276 && commandsEqual(this, p);
277 }
278
279 private static boolean commandsEqual(PushCertificate c1, PushCertificate c2) {
280 if (c1.commands.size() != c2.commands.size()) {
281 return false;
282 }
283 for (int i = 0; i < c1.commands.size(); i++) {
284 ReceiveCommand cmd1 = c1.commands.get(i);
285 ReceiveCommand cmd2 = c2.commands.get(i);
286 if (!cmd1.getOldId().equals(cmd2.getOldId())
287 || !cmd1.getNewId().equals(cmd2.getNewId())
288 || !cmd1.getRefName().equals(cmd2.getRefName())) {
289 return false;
290 }
291 }
292 return true;
293 }
294
295
296 @Override
297 public String toString() {
298 return getClass().getSimpleName() + '['
299 + toTextWithSignature() + ']';
300 }
301 }