View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.ftp.simpleimpl.file;
21  
22  import org.waarp.common.logging.WaarpLogger;
23  import org.waarp.common.logging.WaarpLoggerFactory;
24  
25  /**
26   * Simple Authentication based on a previously load XML file. Not to be used in
27   * production!
28   */
29  public class SimpleAuth {
30    /**
31     * Internal Logger
32     */
33    private static final WaarpLogger logger =
34        WaarpLoggerFactory.getLogger(SimpleAuth.class);
35  
36    /**
37     * User name
38     */
39    private final String user;
40  
41    /**
42     * Password
43     */
44    private final String password;
45  
46    /**
47     * Multiple accounts
48     */
49    private final String[] accounts;
50  
51    /**
52     * Is the current user an administrator (which can shutdown or change
53     * bandwidth limitation)
54     */
55    private boolean isAdmin;
56  
57    /**
58     * @param user
59     * @param password
60     * @param accounts
61     */
62    public SimpleAuth(final String user, final String password,
63                      final String[] accounts) {
64      this.user = user;
65      this.password = password;
66      this.accounts = accounts;
67    }
68  
69    /**
70     * Is the given password a valid one
71     *
72     * @param newpassword
73     *
74     * @return True if the password is valid (or any password is valid)
75     */
76    public final boolean isPasswordValid(final String newpassword) {
77      if (password == null) {
78        return true;
79      }
80      if (newpassword == null) {
81        return false;
82      }
83      return password.equals(newpassword);
84    }
85  
86    /**
87     * Is the given account a valid one
88     *
89     * @param account
90     *
91     * @return True if the account is valid (or any account is valid)
92     */
93    public final boolean isAccountValid(final String account) {
94      if (accounts == null) {
95        logger.debug("No account needed");
96        return true;
97      }
98      if (account == null) {
99        logger.info("No account given");
100       return false;
101     }
102     for (final String acct : accounts) {
103       if (acct.equals(account)) {
104         logger.debug("Account found");
105         return true;
106       }
107     }
108     logger.info("No account found");
109     return false;
110   }
111 
112   /**
113    * @param isAdmin True if the user should be an administrator
114    */
115   public final void setAdmin(final boolean isAdmin) {
116     this.isAdmin = isAdmin;
117   }
118 
119   /**
120    * @return the isAdmin
121    */
122   public final boolean isAdmin() {
123     return isAdmin;
124   }
125 
126   /**
127    * @return the user
128    */
129   public final String getUser() {
130     return user;
131   }
132 }