James Moger
2014-03-22 d500429a1d7a47da3bcd22880b53dce806ba9300
src/main/java/com/gitblit/transport/ssh/gitblit/KeysDispatcher.java
@@ -30,6 +30,8 @@
import com.gitblit.transport.ssh.commands.CommandMetaData;
import com.gitblit.transport.ssh.commands.DispatchCommand;
import com.gitblit.transport.ssh.commands.SshCommand;
import com.gitblit.utils.FlipTable;
import com.gitblit.utils.FlipTable.Borders;
/**
 * The dispatcher and it's commands for SSH public key management.
@@ -140,20 +142,36 @@
         IPublicKeyManager keyManager = getContext().getGitblit().getPublicKeyManager();
         String username = getContext().getClient().getUsername();
         List<SshKey> keys = keyManager.getKeys(username);
         if (keys == null || keys.isEmpty()) {
            stdout.println("You have not registered any public keys for ssh authentication.");
         if (showRaw) {
            asRaw(keys);
         } else {
            asTable(keys);
         }
      }
      /* output in the same format as authorized_keys */
      protected void asRaw(List<SshKey> keys) {
         if (keys == null) {
            return;
         }
         for (int i = 0; i < keys.size(); i++) {
            if (showRaw) {
               // output in the same format as authorized_keys
               stdout.println(keys.get(i).getRawData());
            } else {
               // show 1-based index numbers with the fingerprint
               // this is useful for comparing with "ssh-add -l"
               stdout.println("#" + (i + 1) + ": " + keys.get(i).getFingerprint());
            }
         for (SshKey key : keys) {
            stdout.println(key.getRawData());
         }
      }
      protected void asTable(List<SshKey> keys) {
         String[] headers = { "#", "Fingerprint", "Comment", "Type" };
         int len = keys == null ? 0 : keys.size();
         String[][] data = new String[len][];
         for (int i = 0; i < len; i++) {
            // show 1-based index numbers with the fingerprint
            // this is useful for comparing with "ssh-add -l"
            SshKey k = keys.get(i);
            data[i] = new String[] { "" + (i + 1), k.getFingerprint(), k.getComment(), k.getAlgorithm() };
         }
         stdout.println(FlipTable.of(headers, data, Borders.BODY_HCOLS));
      }
   }
}