1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.gpg.bc.internal;
11
12 import java.net.URISyntaxException;
13 import java.nio.file.Path;
14 import java.text.MessageFormat;
15
16 import org.bouncycastle.openpgp.PGPException;
17 import org.bouncycastle.util.encoders.Hex;
18 import org.eclipse.jgit.api.errors.CanceledException;
19 import org.eclipse.jgit.errors.UnsupportedCredentialItem;
20 import org.eclipse.jgit.transport.CredentialItem.InformationalMessage;
21 import org.eclipse.jgit.transport.CredentialItem.Password;
22 import org.eclipse.jgit.transport.CredentialsProvider;
23 import org.eclipse.jgit.transport.URIish;
24
25
26
27
28
29
30
31
32 class BouncyCastleGpgKeyPassphrasePrompt implements AutoCloseable {
33
34 private Password passphrase;
35
36 private CredentialsProvider credentialsProvider;
37
38 public BouncyCastleGpgKeyPassphrasePrompt(
39 CredentialsProvider credentialsProvider) {
40 this.credentialsProvider = credentialsProvider;
41 }
42
43
44
45
46 public void clear() {
47 if (passphrase != null) {
48 passphrase.clear();
49 passphrase = null;
50 }
51 }
52
53 @Override
54 public void close() {
55 clear();
56 }
57
58 private URIish createURI(Path keyLocation) throws URISyntaxException {
59 return new URIish(keyLocation.toUri().toString());
60 }
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public char[] getPassphrase(byte[] keyFingerprint, Path keyLocation)
78 throws PGPException, CanceledException, UnsupportedCredentialItem,
79 URISyntaxException {
80 if (passphrase == null) {
81 passphrase = new Password(BCText.get().credentialPassphrase);
82 }
83
84 if (credentialsProvider == null) {
85 throw new PGPException(BCText.get().gpgNoCredentialsProvider);
86 }
87
88 if (passphrase.getValue() == null
89 && !credentialsProvider.get(createURI(keyLocation),
90 new InformationalMessage(
91 MessageFormat.format(BCText.get().gpgKeyInfo,
92 Hex.toHexString(keyFingerprint))),
93 passphrase)) {
94 throw new CanceledException(BCText.get().gpgSigningCancelled);
95 }
96 return passphrase.getValue();
97 }
98
99
100
101
102
103
104
105 public boolean hasPassphrase() {
106 return passphrase != null && passphrase.getValue() != null;
107 }
108 }