1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.internal.transport.sshd;
11
12 import static org.apache.sshd.core.CoreModuleProperties.PASSWORD_PROMPTS;
13
14 import org.apache.sshd.client.auth.keyboard.UserInteraction;
15 import org.apache.sshd.client.auth.password.UserAuthPassword;
16 import org.apache.sshd.client.session.ClientSession;
17
18
19
20
21
22
23 public class JGitPasswordAuthentication extends UserAuthPassword {
24
25 private int maxAttempts;
26
27 private int attempts;
28
29 @Override
30 public void init(ClientSession session, String service) throws Exception {
31 super.init(session, service);
32 maxAttempts = Math.max(1,
33 PASSWORD_PROMPTS.getRequired(session).intValue());
34 attempts = 0;
35 }
36
37 @Override
38 protected boolean sendAuthDataRequest(ClientSession session, String service)
39 throws Exception {
40 if (++attempts > maxAttempts) {
41 return false;
42 }
43 UserInteraction interaction = session.getUserInteraction();
44 if (!interaction.isInteractionAllowed(session)) {
45 return false;
46 }
47 String password = getPassword(session, interaction);
48 if (password == null) {
49 throw new AuthenticationCanceledException();
50 }
51
52
53 sendPassword(null, session, password, password);
54 return true;
55 }
56
57 private String getPassword(ClientSession session,
58 UserInteraction interaction) {
59 String[] results = interaction.interactive(session, null, null, "",
60 new String[] { SshdText.get().passwordPrompt },
61 new boolean[] { false });
62 return (results == null || results.length == 0) ? null : results[0];
63 }
64 }