Track the SSH public key used for a session and add a `keys which` command
| | |
| | | UserModel user = authManager.authenticate(username, key); |
| | | if (user != null) { |
| | | client.setUser(user); |
| | | client.setKey(key); |
| | | return true; |
| | | } |
| | | } |
| | |
| | | private final SocketAddress remoteAddress; |
| | | |
| | | private volatile UserModel user; |
| | | private volatile SshKey key; |
| | | private volatile String repositoryName; |
| | | |
| | | SshDaemonClient(SocketAddress peer) { |
| | |
| | | public String getRepositoryName() { |
| | | return repositoryName; |
| | | } |
| | | |
| | | public SshKey getKey() { |
| | | return key; |
| | | } |
| | | |
| | | public void setKey(SshKey key) { |
| | | this.key = key; |
| | | } |
| | | |
| | | } |
| | |
| | | msg.append(user.getDisplayName()); |
| | | msg.append(", you have successfully connected over SSH."); |
| | | msg.append(nl); |
| | | msg.append(" Interactive shells are disabled."); |
| | | msg.append(nl); |
| | | msg.append(" client: "); |
| | | msg.append(nl); |
| | | msg.append(" client: "); |
| | | msg.append(session.getClientVersion()); |
| | | msg.append(nl); |
| | | msg.append(nl); |
| | |
| | | msg.append(nl); |
| | | msg.append(nl); |
| | | |
| | | msg.append(" You may upload an SSH public key with the following syntax:"); |
| | | msg.append(nl); |
| | | msg.append(nl); |
| | | if (client.getKey() == null) { |
| | | // user has authenticated with a password |
| | | // display add public key instructions |
| | | msg.append(" You may upload an SSH public key with the following syntax:"); |
| | | msg.append(nl); |
| | | msg.append(nl); |
| | | |
| | | msg.append(String.format(" cat ~/.ssh/id_rsa.pub | ssh -l %s -p %d %s gitblit keys add -", user.username, port, hostname)); |
| | | msg.append(nl); |
| | | msg.append(nl); |
| | | msg.append(String.format(" cat ~/.ssh/id_rsa.pub | ssh -l %s -p %d %s gitblit keys add -", user.username, port, hostname)); |
| | | msg.append(nl); |
| | | msg.append(nl); |
| | | |
| | | msg.append(b2); |
| | | msg.append(nl); |
| | | msg.append(nl); |
| | | msg.append(b2); |
| | | msg.append(nl); |
| | | msg.append(nl); |
| | | } |
| | | |
| | | // display the core commands |
| | | SshCommandFactory cmdFactory = (SshCommandFactory) session.getFactoryManager().getCommandFactory(); |
| | |
| | | msg.write(' '); |
| | | msg.write(commandName); |
| | | msg.write('\n'); |
| | | msg.write(' '); |
| | | msg.write(" "); |
| | | clp.printSingleLineUsage(msg, null); |
| | | msg.write("\n\n"); |
| | | msg.write("ARGUMENTS & OPTIONS\n"); |
| | |
| | | register(user, AddKey.class); |
| | | register(user, RemoveKey.class); |
| | | register(user, ListKeys.class); |
| | | register(user, WhichKey.class); |
| | | } |
| | | |
| | | @CommandMetaData(name = "add", description = "Add an SSH public key to your account") |
| | |
| | | stdout.println(FlipTable.of(headers, data, Borders.BODY_HCOLS)); |
| | | } |
| | | } |
| | | |
| | | @CommandMetaData(name = "which", description = "Display the SSH public key used for this session") |
| | | public static class WhichKey extends SshCommand { |
| | | |
| | | @Option(name = "-L", usage = "list complete public key parameters") |
| | | private boolean showRaw; |
| | | |
| | | @Override |
| | | public void run() throws UnloggedFailure { |
| | | SshKey key = getContext().getClient().getKey(); |
| | | if (key == null) { |
| | | throw new UnloggedFailure(1, "You have not authenticated with an SSH public key."); |
| | | } |
| | | |
| | | if (showRaw) { |
| | | stdout.println(key.getRawData()); |
| | | } else { |
| | | asTable(key); |
| | | } |
| | | } |
| | | |
| | | protected void asTable(SshKey key) { |
| | | String[] headers = { "Fingerprint", "Comment", "Type" }; |
| | | Object[][] data = new Object[1][]; |
| | | data[0] = new Object[] { key.getFingerprint(), key.getComment(), key.getAlgorithm() }; |
| | | |
| | | stdout.println(FlipTable.of(headers, data, Borders.BODY_HCOLS)); |
| | | } |
| | | } |
| | | } |