Joaquín Reñé
2025-10-07 146a0fb8b0e90f9196e569152f649baf60d6cc8f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
 * Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved.
 */
package net.curisit.securis.services;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import jakarta.annotation.security.RolesAllowed;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceException;
import jakarta.persistence.TypedQuery;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.FormParam;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.HeaderParam;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import net.curisit.integrity.commons.Utils;
import net.curisit.securis.DefaultExceptionHandler;
import net.curisit.securis.SeCurisException;
import net.curisit.securis.db.Application;
import net.curisit.securis.db.Organization;
import net.curisit.securis.db.User;
import net.curisit.securis.db.User.Rol;
import net.curisit.securis.ioc.EnsureTransaction;
import net.curisit.securis.security.BasicSecurityContext;
import net.curisit.securis.security.Securable;
import net.curisit.securis.services.exception.SeCurisServiceException;
import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes;
import net.curisit.securis.utils.CacheTTL;
import net.curisit.securis.utils.TokenHelper;
/**
 * UserResource
 * <p>
 * REST resource that manages users (CRUD + authentication helpers).
 * All endpoints are guarded and ADMIN-only unless otherwise stated.
 * <p>
 * Notes:
 * - Uses {@link BasicSecurityContext} authorization via @Securable and @RolesAllowed.
 * - Uses JPA {@link EntityManager} injected through @Context.
 * - Mutating endpoints are wrapped in @EnsureTransaction to guarantee commit/rollback.
 * - Passwords are stored as SHA-256 hashes (see {@link Utils#sha256(String)}).
 *
 * Endpoints:
 *  GET  /user/              -> list users
 *  GET  /user/{uid}         -> get user by username
 *  POST /user/              -> create user (idempotent: upsert semantics)
 *  PUT  /user/{uid}         -> update user (creates if not exists)
 *  POST /user/login         -> password authentication; returns token and basic identity
 *  POST /user/check         -> validates a token and returns token metadata
 *  GET  /user/logout        -> invalidates HTTP session (non-token based)
 *
 * Thread-safety: RequestScoped. No shared mutable state.
 *
 * @author roberto <roberto.sanchez@curisit.net>
 * Last reviewed by JRA on Oct 5, 2025.
 */
