1 package ch
.cyberduck
.core
;
4 * Copyright (c) 2002-2013 David Kocher. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * Bug fixes, suggestions and comments should be sent to feedback@cyberduck.ch
20 import org
.apache
.commons
.lang3
.StringUtils
;
21 import org
.apache
.log4j
.Logger
;
26 public abstract class HostPasswordStore
implements PasswordStore
{
27 private static final Logger log
= Logger
.getLogger(HostPasswordStore
.class);
30 * @param host Hostname
31 * @return the password fetched from the keychain or null if it was not found
33 public String
find(final Host host
) {
34 if(log
.isInfoEnabled()) {
35 log
.info(String
.format("Fetching password from keychain for %s", host
));
37 if(StringUtils
.isEmpty(host
.getHostname())) {
38 log
.warn("No hostname given");
41 final Credentials credentials
= host
.getCredentials();
42 if(StringUtils
.isEmpty(credentials
.getUsername())) {
43 log
.warn("No username given");
47 if(credentials
.isPublicKeyAuthentication()) {
48 final Local key
= credentials
.getIdentity();
49 p
= this.getPassword(host
.getHostname(), key
.getAbbreviatedPath());
51 // Interoperability with OpenSSH (ssh, ssh-agent, ssh-add)
52 p
= this.getPassword("SSH", key
.getAbsolute());
55 // Backward compatibility
56 p
= this.getPassword("SSHKeychain", key
.getAbbreviatedPath());
60 p
= this.getPassword(host
.getProtocol().getScheme(), host
.getPort(),
61 host
.getHostname(), credentials
.getUsername());
64 if(log
.isInfoEnabled()) {
65 log
.info(String
.format("Password not found in keychain for %s", host
));
72 * Adds the password to the login keychain
74 * @param host Hostname
75 * @see ch.cyberduck.core.Host#getCredentials()
77 public void save(final Host host
) {
78 if(StringUtils
.isEmpty(host
.getHostname())) {
79 log
.warn("No hostname given");
82 final Credentials credentials
= host
.getCredentials();
83 if(!credentials
.isSaved()) {
84 if(log
.isInfoEnabled()) {
85 log
.info(String
.format("Skip writing credentials for host %s", host
.getHostname()));
89 if(StringUtils
.isEmpty(credentials
.getUsername())) {
90 log
.warn(String
.format("No username in credentials for host %s", host
.getHostname()));
93 if(StringUtils
.isEmpty(credentials
.getPassword())) {
94 log
.warn(String
.format("No password in credentials for host %s", host
.getHostname()));
97 if(credentials
.isAnonymousLogin()) {
98 if(log
.isInfoEnabled()) {
99 log
.info(String
.format("Do not write anonymous credentials for host %s", host
.getHostname()));
103 if(log
.isInfoEnabled()) {
104 log
.info(String
.format("Add password for host %s", host
));
106 if(credentials
.isPublicKeyAuthentication()) {
107 this.addPassword(host
.getHostname(), credentials
.getIdentity().getAbbreviatedPath(),
108 credentials
.getPassword());
111 this.addPassword(host
.getProtocol().getScheme(), host
.getPort(),
112 host
.getHostname(), credentials
.getUsername(), credentials
.getPassword());