View Javadoc
1   package net.trajano.wagon.git.internal;
2   
3   import org.eclipse.jgit.transport.CredentialItem;
4   import org.eclipse.jgit.transport.CredentialsProvider;
5   import org.eclipse.jgit.transport.URIish;
6   
7   /**
8    * A {@link CredentialsProvider} that takes in a passphrase.
9    */
10  public class PassphraseCredentialsProvider extends CredentialsProvider {
11      /**
12       * Passphrase.
13       */
14      private final String passphrase;
15  
16      /**
17       * Constructs the provider using a given passphrase.
18       *
19       * @param passphrase
20       *            passphrase
21       */
22      public PassphraseCredentialsProvider(final String passphrase) {
23          super();
24          this.passphrase = passphrase;
25      }
26  
27      /**
28       * {@inheritDoc}
29       */
30      @Override
31      public boolean get(final URIish uriish, final CredentialItem... items) {
32          for (final CredentialItem item : items) {
33              if (item instanceof CredentialItem.StringType) {
34                  ((CredentialItem.StringType) item).setValue(passphrase);
35                  continue;
36              }
37          }
38          return true;
39      }
40  
41      /**
42       * This provider does not interact with the user, it pulls directly from the
43       * value that was set in the constructor.
44       *
45       * @return <code>false</code>
46       */
47      @Override
48      public boolean isInteractive() {
49          return false;
50      }
51  
52      /**
53       * {@inheritDoc}
54       *
55       * @return <code>true</code> when items contains a {@link CredentialItem}
56       *         .StringType
57       */
58      @Override
59      public boolean supports(final CredentialItem... items) {
60          for (final CredentialItem item : items) {
61              if (item instanceof CredentialItem.StringType) {
62                  return true;
63              }
64          }
65          return false;
66      }
67  }