1 /*
2 * SSHTools - Java SSH2 API
3 *
4 * Copyright (C) 2002-2003 Lee David Painter and Contributors.
5 *
6 * Contributions made by:
7 *
8 * Brett Smith
9 * Richard Pernavas
10 * Erwin Bolwidt
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 */
26 package com.sshtools.common.automate;
27
28 import com.sshtools.j2ssh.transport.publickey.InvalidSshKeyException;
29 import com.sshtools.j2ssh.transport.publickey.SshPublicKey;
30
31 import java.io.IOException;
32
33 import java.util.HashMap;
34 import java.util.Map;
35
36
37 /**
38 *
39 *
40 * @author $author$
41 * @version $Revision: 1.15 $
42 */
43 public class AuthorizedKeys {
44 private HashMap keys = new HashMap();
45
46 /**
47 *
48 *
49 * @return
50 */
51 public Map getAuthorizedKeys() {
52 return keys;
53 }
54
55 /**
56 *
57 *
58 * @param username
59 * @param key
60 */
61 public void addKey(String username, SshPublicKey key) {
62 if (!containsKey(key)) {
63 keys.put(key, username);
64 }
65 }
66
67 /**
68 *
69 *
70 * @param key
71 */
72 public void removeKey(SshPublicKey key) {
73 keys.remove(key);
74 }
75
76 /**
77 *
78 *
79 * @param key
80 *
81 * @return
82 */
83 public boolean containsKey(SshPublicKey key) {
84 return keys.containsValue(key);
85 }
86
87 /**
88 *
89 *
90 * @param formatted
91 * @param serverId
92 * @param loader
93 *
94 * @return
95 *
96 * @throws RemoteIdentificationException
97 * @throws IOException
98 * @throws InvalidSshKeyException
99 */
100 public static AuthorizedKeys parse(byte[] formatted, String serverId,
101 String hostname, AuthorizedKeysFileLoader loader)
102 throws RemoteIdentificationException, IOException,
103 InvalidSshKeyException {
104 AuthorizedKeysFormat format = RemoteIdentificationFactory.getInstance(serverId,
105 hostname).getAuthorizedKeysFormat();
106
107 if (format.requiresKeyFiles()) {
108 return format.unformat(formatted, loader);
109 } else {
110 return format.unformat(formatted);
111 }
112 }
113
114 /**
115 *
116 *
117 * @param keys
118 * @param serverId
119 * @param saver
120 *
121 * @return
122 *
123 * @throws RemoteIdentificationException
124 * @throws IOException
125 * @throws InvalidSshKeyException
126 */
127 public static byte[] create(AuthorizedKeys keys, String serverId,
128 String hostname, AuthorizedKeysFileSaver saver)
129 throws RemoteIdentificationException, IOException,
130 InvalidSshKeyException {
131 AuthorizedKeysFormat format = RemoteIdentificationFactory.getInstance(serverId,
132 hostname).getAuthorizedKeysFormat();
133
134 if (format.requiresKeyFiles()) {
135 return format.format(keys, saver);
136 } else {
137 return format.format(keys);
138 }
139 }
140 }