Skip to content

Commit 9dbb892

Browse files
Add option to save access tokens on database or not
1 parent b75b19a commit 9dbb892

169 files changed

Lines changed: 4627 additions & 4271 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎iam-common/src/main/java/it/infn/mw/iam/service/aup/DefaultAupSignatureCheckService.java‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static java.util.Objects.isNull;
1919

20+
import java.time.Clock;
2021
import java.util.Date;
2122
import java.util.Optional;
2223
import java.util.concurrent.TimeUnit;
@@ -25,7 +26,6 @@
2526
import org.slf4j.LoggerFactory;
2627
import org.springframework.stereotype.Service;
2728

28-
import it.infn.mw.iam.core.time.TimeProvider;
2929
import it.infn.mw.iam.persistence.model.IamAccount;
3030
import it.infn.mw.iam.persistence.model.IamAup;
3131
import it.infn.mw.iam.persistence.repository.IamAupRepository;
@@ -38,20 +38,21 @@ public class DefaultAupSignatureCheckService implements AUPSignatureCheckService
3838

3939
final IamAupRepository aupRepo;
4040
final IamAupSignatureRepository signatureRepo;
41-
final TimeProvider timeProvider;
41+
final Clock clock;
4242

4343
public DefaultAupSignatureCheckService(IamAupRepository aupRepo,
44-
IamAupSignatureRepository signatureRepo, TimeProvider timeProvider) {
44+
IamAupSignatureRepository signatureRepo, Clock clock) {
4545
this.aupRepo = aupRepo;
4646
this.signatureRepo = signatureRepo;
47-
this.timeProvider = timeProvider;
47+
this.clock = clock;
4848
}
4949

