View Javadoc
1   /*
2    * Copyright (C) 2021, Thomas Wolf <thomas.wolf@paranor.ch> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.util;
11  
12  import java.text.MessageFormat;
13  import java.util.Locale;
14  
15  import org.eclipse.jgit.internal.JGitText;
16  import org.eclipse.jgit.lib.GpgSignatureVerifier.SignatureVerification;
17  import org.eclipse.jgit.lib.GpgSignatureVerifier.TrustLevel;
18  import org.eclipse.jgit.lib.PersonIdent;
19  
20  /**
21   * Utilities for signature verification.
22   *
23   * @since 5.11
24   */
25  public final class SignatureUtils {
26  
27  	private SignatureUtils() {
28  		// No instantiation
29  	}
30  
31  	/**
32  	 * Writes information about a signature verification to a string.
33  	 *
34  	 * @param verification
35  	 *            to show
36  	 * @param creator
37  	 *            of the object verified; used for time zone information
38  	 * @param formatter
39  	 *            to use for dates
40  	 * @return a textual representation of the {@link SignatureVerification},
41  	 *         using LF as line separator
42  	 */
43  	public static String toString(SignatureVerification verification,
44  			PersonIdent creator, GitDateFormatter formatter) {
45  		StringBuilder result = new StringBuilder();
46  		// Use the creator's timezone for the signature date
47  		PersonIdent dateId = new PersonIdent(creator,
48  				verification.getCreationDate());
49  		result.append(MessageFormat.format(JGitText.get().verifySignatureMade,
50  				formatter.formatDate(dateId)));
51  		result.append('\n');
52  		result.append(MessageFormat.format(
53  				JGitText.get().verifySignatureKey,
54  				verification.getKeyFingerprint().toUpperCase(Locale.ROOT)));
55  		result.append('\n');
56  		if (!StringUtils.isEmptyOrNull(verification.getSigner())) {
57  			result.append(
58  					MessageFormat.format(JGitText.get().verifySignatureIssuer,
59  							verification.getSigner()));
60  			result.append('\n');
61  		}
62  		String msg;
63  		if (verification.getVerified()) {
64  			if (verification.isExpired()) {
65  				msg = JGitText.get().verifySignatureExpired;
66  			} else {
67  				msg = JGitText.get().verifySignatureGood;
68  			}
69  		} else {
70  			msg = JGitText.get().verifySignatureBad;
71  		}
72  		result.append(MessageFormat.format(msg, verification.getKeyUser()));
73  		if (!TrustLevel.UNKNOWN.equals(verification.getTrustLevel())) {
74  			result.append(' ' + MessageFormat
75  					.format(JGitText.get().verifySignatureTrust, verification
76  							.getTrustLevel().name().toLowerCase(Locale.ROOT)));
77  		}
78  		result.append('\n');
79  		msg = verification.getMessage();
80  		if (!StringUtils.isEmptyOrNull(msg)) {
81  			result.append(msg);
82  			result.append('\n');
83  		}
84  		return result.toString();
85  	}
86  }