View Javadoc

1   /*
2    * Copyright (C) 2017-2019 Centre National d'Etudes Spatiales (CNES).
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 3.0 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17   * MA 02110-1301  USA
18   */
19  package fr.cnes.doi.plugin.impl.db;
20  
21  import fr.cnes.doi.plugin.impl.db.service.DatabaseSingleton;
22  import fr.cnes.doi.plugin.impl.db.service.DOIDbDataAccessService;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.logging.log4j.LogManager;
27  import org.apache.logging.log4j.Logger;
28  
29  import fr.cnes.doi.exception.DOIDbException;
30  import fr.cnes.doi.exception.DoiRuntimeException;
31  import fr.cnes.doi.plugin.AbstractTokenDBPluginHelper;
32  import static fr.cnes.doi.plugin.impl.db.impl.DOIDbDataAccessServiceImpl.DB_MAX_ACTIVE_CONNECTIONS;
33  import static fr.cnes.doi.plugin.impl.db.impl.DOIDbDataAccessServiceImpl.DB_MAX_IDLE_CONNECTIONS;
34  import static fr.cnes.doi.plugin.impl.db.impl.DOIDbDataAccessServiceImpl.DB_MIN_IDLE_CONNECTIONS;
35  import static fr.cnes.doi.plugin.impl.db.impl.DOIDbDataAccessServiceImpl.DB_PWD;
36  import static fr.cnes.doi.plugin.impl.db.impl.DOIDbDataAccessServiceImpl.DB_URL;
37  import static fr.cnes.doi.plugin.impl.db.impl.DOIDbDataAccessServiceImpl.DB_USER;
38  import fr.cnes.doi.utils.Utils;
39  import java.util.Map;
40  import java.util.concurrent.ConcurrentHashMap;
41  
42  /**
43   * Default implementation of the token database.
44   *
45   * @author Jean-Christophe Malapert (jean-christophe.malapert@cnes.fr)
46   */
47  public final class DefaultTokenImpl extends AbstractTokenDBPluginHelper {
48  
49      /**
50       * Plugin description.
51       */
52      private static final String DESCRIPTION = "Provides a pre-defined list of users and groups";
53      /**
54       * Plugin version.
55       */
56      private static final String VERSION = "1.0.0";
57      /**
58       * Plugin owner.
59       */
60      private static final String OWNER = "CNES";
61      /**
62       * Plugin author.
63       */
64      private static final String AUTHOR = "Jean-Christophe Malapert";
65      /**
66       * Plugin license.
67       */
68      private static final String LICENSE = "LGPLV3";
69      /**
70       * Logger.
71       */
72      private static final Logger LOG = LogManager.getLogger(DefaultTokenImpl.class.getName());
73  
74      /**
75       * Name of the class.
76       */
77      private final String NAME = this.getClass().getName();
78  
79      /**
80       * Database access.
81       */
82      private DOIDbDataAccessService das;
83  
84      /**
85       * Configuration file.
86       */
87      private Map<String, String> conf;
88  
89      /**
90       * Status of the plugin configuration.
91       */
92      private boolean configured = false;
93  
94      /**
95       * Options for JDBC.
96       */
97      private final Map<String, Integer> options = new ConcurrentHashMap<>();
98  
99      /**
100      * {@inheritDoc }
101      */
102     @Override
103     public void setConfiguration(final Object configuration) {
104         this.conf = (Map<String, String>) configuration;
105         final String dbUrl = this.conf.get(DB_URL);
106         final String dbUser = this.conf.get(DB_USER);
107         final String dbPwd = this.conf.get(DB_PWD);
108         if (this.conf.containsKey(DB_MIN_IDLE_CONNECTIONS)) {
109             this.options.put(DB_MIN_IDLE_CONNECTIONS,
110                     Integer.valueOf(this.conf.get(DB_MIN_IDLE_CONNECTIONS)));
111         }
112         if (this.conf.containsKey(DB_MAX_IDLE_CONNECTIONS)) {
113             this.options.put(DB_MAX_IDLE_CONNECTIONS,
114                     Integer.valueOf(this.conf.get(DB_MAX_IDLE_CONNECTIONS)));
115         }
116         if (this.conf.containsKey(DB_MAX_ACTIVE_CONNECTIONS)) {
117             this.options.put(DB_MAX_ACTIVE_CONNECTIONS,
118                     Integer.valueOf(this.conf.get(DB_MAX_ACTIVE_CONNECTIONS)));
119         }
120         LOG.info("[CONF] Plugin database URL : {}", dbUrl);
121         LOG.info("[CONF] Plugin database user : {}", dbUser);
122         LOG.info("[CONF] Plugin database password : {}", Utils.transformPasswordToStars(dbPwd));
123         LOG.info("[CONF] Plugin options : {}", this.options);
124         this.configured = true;
125     }
126 
127     /**
128      * {@inheritDoc }
129      */
130     @Override
131     public void initConnection() throws DoiRuntimeException {
132         DatabaseSingleton.getInstance().init(
133                 this.conf.get(DB_URL), this.conf.get(DB_USER), this.conf.get(DB_PWD), this.options);
134         this.das = DatabaseSingleton.getInstance().getDatabaseAccess();
135     }
136 
137     /**
138      * {@inheritDoc }
139      */
140     @Override
141     public boolean addToken(final String jwt) {
142 
143         boolean isAdded = false;
144         try {
145             das.addToken(jwt);
146             LOG.info("token added : {}", jwt);
147             isAdded = true;
148         } catch (DOIDbException e) {
149             LOG.fatal("The token {} cannot be saved in database", jwt, e);
150         }
151         return isAdded;
152     }
153 
154     /**
155      * {@inheritDoc }
156      */
157     @Override
158     public boolean deleteToken(final String jwt) {
159         boolean isRemoved;
160         try {
161             das.deleteToken(jwt);
162             isRemoved = true;
163             LOG.info("token deleted : {}", jwt);
164         } catch (DOIDbException e) {
165             isRemoved = false;
166             LOG.fatal("The token {} cannot be deleted in database", jwt, e);
167         }
168         return isRemoved;
169     }
170 
171     /**
172      * {@inheritDoc }
173      */
174     @Override
175     public boolean isExist(final String jwt) {
176         boolean isTokenExist = false;
177         try {
178             final List<String> tokenList = das.getTokens();
179             if (!tokenList.isEmpty()) {
180                 for (final String token : tokenList) {
181                     if (token.equals(jwt)) {
182                         isTokenExist = true;
183                     }
184                 }
185             }
186         } catch (DOIDbException e) {
187             LOG.fatal("The token {} cannot access to token database", jwt, e);
188         }
189         return isTokenExist;
190     }
191 
192     /**
193      * {@inheritDoc }
194      */
195     @Override
196     public List<String> getTokens() throws DOIDbException {
197         final List<String> tokens = new ArrayList<>();
198         tokens.addAll(das.getTokens());
199         return tokens;
200     }
201 
202     /**
203      * {@inheritDoc }
204      */
205     @Override
206     public String getName() {
207         return NAME;
208     }
209 
210     /**
211      * {@inheritDoc }
212      */
213     @Override
214     public String getDescription() {
215         return DESCRIPTION;
216     }
217 
218     /**
219      * {@inheritDoc }
220      */
221     @Override
222     public String getVersion() {
223         return VERSION;
224     }
225 
226     /**
227      * {@inheritDoc }
228      */
229     @Override
230     public String getAuthor() {
231         return AUTHOR;
232     }
233 
234     /**
235      * {@inheritDoc }
236      */
237     @Override
238     public String getOwner() {
239         return OWNER;
240     }
241 
242     /**
243      * {@inheritDoc }
244      */
245     @Override
246     public String getLicense() {
247         return LICENSE;
248     }
249 
250     /**
251      * {@inheritDoc}
252      */
253     @Override
254     public StringBuilder validate() {
255         return new StringBuilder();
256     }
257 
258     /**
259      * Checks if the keyword is a password.
260      *
261      * @param key keyword to check
262      * @return True when the keyword is a password otherwise False
263      */
264     public static boolean isPassword(final String key) {
265         return false;
266     }
267 
268     /**
269      * {@inheritDoc}
270      */
271     @Override
272     public void release() {
273         try {
274             if (this.das != null) {
275                 this.das.close();
276             }
277         } catch (DOIDbException ex) {
278         }
279         this.configured = false;
280     }
281 
282     /**
283      * {@inheritDoc}
284      */
285     @Override
286     public boolean isConfigured() {
287         return this.configured;
288     }
289 
290 }