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 public String getVersion() {
139 return version;
140 }
141
142
143
144
145
146 public String getPusher() {
147 return pusher.getRaw();
148 }
149
150
151
152
153
154 public PushCertificateIdent getPusherIdent() {
155 return pusher;
156 }
157
158
159
160
161
162 public String getPushee() {
163 return pushee;
164 }
165
166
167
168
169
170 public String getNonce() {
171 return nonce;
172 }
173
174
175
176
177
178 public NonceStatus getNonceStatus() {
179 return nonceStatus;
180 }
181
182
183
184
185
186
187 public List<ReceiveCommand> getCommands() {
188 return commands;
189 }
190
191
192
193
194
195
196
197 public String getSignature() {
198 return signature;
199 }
200
201
202
203
204
205 public String toText() {
206 return toStringBuilder().toString();
207 }
208
209
210
211
212
213
214 public String toTextWithSignature() {
215 return toStringBuilder().append(signature).toString();
216 }
217
218 private StringBuilder toStringBuilder() {
219 StringBuilder sb = new StringBuilder()
220 .append(VERSION).append(' ').append(version).append('\n')
221 .append(PUSHER).append(' ').append(getPusher())
222 .append('\n');
223 if (pushee != null) {
224 sb.append(PUSHEE).append(' ').append(pushee).append('\n');
225 }
226 sb.append(NONCE).append(' ').append(nonce).append('\n')
227 .append('\n');
228 for (ReceiveCommand cmd : commands) {
229 sb.append(cmd.getOldId().name())
230 .append(' ').append(cmd.getNewId().name())
231 .append(' ').append(cmd.getRefName()).append('\n');
232 }
233 return sb;
234 }
235
236 @Override
237 public int hashCode() {
238 return signature.hashCode();
239 }
240
241 @Override
242 public boolean equals(Object o) {
243 if (!(o instanceof PushCertificate)) {
244 return false;
245 }
246 PushCertificate p = (PushCertificate) o;
247 return version.equals(p.version)
248 && pusher.equals(p.pusher)
249 && Objects.equals(pushee, p.pushee)
250 && nonceStatus == p.nonceStatus
251 && signature.equals(p.signature)
252 && commandsEqual(this, p);
253 }
254
255 private static boolean commandsEqual(PushCertificate c1, PushCertificate c2) {
256 if (c1.commands.size() != c2.commands.size()) {
257 return false;
258 }
259 for (int i = 0; i < c1.commands.size(); i++) {
260 ReceiveCommand cmd1 = c1.commands.get(i);
261 ReceiveCommand cmd2 = c2.commands.get(i);
262 if (!cmd1.getOldId().equals(cmd2.getOldId())
263 || !cmd1.getNewId().equals(cmd2.getNewId())
264 || !cmd1.getRefName().equals(cmd2.getRefName())) {
265 return false;
266 }
267 }
268 return true;
269 }
270
271 @Override
272 public String toString() {
273 return getClass().getSimpleName() + '['
274 + toTextWithSignature() + ']';
275 }
276 }