-
-
Notifications
You must be signed in to change notification settings - Fork 598
/
Copy pathUserResource.java
671 lines (645 loc) · 33.2 KB
/
UserResource.java
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/*
* This file is part of Dependency-Track.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* Copyright (c) Steve Springett. All Rights Reserved.
*/
package org.dependencytrack.resources.v1;
import alpine.Config;
import alpine.common.logging.Logger;
import alpine.model.LdapUser;
import alpine.model.ManagedUser;
import alpine.model.OidcUser;
import alpine.model.Permission;
import alpine.model.Team;
import alpine.model.UserPrincipal;
import alpine.security.crypto.KeyManager;
import alpine.server.auth.AlpineAuthenticationException;
import alpine.server.auth.AuthenticationNotRequired;
import alpine.server.auth.Authenticator;
import alpine.server.auth.JsonWebToken;
import alpine.server.auth.OidcAuthenticationService;
import alpine.server.auth.PasswordService;
import alpine.server.auth.PermissionRequired;
import alpine.server.resources.AlpineResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.Authorization;
import io.swagger.annotations.ResponseHeader;
import org.apache.commons.lang3.StringUtils;
import org.dependencytrack.auth.Permissions;
import org.dependencytrack.model.IdentifiableObject;
import org.dependencytrack.persistence.QueryManager;
import org.owasp.security.logging.SecurityMarkers;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.security.Principal;
import java.util.List;
/**
* JAX-RS resources for processing users.
*
* @author Steve Springett
* @since 3.0.0
*/
@Path("/v1/user")
@Api(value = "user", authorizations = @Authorization(value = "X-Api-Key"))
public class UserResource extends AlpineResource {
private static final Logger LOGGER = Logger.getLogger(UserResource.class);
@POST
@Path("login")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_PLAIN)
@ApiOperation(
value = "Assert login credentials",
notes = "Upon a successful login, a JSON Web Token will be returned in the response body. This functionality requires authentication to be enabled.",
response = String.class
)
@ApiResponses(value = {
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Forbidden")
})
@AuthenticationNotRequired
public Response validateCredentials(@FormParam("username") String username, @FormParam("password") String password) {
final Authenticator auth = new Authenticator(username, password);
try (QueryManager qm = new QueryManager()) {
final Principal principal = auth.authenticate();
super.logSecurityEvent(LOGGER, SecurityMarkers.SECURITY_SUCCESS, "Successful user login / username: " + username);
final List<Permission> permissions = qm.getEffectivePermissions((UserPrincipal) principal);
final KeyManager km = KeyManager.getInstance();
final JsonWebToken jwt = new JsonWebToken(km.getSecretKey());
final String token = jwt.createToken(principal, permissions);
return Response.ok(token).build();
} catch (AlpineAuthenticationException e) {
if (AlpineAuthenticationException.CauseType.SUSPENDED == e.getCauseType() || AlpineAuthenticationException.CauseType.UNMAPPED_ACCOUNT == e.getCauseType()) {
super.logSecurityEvent(LOGGER, SecurityMarkers.SECURITY_FAILURE, "Unauthorized login attempt / account is suspended / username: " + username);
return Response.status(Response.Status.FORBIDDEN).entity(e.getCauseType().name()).build();
} else {
super.logSecurityEvent(LOGGER, SecurityMarkers.SECURITY_FAILURE, "Unauthorized login attempt / invalid credentials / username: " + username);
return Response.status(Response.Status.UNAUTHORIZED).entity(e.getCauseType().name()).build();
}
}
}
/**
* @since 4.0.0
*/
@POST
@Path("oidc/login")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_PLAIN)
@ApiOperation(
value = "Login with OpenID Connect",
notes = "Upon a successful login, a JSON Web Token will be returned in the response body. This functionality requires authentication to be enabled.",
response = String.class
)
@ApiResponses(value = {
@ApiResponse(code = 204, message = "No Content"),
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Forbidden")
})
@AuthenticationNotRequired
public Response validateOidcAccessToken(@ApiParam(value = "An OAuth2 access token", required = true)
@FormParam("idToken") final String idToken,
@FormParam("accessToken") final String accessToken) {
final OidcAuthenticationService authService = new OidcAuthenticationService(idToken, accessToken);
if (!authService.isSpecified()) {
super.logSecurityEvent(LOGGER, SecurityMarkers.SECURITY_AUDIT, "An OpenID Connect login attempt was made, but OIDC is disabled or not properly configured");
return Response.status(Response.Status.NO_CONTENT).build();
}
try (final QueryManager qm = new QueryManager()) {
final Principal principal = authService.authenticate();
super.logSecurityEvent(LOGGER, SecurityMarkers.SECURITY_SUCCESS, "Successful OpenID Connect login / username: " + principal.getName());
final List<Permission> permissions = qm.getEffectivePermissions((UserPrincipal) principal);
final KeyManager km = KeyManager.getInstance();
final JsonWebToken jwt = new JsonWebToken(km.getSecretKey());
final String token = jwt.createToken(principal, permissions);
return Response.ok(token).build();
} catch (AlpineAuthenticationException e) {
super.logSecurityEvent(LOGGER, SecurityMarkers.SECURITY_FAILURE, "Unauthorized OpenID Connect login attempt");
if (AlpineAuthenticationException.CauseType.SUSPENDED == e.getCauseType() || AlpineAuthenticationException.CauseType.UNMAPPED_ACCOUNT == e.getCauseType()) {
return Response.status(Response.Status.FORBIDDEN).entity(e.getCauseType().name()).build();
} else {
return Response.status(Response.Status.UNAUTHORIZED).entity(e.getCauseType().name()).build();
}
}
}
@POST
@Path("forceChangePassword")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_PLAIN)
@ApiOperation(
value = "Asserts login credentials and upon successful authentication, verifies passwords match and changes users password",
notes = "Upon a successful login, a JSON Web Token will be returned in the response body. This functionality requires authentication to be enabled.",
response = String.class
)
@ApiResponses(value = {
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 403, message = "Forbidden")
})
@AuthenticationNotRequired
public Response forceChangePassword(@FormParam("username") String username, @FormParam("password") String password,
@FormParam("newPassword") String newPassword, @FormParam("confirmPassword") String confirmPassword) {
final Authenticator auth = new Authenticator(username, password);
Principal principal;
try (QueryManager qm = new QueryManager()) {
try {
principal = auth.authenticate();
} catch (AlpineAuthenticationException e) {
if (AlpineAuthenticationException.CauseType.FORCE_PASSWORD_CHANGE == e.getCauseType()) {
principal = e.getPrincipal();
} else {
throw new AlpineAuthenticationException(e.getCauseType());
}
}
if (principal instanceof ManagedUser) {
final ManagedUser user = qm.getManagedUser(((ManagedUser) principal).getUsername());
if (StringUtils.isNotBlank(newPassword) && StringUtils.isNotBlank(confirmPassword) && newPassword.equals(confirmPassword)) {
if (PasswordService.matches(newPassword.toCharArray(), user)) {
super.logSecurityEvent(LOGGER, SecurityMarkers.SECURITY_FAILURE, "Existing password is the same as new password. Password not changed. / username: " + username);
return Response.status(Response.Status.NOT_ACCEPTABLE).entity("Existing password is the same as new password. Password not changed.").build();
} else {
user.setPassword(String.valueOf(PasswordService.createHash(newPassword.toCharArray())));
user.setForcePasswordChange(false);
qm.updateManagedUser(user);
super.logSecurityEvent(LOGGER, SecurityMarkers.SECURITY_AUDIT, "Password successfully changed / username: " + username);
return Response.ok().build();
}
} else {
super.logSecurityEvent(LOGGER, SecurityMarkers.SECURITY_FAILURE, "The passwords do not match. Password not changed. / username: " + username);
return Response.status(Response.Status.NOT_ACCEPTABLE).entity("The passwords do not match. Password not changed.").build();
}
} else {
super.logSecurityEvent(LOGGER, SecurityMarkers.SECURITY_FAILURE, "Changing passwords for non-managed users is not forbidden. Password not changed. / username: " + username);
return Response.status(Response.Status.NOT_ACCEPTABLE).entity("Changing passwords for non-managed users is not forbidden. Password not changed.").build();
}
} catch (AlpineAuthenticationException e) {
if (AlpineAuthenticationException.CauseType.SUSPENDED == e.getCauseType() || AlpineAuthenticationException.CauseType.UNMAPPED_ACCOUNT == e.getCauseType()) {
super.logSecurityEvent(LOGGER, SecurityMarkers.SECURITY_FAILURE, "Unauthorized login attempt / account is suspended / username: " + username);
return Response.status(Response.Status.FORBIDDEN).entity(e.getCauseType().name()).build();
} else {
super.logSecurityEvent(LOGGER, SecurityMarkers.SECURITY_FAILURE, "Unauthorized login attempt / invalid credentials / username: " + username);
return Response.status(Response.Status.UNAUTHORIZED).entity(e.getCauseType().name()).build();
}
}
}
@GET
@Path("managed")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Returns a list of all managed users",
response = ManagedUser.class,
responseContainer = "List",
responseHeaders = @ResponseHeader(name = TOTAL_COUNT_HEADER, response = Long.class, description = "The total number of managed users")
)
@ApiResponses(value = {
@ApiResponse(code = 401, message = "Unauthorized")
})
@PermissionRequired(Permissions.Constants.ACCESS_MANAGEMENT)
public Response getManagedUsers() {
try (QueryManager qm = new QueryManager(getAlpineRequest())) {
final long totalCount = qm.getCount(ManagedUser.class);
final List<ManagedUser> users = qm.getManagedUsers();
return Response.ok(users).header(TOTAL_COUNT_HEADER, totalCount).build();
}
}
@GET
@Path("ldap")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Returns a list of all LDAP users",
response = LdapUser.class,
responseContainer = "List",
responseHeaders = @ResponseHeader(name = TOTAL_COUNT_HEADER, response = Long.class, description = "The total number of LDAP users")
)
@ApiResponses(value = {
@ApiResponse(code = 401, message = "Unauthorized")
})
@PermissionRequired(Permissions.Constants.ACCESS_MANAGEMENT)
public Response getLdapUsers() {
try (QueryManager qm = new QueryManager(getAlpineRequest())) {
final long totalCount = qm.getCount(LdapUser.class);
final List<LdapUser> users = qm.getLdapUsers();
return Response.ok(users).header(TOTAL_COUNT_HEADER, totalCount).build();
}
}
/**
* @since 4.0.0
*/
@GET
@Path("oidc")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Returns a list of all OIDC users",
response = OidcUser.class,
responseContainer = "List",
responseHeaders = @ResponseHeader(name = TOTAL_COUNT_HEADER, response = Long.class, description = "The total number of OIDC users")
)
@ApiResponses(value = {
@ApiResponse(code = 401, message = "Unauthorized")
})
@PermissionRequired(Permissions.Constants.ACCESS_MANAGEMENT)
public Response getOidcUsers() {
try (QueryManager qm = new QueryManager(getAlpineRequest())) {
final long totalCount = qm.getCount(OidcUser.class);
final List<OidcUser> users = qm.getOidcUsers();
return Response.ok(users).header(TOTAL_COUNT_HEADER, totalCount).build();
}
}
@GET
@Path("self")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Returns information about the current logged in user.",
response = UserPrincipal.class
)
@ApiResponses(value = {
@ApiResponse(code = 401, message = "Unauthorized")
})
public Response getSelf() {
if (Config.getInstance().getPropertyAsBoolean(Config.AlpineKey.ENFORCE_AUTHENTICATION)) {
try (QueryManager qm = new QueryManager()) {
if (super.isLdapUser()) {
final LdapUser user = qm.getLdapUser(getPrincipal().getName());
return Response.ok(user).build();
} else if (super.isManagedUser()) {
final ManagedUser user = qm.getManagedUser(getPrincipal().getName());
return Response.ok(user).build();
} else if (super.isOidcUser()) {
final OidcUser user = qm.getOidcUser(getPrincipal().getName());
return Response.ok(user).build();
}
return Response.status(401).build();
}
}
// Authentication is not enabled, but we need to return a positive response without any principal data.
return Response.ok().build();
}
@POST
@Path("self")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Updates information about the current logged in user.",
response = UserPrincipal.class
)
@ApiResponses(value = {
@ApiResponse(code = 400, message = "An invalid payload was submitted or the user is not a managed user."),
@ApiResponse(code = 401, message = "Unauthorized")
})
public Response updateSelf(ManagedUser jsonUser) {
if (Config.getInstance().getPropertyAsBoolean(Config.AlpineKey.ENFORCE_AUTHENTICATION)) {
try (QueryManager qm = new QueryManager()) {
if (super.isLdapUser()) {
final LdapUser user = qm.getLdapUser(getPrincipal().getName());
return Response.status(Response.Status.BAD_REQUEST).entity(user).build();
} else if (super.isOidcUser()) {
final OidcUser user = qm.getOidcUser(getPrincipal().getName());
return Response.status(Response.Status.BAD_REQUEST).entity(user).build();
} else if (super.isManagedUser()) {
final ManagedUser user = (ManagedUser) super.getPrincipal();
if (StringUtils.isBlank(jsonUser.getFullname())) {
return Response.status(Response.Status.BAD_REQUEST).entity("Full name is required.").build();
}
if (StringUtils.isBlank(jsonUser.getEmail())) {
return Response.status(Response.Status.BAD_REQUEST).entity("Email address is required.").build();
}
user.setFullname(StringUtils.trimToNull(jsonUser.getFullname()));
user.setEmail(StringUtils.trimToNull(jsonUser.getEmail()));
if (StringUtils.isNotBlank(jsonUser.getNewPassword()) && StringUtils.isNotBlank(jsonUser.getConfirmPassword())) {
if (jsonUser.getNewPassword().equals(jsonUser.getConfirmPassword())) {
user.setPassword(String.valueOf(PasswordService.createHash(jsonUser.getNewPassword().toCharArray())));
} else {
return Response.status(Response.Status.BAD_REQUEST).entity("Passwords do not match.").build();
}
}
qm.updateManagedUser(user);
super.logSecurityEvent(LOGGER, SecurityMarkers.SECURITY_AUDIT, "User profile updated: " + user.getUsername());
return Response.ok(user).build();
}
return Response.status(Response.Status.UNAUTHORIZED).build();
}
}
// Authentication is not enabled, but we need to return a positive response without any principal data.
return Response.ok().build();
}
@PUT
@Path("ldap")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Creates a new user that references an existing LDAP object.",
response = LdapUser.class,
code = 201
)
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Username cannot be null or blank."),
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 409, message = "A user with the same username already exists. Cannot create new user")
})
@PermissionRequired(Permissions.Constants.ACCESS_MANAGEMENT)
public Response createLdapUser(LdapUser jsonUser) {
try (QueryManager qm = new QueryManager()) {
if (StringUtils.isBlank(jsonUser.getUsername())) {
return Response.status(Response.Status.BAD_REQUEST).entity("Username cannot be null or blank.").build();
}
LdapUser user = qm.getLdapUser(jsonUser.getUsername());
if (user == null) {
user = qm.createLdapUser(jsonUser.getUsername());
super.logSecurityEvent(LOGGER, SecurityMarkers.SECURITY_AUDIT, "LDAP user created: " + jsonUser.getUsername());
return Response.status(Response.Status.CREATED).entity(user).build();
} else {
return Response.status(Response.Status.CONFLICT).entity("A user with the same username already exists. Cannot create new user.").build();
}
}
}
@DELETE
@Path("ldap")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Deletes a user.",
code = 204
)
@ApiResponses(value = {
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 404, message = "The user could not be found")
})
@PermissionRequired(Permissions.Constants.ACCESS_MANAGEMENT)
public Response deleteLdapUser(LdapUser jsonUser) {
try (QueryManager qm = new QueryManager()) {
final LdapUser user = qm.getLdapUser(jsonUser.getUsername());
if (user != null) {
qm.delete(user);
super.logSecurityEvent(LOGGER, SecurityMarkers.SECURITY_AUDIT, "LDAP user deleted: " + jsonUser.getUsername());
return Response.status(Response.Status.NO_CONTENT).build();
} else {
return Response.status(Response.Status.NOT_FOUND).entity("The user could not be found.").build();
}
}
}
@PUT
@Path("managed")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Creates a new user.",
response = ManagedUser.class,
code = 201
)
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Missing required field"),
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 409, message = "A user with the same username already exists. Cannot create new user")
})
@PermissionRequired(Permissions.Constants.ACCESS_MANAGEMENT)
public Response createManagedUser(ManagedUser jsonUser) {
try (QueryManager qm = new QueryManager()) {
if (StringUtils.isBlank(jsonUser.getUsername())) {
return Response.status(Response.Status.BAD_REQUEST).entity("Username cannot be null or blank.").build();
}
if (StringUtils.isBlank(jsonUser.getFullname())) {
return Response.status(Response.Status.BAD_REQUEST).entity("The users full name is missing.").build();
}
if (StringUtils.isBlank(jsonUser.getEmail())) {
return Response.status(Response.Status.BAD_REQUEST).entity("The users email address is missing.").build();
}
if (StringUtils.isBlank(jsonUser.getNewPassword()) || StringUtils.isBlank(jsonUser.getConfirmPassword())) {
return Response.status(Response.Status.BAD_REQUEST).entity("A password must be set.").build();
}
if (!jsonUser.getNewPassword().equals(jsonUser.getConfirmPassword())) {
return Response.status(Response.Status.BAD_REQUEST).entity("The passwords do not match.").build();
}
ManagedUser user = qm.getManagedUser(jsonUser.getUsername());
if (user == null) {
user = qm.createManagedUser(jsonUser.getUsername(), jsonUser.getFullname(), jsonUser.getEmail(),
String.valueOf(PasswordService.createHash(jsonUser.getNewPassword().toCharArray())),
jsonUser.isForcePasswordChange(), jsonUser.isNonExpiryPassword(), jsonUser.isSuspended());
super.logSecurityEvent(LOGGER, SecurityMarkers.SECURITY_AUDIT, "Managed user created: " + jsonUser.getUsername());
return Response.status(Response.Status.CREATED).entity(user).build();
} else {
return Response.status(Response.Status.CONFLICT).entity("A user with the same username already exists. Cannot create new user.").build();
}
}
}
@POST
@Path("managed")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Updates a managed user.",
response = ManagedUser.class
)
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Missing required field"),
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 404, message = "The user could not be found")
})
@PermissionRequired(Permissions.Constants.ACCESS_MANAGEMENT)
public Response updateManagedUser(ManagedUser jsonUser) {
try (QueryManager qm = new QueryManager()) {
ManagedUser user = qm.getManagedUser(jsonUser.getUsername());
if (user != null) {
if (StringUtils.isBlank(jsonUser.getFullname())) {
return Response.status(Response.Status.BAD_REQUEST).entity("The users full name is missing.").build();
}
if (StringUtils.isBlank(jsonUser.getEmail())) {
return Response.status(Response.Status.BAD_REQUEST).entity("The users email address is missing.").build();
}
if (StringUtils.isNotBlank(jsonUser.getNewPassword()) && StringUtils.isNotBlank(jsonUser.getConfirmPassword()) &&
jsonUser.getNewPassword().equals(jsonUser.getConfirmPassword())) {
user.setPassword(String.valueOf(PasswordService.createHash(jsonUser.getNewPassword().toCharArray())));
}
user.setFullname(jsonUser.getFullname());
user.setEmail(jsonUser.getEmail());
user.setForcePasswordChange(jsonUser.isForcePasswordChange());
user.setNonExpiryPassword(jsonUser.isNonExpiryPassword());
user.setSuspended(jsonUser.isSuspended());
user = qm.updateManagedUser(user);
super.logSecurityEvent(LOGGER, SecurityMarkers.SECURITY_AUDIT, "Managed user updated: " + jsonUser.getUsername());
return Response.ok(user).build();
} else {
return Response.status(Response.Status.NOT_FOUND).entity("The user could not be found.").build();
}
}
}
@DELETE
@Path("managed")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Deletes a user.",
code = 204
)
@ApiResponses(value = {
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 404, message = "The user could not be found")
})
@PermissionRequired(Permissions.Constants.ACCESS_MANAGEMENT)
public Response deleteManagedUser(ManagedUser jsonUser) {
try (QueryManager qm = new QueryManager()) {
final ManagedUser user = qm.getManagedUser(jsonUser.getUsername());
if (user != null) {
qm.delete(user);
super.logSecurityEvent(LOGGER, SecurityMarkers.SECURITY_AUDIT, "Managed user deleted: " + jsonUser.getUsername());
return Response.status(Response.Status.NO_CONTENT).build();
} else {
return Response.status(Response.Status.NOT_FOUND).entity("The user could not be found.").build();
}
}
}
@PUT
@Path("oidc")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Creates a new user that references an existing OpenID Connect user.",
response = OidcUser.class,
code = 201
)
@ApiResponses(value = {
@ApiResponse(code = 400, message = "Username cannot be null or blank."),
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 409, message = "A user with the same username already exists. Cannot create new user")
})
@PermissionRequired(Permissions.Constants.ACCESS_MANAGEMENT)
public Response createOidcUser(final OidcUser jsonUser) {
try (QueryManager qm = new QueryManager()) {
if (StringUtils.isBlank(jsonUser.getUsername())) {
return Response.status(Response.Status.BAD_REQUEST).entity("Username cannot be null or blank.").build();
}
OidcUser user = qm.getOidcUser(jsonUser.getUsername());
if (user == null) {
user = qm.createOidcUser(jsonUser.getUsername());
super.logSecurityEvent(LOGGER, SecurityMarkers.SECURITY_AUDIT, "OpenID Connect user created: " + jsonUser.getUsername());
return Response.status(Response.Status.CREATED).entity(user).build();
} else {
return Response.status(Response.Status.CONFLICT).entity("A user with the same username already exists. Cannot create new user.").build();
}
}
}
@DELETE
@Path("oidc")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Deletes an OpenID Connect user.",
code = 204
)
@ApiResponses(value = {
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 404, message = "The user could not be found")
})
@PermissionRequired(Permissions.Constants.ACCESS_MANAGEMENT)
public Response deleteOidcUser(final OidcUser jsonUser) {
try (QueryManager qm = new QueryManager()) {
final OidcUser user = qm.getOidcUser(jsonUser.getUsername());
if (user != null) {
qm.delete(user);
super.logSecurityEvent(LOGGER, SecurityMarkers.SECURITY_AUDIT, "OpenID Connect user deleted: " + jsonUser.getUsername());
return Response.status(Response.Status.NO_CONTENT).build();
} else {
return Response.status(Response.Status.NOT_FOUND).entity("The user could not be found.").build();
}
}
}
@POST
@Path("/{username}/membership")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Adds the username to the specified team.",
response = UserPrincipal.class
)
@ApiResponses(value = {
@ApiResponse(code = 304, message = "The user is already a member of the specified team"),
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 404, message = "The user or team could not be found")
})
@PermissionRequired(Permissions.Constants.ACCESS_MANAGEMENT)
public Response addTeamToUser(
@ApiParam(value = "A valid username", required = true)
@PathParam("username") String username,
@ApiParam(value = "The UUID of the team to associate username with", required = true)
IdentifiableObject identifiableObject) {
try (QueryManager qm = new QueryManager()) {
final Team team = qm.getObjectByUuid(Team.class, identifiableObject.getUuid());
if (team == null) {
return Response.status(Response.Status.NOT_FOUND).entity("The team could not be found.").build();
}
UserPrincipal principal = qm.getUserPrincipal(username);
if (principal == null) {
return Response.status(Response.Status.NOT_FOUND).entity("The user could not be found.").build();
}
final boolean modified = qm.addUserToTeam(principal, team);
principal = qm.getObjectById(principal.getClass(), principal.getId());
if (modified) {
super.logSecurityEvent(LOGGER, SecurityMarkers.SECURITY_AUDIT, "Added team membership for: " + principal.getName() + " / team: " + team.getName());
return Response.ok(principal).build();
} else {
return Response.status(Response.Status.NOT_MODIFIED).entity("The user is already a member of the specified team.").build();
}
}
}
@DELETE
@Path("/{username}/membership")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(
value = "Removes the username from the specified team.",
response = UserPrincipal.class
)
@ApiResponses(value = {
@ApiResponse(code = 304, message = "The user was not a member of the specified team"),
@ApiResponse(code = 401, message = "Unauthorized"),
@ApiResponse(code = 404, message = "The user or team could not be found")
})
@PermissionRequired(Permissions.Constants.ACCESS_MANAGEMENT)
public Response removeTeamFromUser(
@ApiParam(value = "A valid username", required = true)
@PathParam("username") String username,
@ApiParam(value = "The UUID of the team to un-associate username from", required = true)
IdentifiableObject identifiableObject) {
try (QueryManager qm = new QueryManager()) {
final Team team = qm.getObjectByUuid(Team.class, identifiableObject.getUuid());
if (team == null) {
return Response.status(Response.Status.NOT_FOUND).entity("The team could not be found.").build();
}
UserPrincipal principal = qm.getUserPrincipal(username);
if (principal == null) {
return Response.status(Response.Status.NOT_FOUND).entity("The user could not be found.").build();
}
final boolean modified = qm.removeUserFromTeam(principal, team);
principal = qm.getObjectById(principal.getClass(), principal.getId());
if (modified) {
super.logSecurityEvent(LOGGER, SecurityMarkers.SECURITY_AUDIT, "Removed team membership for: " + principal.getName() + " / team: " + team.getName());
return Response.ok(principal).build();
} else {
return Response.status(Response.Status.NOT_MODIFIED)
.entity("The user was not a member of the specified team.")
.build();
}
}
}
}