001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.common.automate;
027:
028: import com.sshtools.j2ssh.transport.publickey.InvalidSshKeyException;
029: import com.sshtools.j2ssh.transport.publickey.SshPublicKey;
030:
031: import java.io.IOException;
032:
033: import java.util.HashMap;
034: import java.util.Map;
035:
036: /**
037: *
038: *
039: * @author $author$
040: * @version $Revision: 1.15 $
041: */
042: public class AuthorizedKeys {
043: private HashMap keys = new HashMap();
044:
045: /**
046: *
047: *
048: * @return
049: */
050: public Map getAuthorizedKeys() {
051: return keys;
052: }
053:
054: /**
055: *
056: *
057: * @param username
058: * @param key
059: */
060: public void addKey(String username, SshPublicKey key) {
061: if (!containsKey(key)) {
062: keys.put(key, username);
063: }
064: }
065:
066: /**
067: *
068: *
069: * @param key
070: */
071: public void removeKey(SshPublicKey key) {
072: keys.remove(key);
073: }
074:
075: /**
076: *
077: *
078: * @param key
079: *
080: * @return
081: */
082: public boolean containsKey(SshPublicKey key) {
083: return keys.containsValue(key);
084: }
085:
086: /**
087: *
088: *
089: * @param formatted
090: * @param serverId
091: * @param loader
092: *
093: * @return
094: *
095: * @throws RemoteIdentificationException
096: * @throws IOException
097: * @throws InvalidSshKeyException
098: */
099: public static AuthorizedKeys parse(byte[] formatted,
100: String serverId, String hostname,
101: AuthorizedKeysFileLoader loader)
102: throws RemoteIdentificationException, IOException,
103: InvalidSshKeyException {
104: AuthorizedKeysFormat format = RemoteIdentificationFactory
105: .getInstance(serverId, hostname)
106: .getAuthorizedKeysFormat();
107:
108: if (format.requiresKeyFiles()) {
109: return format.unformat(formatted, loader);
110: } else {
111: return format.unformat(formatted);
112: }
113: }
114:
115: /**
116: *
117: *
118: * @param keys
119: * @param serverId
120: * @param saver
121: *
122: * @return
123: *
124: * @throws RemoteIdentificationException
125: * @throws IOException
126: * @throws InvalidSshKeyException
127: */
128: public static byte[] create(AuthorizedKeys keys, String serverId,
129: String hostname, AuthorizedKeysFileSaver saver)
130: throws RemoteIdentificationException, IOException,
131: InvalidSshKeyException {
132: AuthorizedKeysFormat format = RemoteIdentificationFactory
133: .getInstance(serverId, hostname)
134: .getAuthorizedKeysFormat();
135:
136: if (format.requiresKeyFiles()) {
137: return format.format(keys, saver);
138: } else {
139: return format.format(keys);
140: }
141: }
142: }
|