2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.ftpupload.internal.ftp;
15 import java.util.HashMap;
18 import org.apache.ftpserver.ftplet.Authentication;
19 import org.apache.ftpserver.ftplet.AuthenticationFailedException;
20 import org.apache.ftpserver.ftplet.FtpException;
21 import org.apache.ftpserver.ftplet.User;
22 import org.apache.ftpserver.ftplet.UserManager;
23 import org.apache.ftpserver.usermanager.UsernamePasswordAuthentication;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Simple FTP user manager implementation.
31 * @author Pauli Anttila - Initial contribution
33 public class FTPUserManager implements UserManager {
34 private final Logger logger = LoggerFactory.getLogger(FTPUserManager.class);
36 private int idleTimeout;
37 private Map<String, UsernamePassword> authenticationData = new HashMap<>();
40 public User authenticate(final Authentication inAuth) throws AuthenticationFailedException {
41 logger.trace("authenticate: {}", inAuth);
43 UsernamePasswordAuthentication upa = (UsernamePasswordAuthentication) inAuth;
44 String login = upa.getUsername();
45 String password = upa.getPassword();
47 if (!autheticate(login, password)) {
48 throw new AuthenticationFailedException();
50 return new FTPUser(login, idleTimeout);
53 private boolean autheticate(String login, String password) {
54 boolean result = false;
56 if (login != null && password != null) {
57 UsernamePassword credential = authenticationData.get(login);
59 if (credential != null) {
60 if (login.equals(credential.getUsername()) && password.equals(credential.getPassword())) {
68 public void setIdleTimeout(int idleTimeout) {
69 this.idleTimeout = idleTimeout;
72 public synchronized void addAuthenticationCredentials(String username, String password)
73 throws IllegalArgumentException {
74 if (authenticationData.containsKey(username)) {
75 throw new IllegalArgumentException("Credentials for user '" + username + "' already exists!");
77 authenticationData.put(username, new UsernamePassword(username, password));
80 public synchronized void removeAuthenticationCredentials(String username) {
81 authenticationData.remove(username);
85 public User getUserByName(final String login) throws FtpException {
86 logger.trace("getUserByName: {}", login);
87 return new FTPUser(login, idleTimeout);
91 public void delete(String arg0) throws FtpException {
92 logger.trace("delete: {}", arg0);
96 public boolean doesExist(String arg0) throws FtpException {
97 logger.trace("doesExist: {}", arg0);
102 public String getAdminName() throws FtpException {
103 logger.trace("getAdminName");
108 public String[] getAllUserNames() throws FtpException {
109 logger.trace("getAllUserNames");
114 public boolean isAdmin(String arg0) throws FtpException {
115 logger.trace("isAdmin: {}", arg0);
120 public void save(User arg0) throws FtpException {
121 logger.trace("save: {}", arg0);