Merge pull request #64 in ITERATE/cyberduck from feature/windows/9074 to master
[cyberduck.git] / source / ch / cyberduck / core / HostPasswordStore.java
blob49be34df53469ab08d3d3ea60e6101b2d1a22867
1 package ch.cyberduck.core;
3 /*
4 * Copyright (c) 2002-2013 David Kocher. All rights reserved.
5 * http://cyberduck.ch/
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;
23 /**
24 * @version $Id$
26 public abstract class HostPasswordStore implements PasswordStore {
27 private static final Logger log = Logger.getLogger(HostPasswordStore.class);
29 /**
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");
39 return null;
41 final Credentials credentials = host.getCredentials();
42 if(StringUtils.isEmpty(credentials.getUsername())) {
43 log.warn("No username given");
44 return null;
46 String p;
47 if(credentials.isPublicKeyAuthentication()) {
48 final Local key = credentials.getIdentity();
49 p = this.getPassword(host.getHostname(), key.getAbbreviatedPath());
50 if(null == p) {
51 // Interoperability with OpenSSH (ssh, ssh-agent, ssh-add)
52 p = this.getPassword("SSH", key.getAbsolute());
54 if(null == p) {
55 // Backward compatibility
56 p = this.getPassword("SSHKeychain", key.getAbbreviatedPath());
59 else {
60 p = this.getPassword(host.getProtocol().getScheme(), host.getPort(),
61 host.getHostname(), credentials.getUsername());
63 if(null == p) {
64 if(log.isInfoEnabled()) {
65 log.info(String.format("Password not found in keychain for %s", host));
68 return p;
71 /**
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");
80 return;
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()));
87 return;
89 if(StringUtils.isEmpty(credentials.getUsername())) {
90 log.warn(String.format("No username in credentials for host %s", host.getHostname()));
91 return;
93 if(StringUtils.isEmpty(credentials.getPassword())) {
94 log.warn(String.format("No password in credentials for host %s", host.getHostname()));
95 return;
97 if(credentials.isAnonymousLogin()) {
98 if(log.isInfoEnabled()) {
99 log.info(String.format("Do not write anonymous credentials for host %s", host.getHostname()));
101 return;
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());
110 else {
111 this.addPassword(host.getProtocol().getScheme(), host.getPort(),
112 host.getHostname(), credentials.getUsername(), credentials.getPassword());