5050
@Override
5151
public boolean needsAupSignature(IamAccount account) {
52+
5253
Optional<IamAup> aup = aupRepo.findDefaultAup();
5354

54-
Date now = new Date(timeProvider.currentTimeMillis());
55+
Date now = Date.from(clock.instant());
5556

5657
if (!aup.isPresent()) {
5758
LOG.debug("AUP signature not needed for account '{}': AUP is not defined",

‎iam-login-service/src/main/java/it/infn/mw/iam/api/aup/AupSignatureController.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static java.lang.String.format;
1919

20+
import java.time.Clock;
2021
import java.util.Date;
2122
import java.util.Optional;
2223
import java.util.function.Supplier;
@@ -50,7 +51,6 @@
5051
import it.infn.mw.iam.audit.events.aup.AupSignatureDeletedEvent;
5152
import it.infn.mw.iam.audit.events.aup.AupSignedEvent;
5253
import it.infn.mw.iam.audit.events.aup.AupSignedOnBehalfEvent;
53-
import it.infn.mw.iam.core.time.TimeProvider;
5454
import it.infn.mw.iam.notification.NotificationFactory;
5555
import it.infn.mw.iam.persistence.model.IamAccount;
5656
import it.infn.mw.iam.persistence.model.IamAup;
@@ -72,18 +72,18 @@ public class AupSignatureController {
7272
private final AccountUtils accountUtils;
7373
private final IamAupSignatureRepository signatureRepo;
7474
private final IamAupRepository aupRepo;
75-
private final TimeProvider timeProvider;
75+
private final Clock clock;
7676
private final ApplicationEventPublisher eventPublisher;
7777
private final NotificationFactory notificationFactory;
7878

7979
public AupSignatureController(AupSignatureConverter conv, AccountUtils utils,
80-
IamAupSignatureRepository signatureRepo, IamAupRepository aupRepo, TimeProvider timeProvider,
80+
IamAupSignatureRepository signatureRepo, IamAupRepository aupRepo, Clock clock,
8181
ApplicationEventPublisher publisher, NotificationFactory notificationFactory) {
8282
this.signatureConverter = conv;
8383
this.accountUtils = utils;
8484
this.signatureRepo = signatureRepo;
8585
this.aupRepo = aupRepo;
86-
this.timeProvider = timeProvider;
86+
this.clock = clock;
8787
this.eventPublisher = publisher;
8888
this.notificationFactory = notificationFactory;
8989
}
@@ -109,7 +109,7 @@ public void signAup() throws AccountNotFoundException {
109109
IamAccount account = accountUtils.getAuthenticatedUserAccount()
110110
.orElseThrow(accountNotFoundException(ACCOUNT_NOT_FOUND_FOR_AUTHENTICATED_USER_MESSAGE));
111111

112-
Date now = new Date(timeProvider.currentTimeMillis());
112+
Date now = Date.from(clock.instant());
113113
IamAupSignature signature = signatureRepo.createSignatureForAccount(aup, account, now);
114114
eventPublisher.publishEvent(new AupSignedEvent(this, signature));
115115
}
@@ -156,7 +156,7 @@ public AupSignatureDTO updateSignatureForAccount(@PathVariable String accountId,
156156
IamAup aup = aupRepo.findDefaultAup().orElseThrow(aupNotFoundException());
157157

158158
Date signatureTime =
159-
dto == null ? new Date(timeProvider.currentTimeMillis()) : dto.getSignatureTime();
159+
dto == null ? Date.from(clock.instant()) : dto.getSignatureTime();
160160
IamAupSignature signature =
161161
signatureRepo.createSignatureForAccount(aup, account, signatureTime);
162162

‎iam-login-service/src/main/java/it/infn/mw/iam/api/aup/AupSignaturePageController.java‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static java.util.Objects.isNull;
2121
import static java.util.Optional.ofNullable;
2222

23+
import java.time.Clock;
2324
import java.util.Date;
2425
import java.util.Optional;
2526

@@ -41,7 +42,6 @@
4142
import it.infn.mw.iam.api.account.AccountUtils;
4243
import it.infn.mw.iam.api.common.ErrorDTO;
4344
import it.infn.mw.iam.audit.events.aup.AupSignedEvent;
44-
import it.infn.mw.iam.core.time.TimeProvider;
4545
import it.infn.mw.iam.persistence.model.IamAccount;
4646
import it.infn.mw.iam.persistence.model.IamAup;
4747
import it.infn.mw.iam.persistence.model.IamAupSignature;
@@ -56,16 +56,16 @@ public class AupSignaturePageController {
5656
final IamAupRepository repo;
5757
final IamAupSignatureRepository signatureRepo;
5858
final AccountUtils accountUtils;
59-
final TimeProvider timeProvider;
59+
final Clock clock;
6060
final ApplicationEventPublisher publisher;
6161

6262
public AupSignaturePageController(IamAupRepository aupRepo,
63-
IamAupSignatureRepository aupSignatureRepo, AccountUtils accountUtils,
64-
TimeProvider timeProvider, ApplicationEventPublisher publisher) {
63+
IamAupSignatureRepository aupSignatureRepo, AccountUtils accountUtils, Clock clock,
64+
ApplicationEventPublisher publisher) {
6565
this.repo = aupRepo;
6666
this.signatureRepo = aupSignatureRepo;
6767
this.accountUtils = accountUtils;
68-
this.timeProvider = timeProvider;
68+
this.clock = clock;
6969
this.publisher = publisher;
7070
}
7171

@@ -112,9 +112,10 @@ public ModelAndView signAup(HttpServletRequest request, HttpServletResponse resp
112112
}
113113

114114
if (aup.isPresent()) {
115-
Date now = new Date(timeProvider.currentTimeMillis());
116-
IamAccount account = accountUtils.getAuthenticatedUserAccount().orElseThrow(
117-
() -> new IllegalStateException("No iam account found for authenticated user"));
115+
Date now = Date.from(clock.instant());
116+
IamAccount account = accountUtils.getAuthenticatedUserAccount()
117+
.orElseThrow(
118+
() -> new IllegalStateException("No iam account found for authenticated user"));
118119

119120
IamAupSignature signature = signatureRepo.createSignatureForAccount(aup.get(), account, now);
120121

‎iam-login-service/src/main/java/it/infn/mw/iam/api/aup/DefaultAupService.java‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
*/
1616
package it.infn.mw.iam.api.aup;
1717

18+
import java.time.Clock;
1819
import java.util.Date;
1920
import java.util.Optional;
2021

21-
import org.springframework.beans.factory.annotation.Autowired;
2222
import org.springframework.context.ApplicationEventPublisher;
2323
import org.springframework.stereotype.Service;
2424

@@ -28,7 +28,6 @@
2828
import it.infn.mw.iam.audit.events.aup.AupCreatedEvent;
2929
import it.infn.mw.iam.audit.events.aup.AupDeletedEvent;
3030
import it.infn.mw.iam.audit.events.aup.AupUpdatedEvent;
31-
import it.infn.mw.iam.core.time.TimeProvider;
3231
import it.infn.mw.iam.persistence.model.IamAup;
3332
import it.infn.mw.iam.persistence.repository.IamAupRepository;
3433
import it.infn.mw.iam.persistence.repository.IamAupSignatureRepository;
@@ -40,16 +39,15 @@ public class DefaultAupService implements AupService {
4039
private final IamAupSignatureRepository signatureRepo;
4140
private final AupConverter converter;
4241

43-
private final TimeProvider timeProvider;
42+
private final Clock clock;
4443
private final ApplicationEventPublisher eventPublisher;
4544

46-
@Autowired
4745
public DefaultAupService(IamAupRepository repo, IamAupSignatureRepository signatureRepo,
48-
AupConverter converter, TimeProvider timeProvider, ApplicationEventPublisher eventPublisher) {
46+
AupConverter converter, Clock clock, ApplicationEventPublisher eventPublisher) {
4947
this.repo = repo;
5048
this.signatureRepo = signatureRepo;
5149
this.converter = converter;
52-
this.timeProvider = timeProvider;
50+
this.clock = clock;
5351
this.eventPublisher = eventPublisher;
5452
}
5553

@@ -60,10 +58,9 @@ public Optional<IamAup> findAup() {
6058

6159
@Override
6260
public IamAup saveAup(AupDTO aupDto) {
63-
IamAup aup = converter.entityFromDto(aupDto);
64-
long currentTimeMillis = timeProvider.currentTimeMillis();
6561

66-
Date now = new Date(currentTimeMillis);
62+
IamAup aup = converter.entityFromDto(aupDto);
63+
Date now = Date.from(clock.instant());
6764
aup.setCreationTime(now);
6865
aup.setLastUpdateTime(now);
6966

@@ -105,8 +102,7 @@ public IamAup updateAup(AupDTO aupDto) {
105102
@Override
106103
public IamAup touchAup() {
107104

108-
long currentTimeMillis = timeProvider.currentTimeMillis();
109-
Date now = new Date(currentTimeMillis);
105+
Date now = Date.from(clock.instant());
110106

111107
IamAup aup = repo.findDefaultAup().orElseThrow(AupNotFoundError::new);
112108

‎iam-login-service/src/main/java/it/infn/mw/iam/api/client/management/service/DefaultClientManagementService.java‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515
*/
1616
package it.infn.mw.iam.api.client.management.service;
1717

18+
import static it.infn.mw.iam.api.client.util.ClientSuppliers.accountNotFound;
19+
import static it.infn.mw.iam.api.client.util.ClientSuppliers.clientNotFound;
20+
1821
import java.text.ParseException;
1922
import java.time.Clock;
2023
import java.util.Date;
2124
import java.util.List;
25+
import java.util.Objects;
2226
import java.util.Optional;
2327
import java.util.stream.Collectors;
2428

@@ -32,6 +36,7 @@
3236
import org.springframework.data.domain.Pageable;
3337
import org.springframework.security.oauth2.common.exceptions.InvalidRequestException;
3438
import org.springframework.stereotype.Service;
39+
import org.springframework.transaction.annotation.Transactional;
3540
import org.springframework.validation.annotation.Validated;
3641

3742
import it.infn.mw.iam.api.client.management.validation.OnClientCreation;
@@ -40,8 +45,6 @@
4045
import it.infn.mw.iam.api.client.service.ClientDefaultsService;
4146
import it.infn.mw.iam.api.client.service.ClientService;
4247
import it.infn.mw.iam.api.client.util.ClientSuppliers;
43-
import static it.infn.mw.iam.api.client.util.ClientSuppliers.accountNotFound;
44-
import static it.infn.mw.iam.api.client.util.ClientSuppliers.clientNotFound;
4548
import it.infn.mw.iam.api.common.ListResponseDTO;
4649
import it.infn.mw.iam.api.common.PagingUtils;
4750
import it.infn.mw.iam.api.common.client.RegisteredClientDTO;
@@ -62,6 +65,7 @@
6265

6366
@Service
6467
@Validated
68+
@Transactional
6569
public class DefaultClientManagementService implements ClientManagementService {
6670

6771
private final Clock clock;
@@ -233,6 +237,7 @@ public ListResponseDTO<ScimUser> getClientOwners(String clientId, Pageable pagea
233237

234238
@Override
235239
public void assignClientOwner(String clientId, String accountId) {
240+
236241
ClientDetailsEntity client =
237242
clientService.findClientByClientId(clientId).orElseThrow(clientNotFound(clientId));
238243
IamAccount account = accountRepo.findByUuid(accountId).orElseThrow(accountNotFound(accountId));
@@ -243,6 +248,7 @@ public void assignClientOwner(String clientId, String accountId) {
243248

244249
@Override
245250
public void removeClientOwner(String clientId, String accountId) {
251+
246252
ClientDetailsEntity client =
247253
clientService.findClientByClientId(clientId).orElseThrow(clientNotFound(clientId));
248254
IamAccount account = accountRepo.findByUuid(accountId).orElseThrow(accountNotFound(accountId));
@@ -254,19 +260,22 @@ public void removeClientOwner(String clientId, String accountId) {
254260

255261
private OAuth2AccessTokenEntity createRegistrationAccessTokenForClient(
256262
ClientDetailsEntity client) {
263+
257264
OAuth2AccessTokenEntity token = oidcTokenService.createRegistrationAccessToken(client);
258265
return tokenService.saveAccessToken(token);
259266

260267
}
261268

262269
@Override
263270
public RegisteredClientDTO rotateRegistrationAccessToken(@NotBlank String clientId) {
271+
264272
ClientDetailsEntity client =
265273
clientService.findClientByClientId(clientId).orElseThrow(clientNotFound(clientId));
266274

267-
OAuth2AccessTokenEntity rat =
268-
Optional.ofNullable(oidcTokenService.rotateRegistrationAccessTokenForClient(client))
269-
.orElse(createRegistrationAccessTokenForClient(client));
275+
OAuth2AccessTokenEntity rat = oidcTokenService.rotateRegistrationAccessTokenForClient(client);
276+
if (Objects.isNull(rat)) {
277+
rat = createRegistrationAccessTokenForClient(client);
278+
}
270279

271280
tokenService.saveAccessToken(rat);
272281

0 commit comments

Comments
 (0)