View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2.provider.sftp;
18  
19  import java.io.File;
20  
21  import com.jcraft.jsch.JSch;
22  import com.jcraft.jsch.JSchException;
23  
24  /**
25   * Structure for an identity based on Files.
26   *
27   * @since 2.1
28   */
29  public class IdentityInfo implements IdentityProvider {
30  
31      private final byte[] passPhrase;
32      private final File privateKey;
33      private final File publicKey;
34  
35      /**
36       * Constructs an identity info with private key.
37       * <p>
38       * The key is not passphrase protected.
39       * </p>
40       * <p>
41       * We use java.io.File because JSch cannot deal with VFS FileObjects.
42       * </p>
43       *
44       * @param privateKey The file with the private key
45       * @since 2.1
46       */
47      public IdentityInfo(final File privateKey) {
48          this(privateKey, null, null);
49      }
50  
51      /**
52       * Constructs an identity info with private key and its passphrase.
53       * <p>
54       * We use java.io.File because JSch cannot deal with VFS FileObjects.
55       * </p>
56       *
57       * @param privateKey The file with the private key
58       * @param passPhrase The passphrase to decrypt the private key (can be {@code null} if no passphrase is used)
59       * @since 2.1
60       */
61      public IdentityInfo(final File privateKey, final byte[] passPhrase) {
62          this(privateKey, null, passPhrase);
63      }
64  
65      /**
66       * Constructs an identity info with private and public key and passphrase for the private key.
67       * <p>
68       * We use java.io.File because JSch cannot deal with VFS FileObjects.
69       * </p>
70       *
71       * @param privateKey The file with the private key
72       * @param publicKey  The public key part used for connections with exchange of certificates (can be {@code null})
73       * @param passPhrase The passphrase to decrypt the private key (can be {@code null} if no passphrase is used)
74       * @since 2.1
75       */
76      public IdentityInfo(final File privateKey, final File publicKey, final byte[] passPhrase) {
77          this.privateKey = privateKey;
78          this.publicKey = publicKey;
79          this.passPhrase = passPhrase;
80      }
81  
82      /**
83       * @since 2.4
84       */
85      @Override
86      public void addIdentity(final JSch jsch) throws JSchException {
87          jsch.addIdentity(getAbsolutePath(privateKey), getAbsolutePath(publicKey), passPhrase);
88      }
89  
90      private String getAbsolutePath(final File file) {
91          return file != null ? file.getAbsolutePath() : null;
92      }
93  
94      /**
95       * Get the passphrase of the private key.
96       *
97       * @return the passphrase
98       * @since 2.1
99       */
100     public byte[] getPassPhrase() {
101         return passPhrase;
102     }
103 
104     /**
105      * Get the file with the private key.
106      *
107      * @return the file
108      * @since 2.1
109      */
110     public File getPrivateKey() {
111         return privateKey;
112     }
113 
114     /**
115      * Get the file with the public key.
116      *
117      * @return the file
118      * @since 2.1
119      */
120     public File getPublicKey() {
121         return publicKey;
122     }
123 }