@Path("/user")
@RequestScoped
public class UserResource {
    /** Token encoder/decoder & validator. */
    @Inject TokenHelper tokenHelper;
    /** Small cache to invalidate role/org derived data after user mutations. */
    @Inject private CacheTTL cache;
    /** JPA entity manager bound to the current request context. */
    @Context EntityManager em;
    private static final Logger LOG = LogManager.getLogger(UserResource.class);
    /**
     * UserResource 
     * Default constructor for CDI. 
     */
    public UserResource() {
    }
    // ---------------------------------------------------------------------
    // Read operations
    // ---------------------------------------------------------------------
    /**
     * index
     * <p>
     * List all users.
     *
     * Security: ADMIN only.
     *
     * @return 200 OK with JSON array of {@link User}, or 200 OK with empty list.
     */
    @GET
    @Path("/")
    @Produces({ MediaType.APPLICATION_JSON })
    @Securable(roles = Rol.ADMIN)
    @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
    public Response index() {
        LOG.info("Getting users list ");
        em.clear();
        TypedQuery<User> q = em.createNamedQuery("list-users", User.class);
        List<User> list = q.getResultList();
        return Response.ok(list).build();
    }
    /**
     * get
     * <p>
     * Retrieve a single user by username.
     *
     * Security: ADMIN only.
     *
     * @param uid Username (primary key).
     * @param token Optional token header (unused here, enforced by filters).
     * @return 200 OK with user payload or 404 if not found/invalid uid.
     */
    @GET
    @Path("/{uid}")
    @Produces({ MediaType.APPLICATION_JSON })
    @Securable(roles = Rol.ADMIN)
    @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
    public Response get(@PathParam("uid") String uid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
        LOG.info("Getting user data for id: {}: ", uid);
        if (uid == null || "".equals(uid)) {
            LOG.error("User ID is mandatory");
            return Response.status(Status.NOT_FOUND).build();
        }
        em.clear();
        User lt = em.find(User.class, uid);
        if (lt == null) {
            LOG.error("User with id {} not found in DB", uid);
            return Response.status(Status.NOT_FOUND).build();
        }
        return Response.ok(lt).build();
    }
    // ---------------------------------------------------------------------
    // Create / Update / Delete
    // ---------------------------------------------------------------------
    /**
     * create
     * <p>
     * Create a new user. If the username already exists, delegates to {@link #modify(User, String, String)}
     * to behave like an upsert.
     *
     * Security: ADMIN only.
     * Transaction: yes (via @EnsureTransaction).
     *
     * @param user  Incoming user payload. Password must be non-empty (plain text).
     *              Password is SHA-256 hashed before persist.
     * @param token Security token header (unused here; enforced by filters).
     * @return 200 OK with created/updated user; 4xx on validation errors.
     */
    @POST
    @Path("/")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces({ MediaType.APPLICATION_JSON })
    @EnsureTransaction
    @Securable(roles = Rol.ADMIN)
    @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
    public Response create(User user, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
        LOG.info("Creating new user");
        User currentUser = em.find(User.class, user.getUsername());
        if (currentUser != null) {
            LOG.info("User with id {} was found in DB, we'll try to modify it", user.getUsername());
            return modify(user, user.getUsername(), token);
        }
        try {
            this.setUserOrgs(user, user.getOrgsIds(), em);
        } catch (SeCurisException e) {
            return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
        }
        try {
            this.setUserApps(user, user.getAppsIds(), em);
        } catch (SeCurisException e) {
            return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
        }
        // Password must be provided on create
        if (user.getPassword() != null && !"".equals(user.getPassword())) {
            user.setPassword(Utils.sha256(user.getPassword()));
        } else {
            return Response.status(DefaultExceptionHandler.DEFAULT_APP_ERROR_STATUS_CODE)
                    .header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "User password is mandatory")
                    .build();
        }
        user.setModificationTimestamp(new Date());
        user.setLastLogin(null);
        user.setCreationTimestamp(new Date());
        em.persist(user);
        return Response.ok(user).build();
    }
    /**
     * setUserOrgs
     * <p>
     * Resolve and set the organizations for a user from a set of IDs.
     * Validates each id exists in DB.
     *
     * @param user     Target user entity.
     * @param orgsIds  Organization ids to assign (nullable/empty allowed).
     * @param em       EntityManager.
     * @throws SeCurisException if any of the referenced organizations does not exist.
     */
    private void setUserOrgs(User user, Set<Integer> orgsIds, EntityManager em) throws SeCurisException {
        Set<Organization> orgs = null;
        if (orgsIds != null && !orgsIds.isEmpty()) {
            orgs = new HashSet<>();
            for (Integer orgId : orgsIds) {
                Organization o = em.find(Organization.class, orgId);
                if (o == null) {
                    LOG.error("User organization with id {} not found in DB", orgId);
                    throw new SeCurisException("User's organization not found with ID: " + orgId);
                }
                orgs.add(o);
            }
        }
        user.setOrganizations(orgs);
    }
    /**
     * setUserApps
     * <p>
     * Resolve and set the applications for a user from a set of IDs.
     * Validates each id exists in DB.
     *
     * @param user     Target user entity.
     * @param appsIds  Application ids to assign (nullable/empty allowed).
     * @param em       EntityManager.
     * @throws SeCurisException if any of the referenced applications does not exist.
     */
    private void setUserApps(User user, Set<Integer> appsIds, EntityManager em) throws SeCurisException {
        Set<Application> apps = null;
        if (appsIds != null && !appsIds.isEmpty()) {
            apps = new HashSet<>();
            for (Integer appId : appsIds) {
                Application o = em.find(Application.class, appId);
                if (o == null) {
                    LOG.error("User application with id {} not found in DB", appId);
                    throw new SeCurisException("User's application not found with ID: " + appId);
                }
                apps.add(o);
            }
        }
        user.setApplications(apps);
    }
    /**
     * modify
     * <p>
     * Update an existing user. If the user does not exist, delegates to {@link #create(User, String)}.
     * Password is updated only if a non-empty password is provided.
     * Organizations & applications are fully replaced with the given ids.
     *
     * Security: ADMIN only.
     * Transaction: yes (via @EnsureTransaction).
     *
     * @param user  Incoming user payload.
     * @param uid   Username (path param) to update.
     * @param token Security token header (unused here).
     * @return 200 OK with updated user; 404 if reference entities are missing.
     */
    @PUT
    @POST
    @Path("/{uid}")
    @EnsureTransaction
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces({ MediaType.APPLICATION_JSON })
    @Securable(roles = Rol.ADMIN)
    @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
    public Response modify(User user, @PathParam("uid") String uid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
        LOG.info("Modifying user with id: {}", uid);
        User currentUser = em.find(User.class, uid);
        if (currentUser == null) {
            LOG.info("User with id {} not found in DB, we'll try to create it", uid);
            return create(user, token);
        }
        try {
            this.setUserOrgs(currentUser, user.getOrgsIds(), em);
        } catch (SeCurisException e) {
            return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
        }
        try {
            this.setUserApps(currentUser, user.getAppsIds(), em);
        } catch (SeCurisException e) {
            return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
        }
        currentUser.setFirstName(user.getFirstName());
        currentUser.setLastName(user.getLastName());
        currentUser.setRoles(user.getRoles());
        currentUser.setLang(user.getLang());
        currentUser.setModificationTimestamp(new Date());
        // Optional password update
        if (user.getPassword() != null && !"".equals(user.getPassword())) {
            currentUser.setPassword(Utils.sha256(user.getPassword()));
        }
        // lastLogin can be set through API (rare), otherwise managed at login
        currentUser.setLastLogin(user.getLastLogin());
        em.persist(currentUser);
        clearUserCache(currentUser.getUsername());
        return Response.ok(currentUser).build();
    }
    /**
     * delete
     * <p>
     * Delete a user by username.
     *
     * Security: ADMIN only.
     * Transaction: yes (via @EnsureTransaction).
     *
     * @param uid Username to delete.
     * @param request Http servlet request (unused).
     * @return 200 OK on success; 404 if user does not exist.
     */
    @DELETE
    @Path("/{uid}")
    @EnsureTransaction
    @Produces({ MediaType.APPLICATION_JSON })
    @Securable(roles = Rol.ADMIN)
    @RolesAllowed(BasicSecurityContext.ROL_ADMIN)
    public Response delete(@PathParam("uid") String uid, @Context HttpServletRequest request) {
        LOG.info("Deleting app with id: {}", uid);
        User user = em.find(User.class, uid);
        if (user == null) {
            LOG.error("User with id {} can not be deleted, It was not found in DB", uid);
            return Response.status(Status.NOT_FOUND).build();
        }
        em.remove(user);
        clearUserCache(user.getUsername());
        return Response.ok(Utils.createMap("success", true, "id", uid)).build();
    }
    /**
     * clearUserCache
     * <p>
     * Helper to invalidate cached role/org projections after changes.
     *
     * @param username The user whose cache entries must be cleared.
     */
    private void clearUserCache(String username) {
        cache.remove("roles_" + username);
        cache.remove("orgs_" + username);
    }
    // ---------------------------------------------------------------------
    // Auth helpers
    // ---------------------------------------------------------------------
    /**
     * login
     * <p>
     * Validates username & password against stored SHA-256 hash. On success,
     * updates lastLogin timestamp, clears cache and returns an auth token.
     *
     * Token format: Base64("<secret> <user> <ISO8601-date>")
     * where secret = SHA-256(seed + user + date).
     *
     * @param username Plain username.
     * @param password Plain password (SHA-256 will be computed server-side).
     * @param request  Http request, used to log underlying session (not required for token flow).
     * @return 200 OK with {token, username, full_name}; 401 on invalid credentials.
     * @throws SeCurisServiceException if user is missing or password mismatch.
     */
    @POST
    @Path("/login")
    @Produces({ MediaType.APPLICATION_JSON })
    public Response login(@FormParam("username") String username, @FormParam("password") String password, @Context HttpServletRequest request) throws SeCurisServiceException {
        LOG.info("index session: " + request.getSession());
        User user = em.find(User.class, username);
        if (user == null) {
            LOG.error("Unknown username {} used in login service", username);
            throw new SeCurisServiceException(ErrorCodes.UNAUTHORIZED_ACCESS, "Wrong credentials");
        }
        String securedPassword = Utils.sha256(password);
        if (securedPassword == null || !securedPassword.equals(user.getPassword())) {
            throw new SeCurisServiceException(ErrorCodes.UNAUTHORIZED_ACCESS, "Wrong credentials");
        }
        user.setLastLogin(new Date());
        em.getTransaction().begin();
        try {
            em.persist(user);
            em.getTransaction().commit();
        } catch (PersistenceException ex) {
            LOG.error("Error updating last login date for user: {}", username);
            LOG.error(ex);
            em.getTransaction().rollback();
        }
        clearUserCache(username);
        String userFullName = String.format("%s %s", user.getFirstName(), user.getLastName() == null ? "" : user.getLastName()).trim();
        String tokenAuth = tokenHelper.generateToken(username);
        return Response.ok(Utils.createMap("success", true, "token", tokenAuth, "username", username, "full_name", userFullName)).build();
    }
    /**
     * check
     * <p>
     * Validates a token and echoes token claims (user, creation date, token string).
     * Accepts header or query param for convenience.
     *
     * @param token  Token in header {@link TokenHelper#TOKEN_HEADER_PÀRAM}, may be null.
     * @param token2 Token in query param 'token', used if header is null.
     * @return 200 OK with {valid, user, date, token} or 403 if token missing.
     */
    @POST
    @Path("/check")
    @Produces({ MediaType.APPLICATION_JSON })
    public Response check(@HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token, @QueryParam("token") String token2) {
        if (token == null) {
            token = token2;
        }
        if (token == null) {
            return Response.status(Status.FORBIDDEN).build();
        }
        LOG.info("Token : " + token);
        String user = tokenHelper.extractUserFromToken(token);
        LOG.info("Token user: " + user);
        Date date = tokenHelper.extractDateCreationFromToken(token);
        LOG.info("Token date: " + date);
        boolean valid = tokenHelper.isTokenValid(token);
        LOG.info("Is Token valid: " + valid);
        return Response.ok(Utils.createMap("valid", true, "user", user, "date", date, "token", token)).build();
    }
    /**
     * logout
     * <p>
     * Invalidates the HTTP session (useful if the UI also tracks session).
     * Note: token-based auth is stateless; tokens are not revoked here.
     *
     * @param request HttpServletRequest to invalidate session.
     * @return 200 OK always.
     */
    @GET
    @Path("/logout")
    @Produces({ MediaType.APPLICATION_JSON })
    public Response logout(@Context HttpServletRequest request) {
        request.getSession().invalidate();
        return Response.ok().build();
    }
}