@@ -35,13 +35,16 @@ public class KeychainLoginService implements LoginService {
3535
3636 private final HostPasswordStore keychain ;
3737
38+ public KeychainLoginService () {
39+ this (PasswordStoreFactory .get ());
40+ }
41+
3842 public KeychainLoginService (final HostPasswordStore keychain ) {
3943 this .keychain = keychain ;
4044 }
4145
4246 @ Override
43- public void validate (final Host bookmark , final String message , final LoginCallback prompt ,
44- final LoginOptions options ) throws LoginCanceledException , LoginFailureException {
47+ public void validate (final Host bookmark , final LoginCallback prompt , final LoginOptions options ) throws LoginCanceledException , LoginFailureException {
4548 if (log .isDebugEnabled ()) {
4649 log .debug (String .format ("Validate login credentials for %s" , bookmark ));
4750 }
@@ -103,10 +106,17 @@ public void validate(final Host bookmark, final String message, final LoginCallb
103106 }
104107 }
105108 if (!credentials .validate (bookmark .getProtocol (), options )) {
106- final StringAppender details = new StringAppender ();
107- details .append (message );
108- details .append (LocaleFactory .localizedString ("No login credentials could be found in the Keychain" , "Credentials" ));
109- this .prompt (bookmark , details .toString (), prompt , options );
109+ final StringAppender message = new StringAppender ();
110+ if (options .password ) {
111+ message .append (MessageFormat .format (LocaleFactory .localizedString (
112+ "Login {0} with username and password" , "Credentials" ), BookmarkNameProvider .toString (bookmark )));
113+ }
114+ if (options .publickey ) {
115+ message .append (LocaleFactory .localizedString (
116+ "Select the private key in PEM or PuTTY format" , "Credentials" ));
117+ }
118+ message .append (LocaleFactory .localizedString ("No login credentials could be found in the Keychain" , "Credentials" ));
119+ this .prompt (bookmark , message .toString (), prompt , options );
110120 }
111121 }
112122
@@ -124,19 +134,19 @@ public boolean prompt(final Host bookmark, final String message, final LoginCall
124134 final Credentials credentials = bookmark .getCredentials ();
125135 if (options .password ) {
126136 final Credentials input = prompt .prompt (bookmark , credentials .getUsername (),
127- String .format ("%s %s" , LocaleFactory .localizedString ("Login" , "Login" ), bookmark .getHostname ()),
128- message ,
129- options );
137+ String .format ("%s %s" , LocaleFactory .localizedString ("Login" , "Login" ), bookmark .getHostname ()),
138+ message ,
139+ options );
130140 credentials .setSaved (input .isSaved ());
131141 credentials .setUsername (input .getUsername ());
132142 credentials .setPassword (input .getPassword ());
133143 credentials .setIdentity (input .getIdentity ());
134144 }
135145 if (options .token ) {
136146 final Credentials input = prompt .prompt (bookmark ,
137- LocaleFactory .localizedString ("Provide additional login credentials" , "Credentials" ),
138- message ,
139- options );
147+ LocaleFactory .localizedString ("Provide additional login credentials" , "Credentials" ),
148+ message ,
149+ options );
140150 credentials .setSaved (input .isSaved ());
141151 credentials .setToken (input .getPassword ());
142152 }
@@ -154,19 +164,19 @@ public boolean authenticate(final Proxy proxy, final Session session, final Prog
154164 final Credentials credentials = bookmark .getCredentials ();
155165 if (credentials .isPasswordAuthentication ()) {
156166 listener .message (MessageFormat .format (LocaleFactory .localizedString ("Authenticating as {0}" , "Status" ),
157- credentials .getUsername ()));
167+ credentials .getUsername ()));
158168 }
159169 else if (credentials .isOAuthAuthentication ()) {
160170 listener .message (MessageFormat .format (LocaleFactory .localizedString ("Authenticating as {0}" , "Status" ),
161- credentials .getOauth ().getAccessToken ()));
171+ credentials .getOauth ().getAccessToken ()));
162172 }
163173 else if (credentials .isPublicKeyAuthentication ()) {
164174 listener .message (MessageFormat .format (LocaleFactory .localizedString ("Authenticating as {0}" , "Status" ),
165- credentials .getIdentity ().getName ()));
175+ credentials .getIdentity ().getName ()));
166176 }
167177 else if (credentials .isCertificateAuthentication ()) {
168178 listener .message (MessageFormat .format (LocaleFactory .localizedString ("Authenticating as {0}" , "Status" ),
169- credentials .getCertificate ()));
179+ credentials .getCertificate ()));
170180 }
171181 try {
172182 if (log .isDebugEnabled ()) {
@@ -177,24 +187,7 @@ else if(credentials.isCertificateAuthentication()) {
177187 log .debug (String .format ("Login successful for session %s" , session ));
178188 }
179189 listener .message (LocaleFactory .localizedString ("Login successful" , "Credentials" ));
180- if (credentials .isSaved ()) {
181- // Write credentials to keychain
182- try {
183- keychain .save (bookmark );
184- }
185- catch (LocalAccessDeniedException e ) {
186- log .error (String .format ("Failure saving credentials for %s in keychain. %s" , bookmark , e ));
187- }
188- }
189- else {
190- if (log .isInfoEnabled ()) {
191- log .info (String .format ("Skip writing credentials for bookmark %s" , bookmark .getHostname ()));
192- }
193- }
194- // Flag for successful authentication
195- credentials .setPassed (true );
196- // Nullify password and tokens
197- credentials .reset ();
190+ this .save (bookmark );
198191 return true ;
199192 }
200193 catch (LoginFailureException e ) {
@@ -213,4 +206,26 @@ else if(credentials.isCertificateAuthentication()) {
213206 throw e ;
214207 }
215208 }
209+
210+ public void save (final Host bookmark ) {
211+ final Credentials credentials = bookmark .getCredentials ();
212+ if (credentials .isSaved ()) {
213+ // Write credentials to keychain
214+ try {
215+ keychain .save (bookmark );
216+ }
217+ catch (LocalAccessDeniedException e ) {
218+ log .error (String .format ("Failure saving credentials for %s in keychain. %s" , bookmark , e ));
219+ }
220+ }
221+ else {
222+ if (log .isInfoEnabled ()) {
223+ log .info (String .format ("Skip writing credentials for bookmark %s" , bookmark .getHostname ()));
224+ }
225+ }
226+ // Flag for successful authentication
227+ credentials .setPassed (true );
228+ // Nullify password and tokens
229+ credentials .reset ();
230+ }
216231}
0 commit comments