1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
44
45
46
47 public final class DefaultTokenImpl extends AbstractTokenDBPluginHelper {
48
49
50
51
52 private static final String DESCRIPTION = "Provides a pre-defined list of users and groups";
53
54
55
56 private static final String VERSION = "1.0.0";
57
58
59
60 private static final String OWNER = "CNES";
61
62
63
64 private static final String AUTHOR = "Jean-Christophe Malapert";
65
66
67
68 private static final String LICENSE = "LGPLV3";
69
70
71
72 private static final Logger LOG = LogManager.getLogger(DefaultTokenImpl.class.getName());
73
74
75
76
77 private final String NAME = this.getClass().getName();
78
79
80
81
82 private DOIDbDataAccessService das;
83
84
85
86
87 private Map<String, String> conf;
88
89
90
91
92 private boolean configured = false;
93
94
95
96
97 private final Map<String, Integer> options = new ConcurrentHashMap<>();
98
99
100
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
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
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
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
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
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
204
205 @Override
206 public String getName() {
207 return NAME;
208 }
209
210
211
212
213 @Override
214 public String getDescription() {
215 return DESCRIPTION;
216 }
217
218
219
220
221 @Override
222 public String getVersion() {
223 return VERSION;
224 }
225
226
227
228
229 @Override
230 public String getAuthor() {
231 return AUTHOR;
232 }
233
234
235
236
237 @Override
238 public String getOwner() {
239 return OWNER;
240 }
241
242
243
244
245 @Override
246 public String getLicense() {
247 return LICENSE;
248 }
249
250
251
252
253 @Override
254 public StringBuilder validate() {
255 return new StringBuilder();
256 }
257
258
259
260
261
262
263
264 public static boolean isPassword(final String key) {
265 return false;
266 }
267
268
269
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
284
285 @Override
286 public boolean isConfigured() {
287 return this.configured;
288 }
289
290 }