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