1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.bremersee.spring.boot.autoconfigure.security.authentication;
18
19 import static java.util.Objects.nonNull;
20 import static java.util.Objects.requireNonNullElseGet;
21 import static org.springframework.util.ObjectUtils.isEmpty;
22
23 import java.io.Serial;
24 import java.io.Serializable;
25 import java.security.Principal;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.LinkedHashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.stream.Collectors;
32 import java.util.stream.Stream;
33 import lombok.Data;
34 import lombok.EqualsAndHashCode;
35 import lombok.Getter;
36 import lombok.Setter;
37 import lombok.ToString;
38 import org.bremersee.spring.security.core.NormalizedPrincipalDetails;
39 import org.bremersee.spring.security.core.NormalizedUserDetails;
40 import org.springframework.boot.context.properties.ConfigurationProperties;
41 import org.springframework.security.core.authority.SimpleGrantedAuthority;
42 import org.springframework.security.crypto.factory.PasswordEncoderFactories;
43 import org.springframework.security.crypto.password.PasswordEncoder;
44
45
46
47
48
49
50 @ConfigurationProperties(prefix = "bremersee.authentication")
51 @Data
52 public class AuthenticationProperties {
53
54
55
56
57 private RememberMeProperties rememberMe = new RememberMeProperties();
58
59
60
61
62 private JwtConverterProperties jwtConverter = new JwtConverterProperties();
63
64
65
66
67 private LdaptiveProperties ldaptive = new LdaptiveProperties();
68
69
70
71
72 private ActuatorProperties actuator = new ActuatorProperties();
73
74
75
76
77 private InMemoryProperties inMemory = new InMemoryProperties();
78
79
80
81
82 public AuthenticationProperties() {
83 super();
84 }
85
86
87
88
89
90
91 @Data
92 public static class RememberMeProperties {
93
94
95
96
97 private String key;
98
99
100
101
102 private Boolean alwaysRemember;
103
104
105
106
107 private String cookieName;
108
109
110
111
112 private String cookieDomain;
113
114
115
116
117 private Boolean useSecureCookie;
118
119
120
121
122 private String parameterName;
123
124
125
126
127 private Integer tokenValiditySeconds;
128
129
130
131
132 public RememberMeProperties() {
133 super();
134 }
135 }
136
137
138
139
140
141
142 @Data
143 public static class JwtConverterProperties {
144
145
146
147
148 private String nameJsonPath = "$.sub";
149
150
151
152
153 private String firstNameJsonPath = "$.given_name";
154
155
156
157
158 private String lastNameJsonPath = "$.family_name";
159
160
161
162
163 private String emailJsonPath = "$.email";
164
165
166
167
168 private String rolesJsonPath = "$.scope";
169
170
171
172
173
174 private boolean rolesValueList = false;
175
176
177
178
179 private String rolesValueSeparator = " ";
180
181
182
183
184 private List<String> defaultRoles = new ArrayList<>();
185
186
187
188
189 private List<SimpleMapping> roleMapping = new ArrayList<>();
190
191
192
193
194 private String rolePrefix = "SCOPE_";
195
196
197
198
199 private CaseTransformation roleCaseTransformation;
200
201
202
203
204 private List<StringReplacement> roleStringReplacements;
205
206
207
208
209 private String groupsJsonPath = "$.groups";
210
211
212
213
214
215 private boolean groupsValueList = false;
216
217
218
219
220 private String groupsValueSeparator = " ";
221
222
223
224
225 private List<String> defaultGroups = new ArrayList<>();
226
227
228
229
230 private List<SimpleMapping> groupMapping = new ArrayList<>();
231
232
233
234
235 private String groupPrefix = "";
236
237
238
239
240 private CaseTransformation groupCaseTransformation;
241
242
243
244
245 private List<StringReplacement> groupStringReplacements;
246
247
248
249
250 public JwtConverterProperties() {
251 super();
252 }
253
254
255
256
257
258
259 public Map<String, String> toRoleMappings() {
260 return Stream.ofNullable(getRoleMapping())
261 .flatMap(Collection::stream)
262 .collect(Collectors.toMap(
263 SimpleMapping::getSource,
264 SimpleMapping::getTarget,
265 (first, second) -> first,
266 LinkedHashMap::new));
267 }
268
269
270
271
272
273
274 public Map<String, String> toGroupMappings() {
275 return Stream.ofNullable(getGroupMapping())
276 .flatMap(Collection::stream)
277 .collect(Collectors.toMap(
278 SimpleMapping::getSource,
279 SimpleMapping::getTarget,
280 (first, second) -> first,
281 LinkedHashMap::new));
282 }
283
284
285
286
287
288
289 public Map<String, String> toRoleStringReplacements() {
290 return Stream.ofNullable(getRoleStringReplacements())
291 .flatMap(Collection::stream)
292 .collect(Collectors.toMap(
293 StringReplacement::getRegex,
294 StringReplacement::getReplacement,
295 (first, second) -> first,
296 LinkedHashMap::new));
297 }
298
299
300
301
302
303
304 public Map<String, String> toGroupStringReplacements() {
305 return Stream.ofNullable(getGroupStringReplacements())
306 .flatMap(Collection::stream)
307 .collect(Collectors.toMap(
308 StringReplacement::getRegex,
309 StringReplacement::getReplacement,
310 (first, second) -> first,
311 LinkedHashMap::new));
312 }
313
314 }
315
316
317
318
319
320
321 @Data
322 public static class LdaptiveProperties {
323
324
325
326
327 private Template template = Template.ACTIVE_DIRECTORY;
328
329
330
331
332 private String userBaseDn;
333
334
335
336
337 private List<String> refusedUsernames;
338
339
340
341
342
343 private String userObjectClass;
344
345
346
347
348
349 private String usernameAttribute;
350
351
352
353
354
355
356
357
358 private String passwordAttribute;
359
360
361
362
363
364 private String passwordLastSetAttribute;
365
366
367
368
369
370 private String userFindOneFilter;
371
372
373
374
375 private SearchScope userFindOneSearchScope;
376
377
378
379
380 protected String firstNameAttribute;
381
382
383
384
385 protected String lastNameAttribute;
386
387
388
389
390 private String emailAttribute;
391
392
393
394
395 private AccountControlEvaluatorProperty accountControlEvaluator;
396
397
398
399
400 private GroupFetchStrategy groupFetchStrategy;
401
402
403
404
405 private String memberAttribute;
406
407
408
409
410
411 private String groupBaseDn;
412
413
414
415
416
417 private SearchScope groupSearchScope;
418
419
420
421
422
423 private String groupObjectClass;
424
425
426
427
428
429 private String groupIdAttribute;
430
431
432
433
434
435 private String groupMemberAttribute;
436
437
438
439
440
441 private String groupMemberFormat;
442
443
444
445
446 private List<SimpleMapping> roleMapping;
447
448
449
450
451 private List<String> defaultRoles;
452
453
454
455
456 private String rolePrefix;
457
458
459
460
461 private CaseTransformation roleCaseTransformation;
462
463
464
465
466 private List<StringReplacement> roleStringReplacements;
467
468
469
470
471 public LdaptiveProperties() {
472 super();
473 }
474
475
476
477
478 public enum SearchScope {
479
480
481
482
483 OBJECT,
484
485
486
487
488 ONELEVEL,
489
490
491
492
493 SUBTREE,
494
495
496
497
498 SUBORDINATE
499 }
500
501
502
503
504 public enum AccountControlEvaluatorProperty {
505
506
507
508
509 NONE,
510
511
512
513
514 ACTIVE_DIRECTORY
515 }
516
517
518
519
520 public enum GroupFetchStrategy {
521
522
523
524
525 NONE,
526
527
528
529
530 USER_CONTAINS_GROUPS,
531
532
533
534
535 GROUP_CONTAINS_USERS
536 }
537
538
539
540
541
542
543 public enum Template {
544
545
546
547
548 ACTIVE_DIRECTORY,
549
550
551
552
553 OPEN_LDAP,
554
555
556
557
558 USER_CONTAINS_GROUPS,
559
560
561
562
563 GROUP_CONTAINS_USERS
564 }
565 }
566
567
568
569
570
571
572 @Data
573 public static class SimpleMapping {
574
575 private String source;
576
577 private String target;
578
579
580
581
582 public SimpleMapping() {
583 super();
584 }
585 }
586
587
588
589
590 public enum CaseTransformation {
591
592
593
594
595 NONE,
596
597
598
599
600 TO_UPPER_CASE,
601
602
603
604
605 TO_LOWER_CASE
606 }
607
608
609
610
611
612
613 @Data
614 public static class StringReplacement {
615
616
617
618
619
620 private String regex;
621
622
623
624
625 private String replacement;
626
627
628
629
630 public StringReplacement() {
631 super();
632 }
633 }
634
635
636
637
638
639
640 @Data
641 public static class ActuatorProperties {
642
643 private List<String> readRoles = new ArrayList<>();
644
645 private List<String> writeRoles = new ArrayList<>();
646
647
648
649
650 public ActuatorProperties() {
651 super();
652 }
653
654
655
656
657
658
659 public List<String> getReadRoles() {
660 if (isEmpty(readRoles)) {
661 return List.of("ROLE_ADMIN", "ROLE_ACTUATOR", "ROLE_ACTUATOR_ADMIN");
662 }
663 return readRoles;
664 }
665
666
667
668
669
670
671 public List<String> getWriteRoles() {
672 if (isEmpty(writeRoles)) {
673 return List.of("ROLE_ADMIN", "ROLE_ACTUATOR_ADMIN");
674 }
675 return writeRoles;
676 }
677 }
678
679
680
681
682
683
684 @Data
685 public static class InMemoryProperties {
686
687
688
689
690
691 private boolean enabled;
692
693
694
695
696 private List<SimpleUser> users = new ArrayList<>();
697
698
699
700
701
702
703
704 public List<NormalizedPrincipalDetails> buildUserDetails(PasswordEncoder passwordEncoder) {
705 PasswordEncoder encoder = requireNonNullElseGet(
706 passwordEncoder,
707 PasswordEncoderFactories::createDelegatingPasswordEncoder);
708 return getUsers().stream()
709 .map(user -> NormalizedUserDetails.builder()
710 .username(user.getName())
711 .password(nonNull(user.getPassword()) ? encoder.encode(user.getPassword()) : null)
712 .firstName(user.getFirstName())
713 .lastName(user.getLastName())
714 .email(user.getEmail())
715 .authorities(user.getAuthorities().stream()
716 .map(SimpleGrantedAuthority::new)
717 .toList())
718 .build())
719 .map(NormalizedPrincipalDetails.class::cast)
720 .toList();
721 }
722 }
723
724
725
726
727
728
729 @Getter
730 @Setter
731 @ToString(exclude = "password")
732 @EqualsAndHashCode(exclude = "password")
733 public static class SimpleUser implements Serializable, Principal {
734
735 @Serial
736 private static final long serialVersionUID = -1393400622632455935L;
737
738
739
740
741 private String name;
742
743
744
745
746 private String password;
747
748
749
750
751 private String firstName;
752
753
754
755
756 private String lastName;
757
758
759
760
761 private String email;
762
763
764
765
766 private List<String> authorities = new ArrayList<>();
767
768
769
770
771 public SimpleUser() {
772 super();
773 }
774 }
775
776 }