Skip to content

Commit

Permalink
Added: Flag ENABLE_TOKEN_MANAGER
Browse files Browse the repository at this point in the history
  • Loading branch information
djuarezgf committed Feb 23, 2024
1 parent 6b56519 commit 45f202c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Fetch project users
- rejectProject state constraints
- Filter output formats for DataSHIELD
- Flag ENABLE_TOKEN_MANAGER
3 changes: 2 additions & 1 deletion src/main/java/de/samply/app/ProjectManagerConst.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ public class ProjectManagerConst {
public final static String MANAGE_TOKENS_CRON_EXPRESSION = "MANAGE_TOKENS_CRON_EXPRESSION";
public final static String CHECK_EXPIRED_ACTIVE_PROJECTS_CRON_EXPRESSION = "CHECK_EXPIRED_ACTIVE_PROJECTS_CRON_EXPRESSION";
public final static String EXPLORER_URL = "EXPLORER_URL";
public final static String ENABLE_TOKEN_MANAGER = "ENABLE_TOKEN_MANAGER";

// Spring Values (SV)
public final static String HEAD_SV = "${";
Expand Down Expand Up @@ -303,13 +304,13 @@ public class ProjectManagerConst {
public final static String FOCUS_URL_SV = HEAD_SV + FOCUS_URL + BOTTOM_SV;
public final static String FOCUS_API_KEY_SV = HEAD_SV + FOCUS_API_KEY + BOTTOM_SV;
public final static String ENABLE_EMAILS_SV = HEAD_SV + ENABLE_EMAILS + ":true" + BOTTOM_SV;
public final static String ENABLE_TOKEN_MANAGER_SV = HEAD_SV + ENABLE_TOKEN_MANAGER + ":true" + BOTTOM_SV;
public final static String MANAGE_TOKENS_CRON_EXPRESSION_SV =
HEAD_SV + MANAGE_TOKENS_CRON_EXPRESSION + ":#{'0 0 * * * *'}" + BOTTOM_SV;
public final static String CHECK_EXPIRED_ACTIVE_PROJECTS_CRON_EXPRESSION_SV =
HEAD_SV + CHECK_EXPIRED_ACTIVE_PROJECTS_CRON_EXPRESSION + ":#{'0 0 1,13 * * *'}" + BOTTOM_SV;
public final static String EXPLORER_URL_SV = HEAD_SV + EXPLORER_URL + BOTTOM_SV;


// Others
public final static String TEST_EMAIL = "[email protected]";
public final static String TEST_BRIDGEHEAD = "bridgehead-test";
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/de/samply/token/DataShieldTokenManagerJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import de.samply.token.dto.DataShieldTokenManagerTokenStatus;
import de.samply.token.dto.DataShieldTokenStatus;
import de.samply.user.roles.ProjectRole;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

Expand All @@ -27,20 +28,26 @@ public class DataShieldTokenManagerJob {
private final DataShieldTokenManagerService tokenManagerService;
private final ProjectBridgeheadUserRepository projectBridgeheadUserRepository;
private final EmailService emailService;
private final boolean isTokenManagerActive;

public DataShieldTokenManagerJob(DataShieldTokenManagerService tokenManagerService,
ProjectBridgeheadUserRepository projectBridgeheadUserRepository,
EmailService emailService) {
EmailService emailService,
@Value(ProjectManagerConst.ENABLE_TOKEN_MANAGER_SV) boolean isTokenManagerActive
) {
this.tokenManagerService = tokenManagerService;
this.projectBridgeheadUserRepository = projectBridgeheadUserRepository;
this.emailService = emailService;
this.isTokenManagerActive = isTokenManagerActive;
}

@Scheduled(cron = ProjectManagerConst.MANAGE_TOKENS_CRON_EXPRESSION_SV)
public void manageTokens() {
manageActiveUsers();
manageInactiveUsers();
manageInactiveProjects();
if (isTokenManagerActive){
manageActiveUsers();
manageInactiveUsers();
manageInactiveProjects();
}
}

private void manageActiveUsers() {
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/de/samply/token/DataShieldTokenManagerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
import de.samply.notification.NotificationService;
import de.samply.notification.OperationType;
import de.samply.security.SessionUser;
import de.samply.token.dto.DataShieldTokenManagerProjectStatus;
import de.samply.token.dto.DataShieldTokenManagerTokenStatus;
import de.samply.token.dto.TokenParams;
import de.samply.token.dto.*;
import de.samply.user.roles.ProjectRole;
import de.samply.utils.WebClientFactory;
import jakarta.validation.constraints.NotNull;
Expand Down Expand Up @@ -50,6 +48,7 @@ public class DataShieldTokenManagerService {
private final ProjectBridgeheadUserRepository projectBridgeheadUserRepository;
private final NotificationService notificationService;
private final BridgeheadConfiguration bridgeheadConfiguration;
private final boolean isTokenManagerActive;

public DataShieldTokenManagerService(SessionUser sessionUser,
WebClientFactory webClientFactory,
Expand All @@ -58,14 +57,16 @@ public DataShieldTokenManagerService(SessionUser sessionUser,
ProjectBridgeheadRepository projectBridgeheadRepository,
ProjectBridgeheadUserRepository projectBridgeheadUserRepository,
NotificationService notificationService,
BridgeheadConfiguration bridgeheadConfiguration) {
BridgeheadConfiguration bridgeheadConfiguration,
@Value(ProjectManagerConst.ENABLE_TOKEN_MANAGER_SV) boolean isTokenManagerActive) {
this.sessionUser = sessionUser;
this.webClientFactory = webClientFactory;
this.projectRepository = projectRepository;
this.projectBridgeheadRepository = projectBridgeheadRepository;
this.projectBridgeheadUserRepository = projectBridgeheadUserRepository;
this.notificationService = notificationService;
this.bridgeheadConfiguration = bridgeheadConfiguration;
this.isTokenManagerActive = isTokenManagerActive;
this.webClient = webClientFactory.createWebClient(tokenManagerUrl);
}

Expand Down Expand Up @@ -145,6 +146,9 @@ public DataShieldTokenManagerTokenStatus fetchTokenStatus(@NotNull String projec
}

public DataShieldTokenManagerProjectStatus fetchProjectStatus(@NotNull String projectCode, @NotNull String bridgehead) {
if (!isTokenManagerActive) {
return new DataShieldTokenManagerProjectStatus(projectCode, bridgehead, DataShieldProjectStatus.INACTIVE);
}
String uri = UriComponentsBuilder.fromPath(ProjectManagerConst.TOKEN_MANAGER_ROOT + ProjectManagerConst.TOKEN_MANAGER_PROJECT_STATUS)
.queryParam(ProjectManagerConst.TOKEN_MANAGER_PARAMETER_BRIDGEHEAD, fetchTokenManagerId(bridgehead))
.queryParam(ProjectManagerConst.TOKEN_MANAGER_PARAMETER_PROJECT_CODE, projectCode)
Expand All @@ -158,6 +162,9 @@ public DataShieldTokenManagerProjectStatus fetchProjectStatus(@NotNull String pr
}

public Resource fetchAuthenticationScript(String projectCode, String bridgehead) throws DataShieldTokenManagerServiceException {
if (!isTokenManagerActive) {
return new ByteArrayResource("Token Manager inactive".getBytes());
}
List<String> tokenManagerIds = fetchTokenManagerIds(fetchProjectBridgeheads(projectCode, bridgehead, sessionUser.getEmail()));
String authenticationScript = webClient.post().uri(uriBuilder ->
uriBuilder.path(ProjectManagerConst.TOKEN_MANAGER_ROOT + ProjectManagerConst.TOKEN_MANAGER_SCRIPTS).build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
public enum DataShieldProjectStatus {
CREATED,
WITH_DATA,
NOT_FOUND
NOT_FOUND,
INACTIVE
}
3 changes: 2 additions & 1 deletion src/main/java/de/samply/token/dto/DataShieldTokenStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
public enum DataShieldTokenStatus {
CREATED,
EXPIRED,
NOT_FOUND
NOT_FOUND,
INACTIVE
}

0 comments on commit 45f202c

Please sign in to comment.