View Javadoc

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