Skip to content

Commit

Permalink
IM-457 Revert "Revert "Merge pull request #216 from integratedmodelli…
Browse files Browse the repository at this point in the history
…ng/IM-457-Merge-develop-in-IM-384""
  • Loading branch information
kristinaBc3 committed Dec 18, 2024
1 parent 861ab72 commit 4009ad0
Show file tree
Hide file tree
Showing 140 changed files with 3,767 additions and 3,003 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,10 @@ public static interface HUB {
* Base URL path for custom properties related to a single user.
*/
public static final String USER_ID_CUSTOM_PROPERTIES = USER_BASE_ID + "/custom-properties";
/**
* URL path for get SPA pages
*/
public static final String UI = "/ui/*";

public static interface PARAMETERS {
/**
Expand Down Expand Up @@ -1279,6 +1283,11 @@ public interface VIEW {
}

}

/**
* URL path for get SPA pages
*/
public static final String UI = "/ui/*";

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.integratedmodelling.klab.api.auth;

/**
* Defines constants for common HTTP headers.
*
* <p>This interface provides a set of predefined header names used
* in HTTP requests and responses. These constants can be used to
* avoid hardcoding string values and reduce errors.</p>
*
* @author Kristina
*/

public interface KlabHttpHeaders {

/**
* Designed to send session information with requests.
**/
public static final String KLAB_AUTHORIZATION = "klab-authorization";

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
*/
public class RemoteUserAuthenticationRequest extends UserAuthenticationRequest {

private String token;
/**
private String token;
/**
* @return the token
*/
public String getToken() {
Expand Down Expand Up @@ -47,6 +47,6 @@ public boolean equals(Object obj) {

@Override
public String toString() {
return "RemoteUserAuthenticationRequest [username=" + getUsername() + ", password=" + getPassword() + ", token="+getToken()+"]";
return "RemoteUserAuthenticationRequest [username=" + getUsername() + ", token="+getToken()+"]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,18 @@

public class UserAuthenticationRequest {

private String username;
private String password;
private boolean remote = false;
private String username;

public String getUsername() {
return username;
}
private boolean remote = false;

public void setUsername(String username) {
public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public boolean isRemote() {
return remote;
}
Expand All @@ -34,7 +26,7 @@ public void setRemote(boolean jwtToken) {

@Override
public int hashCode() {
return Objects.hash(remote, password, username);
return Objects.hash(remote, username);
}

@Override
Expand All @@ -46,12 +38,12 @@ public boolean equals(Object obj) {
return false;
}
UserAuthenticationRequest other = (UserAuthenticationRequest) obj;
return remote == other.remote && Objects.equals(password, other.password) && Objects.equals(username, other.username);
return remote == other.remote && Objects.equals(username, other.username);
}

@Override
public String toString() {
return "UserAuthenticationRequest [username=" + username + ", password=" + password + ", remote=" + remote + "]";
return "UserAuthenticationRequest [username=" + username + ", remote=" + remote + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static String getSessionTokenForDefaultAdministrator(int port) throws URI
URI uri = new URI(baseUrl);
HttpHeaders headers = new HttpHeaders();
UserAuthenticationRequest auth= new UserAuthenticationRequest();
auth.setPassword("password");

auth.setUsername("system");
HttpEntity<?> request = new HttpEntity<>(auth, headers);
RestTemplate restTemplate = new RestTemplate();
Expand Down
16 changes: 15 additions & 1 deletion klab.engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,21 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-resource-server</artifactId>
<version>${spring-security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-jose</artifactId>
<version>${spring-security.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>org.integratedmodelling</groupId>
Expand All @@ -573,7 +588,6 @@
<artifactId>jopt-simple </artifactId>
<version>4.6</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.eclipse.xtext/org.eclipse.xtext -->
<dependency>
<groupId>org.eclipse.xtext</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.integratedmodelling.klab.engine.rest.api;

import javax.validation.constraints.NotEmpty;


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

@Component
@ConfigurationProperties("engine")
public class EngineProperties {

public EnvProperties env;

public EnvProperties getEnv() {
return env;
}

public void setEnv(EnvProperties env) {
this.env = env;
}

@Validated
public static class EnvProperties {

@NotEmpty
private String appBaseUrl;

@NotEmpty
private String keycloakUrl;

public String getAppBaseUrl() {
return appBaseUrl;
}

public void setAppBaseUrl(String appBaseUrl) {
this.appBaseUrl = appBaseUrl;
}

public String getKeycloakUrl() {
return keycloakUrl;
}

public void setKeycloakUrl(String keycloakUrl) {
this.keycloakUrl = keycloakUrl;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.integratedmodelling.klab.engine.rest.controllers.base;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.integratedmodelling.klab.engine.rest.api.EngineProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;

@RestController
public class EnvironmentController {

@Autowired
private EngineProperties engineProperties;

private static final String APP_BASE_URL = "APP_BASE_URL";
private static final String KEYCLOAK_URL = "KEYCLOAK_URL";
private static final String ACTIVE_PROFILE = "ACTIVE_PROFILE";

private static final String ENGINE_REMOTE = "engine.remote";
private static final String ENGINE_LOCAL = "engine.local";

@GetMapping(value = "/engine/environments")
public void getEnvironmentVariables(HttpServletRequest request, HttpServletResponse response) throws IOException {

response.setContentType("text/javascript;utf-8");

List<String> activeProfiles = Pattern.compile(",").splitAsStream(System.getProperty("spring.profiles.active", "unknown"))
.collect(Collectors.toList());

String activeProfile = activeProfiles.contains(ENGINE_REMOTE) ? ENGINE_REMOTE : ENGINE_LOCAL;

/*
* Get engine properties
*/
Map<String, String> kHubEnvironmentVariables = new HashMap<>();

if (activeProfile.equals(ENGINE_REMOTE)) {
kHubEnvironmentVariables = Map.ofEntries(Map.entry(APP_BASE_URL, engineProperties.env.getAppBaseUrl()),
Map.entry(KEYCLOAK_URL, engineProperties.env.getKeycloakUrl()), Map.entry(ACTIVE_PROFILE, activeProfile));
} else {
kHubEnvironmentVariables = Map.ofEntries(Map.entry(ACTIVE_PROFILE, activeProfile));
}

ObjectMapper objectMapper = new ObjectMapper();
String jsonValue = objectMapper.writeValueAsString(kHubEnvironmentVariables);

System.out.println(jsonValue);

response.getWriter().append("var __ENV__= " + jsonValue + ";");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
*/
@RestController
@CrossOrigin(origins = "*")
@Secured(Roles.PUBLIC)
//@Secured(Roles.PUBLIC)
public class KlabController {

@RequestMapping(value = API.ENGINE.RESOURCE.GET_PROJECT_RESOURCE, method = RequestMethod.GET)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.integratedmodelling.klab.api.API;
import org.integratedmodelling.klab.api.PublicAPI;
import org.integratedmodelling.klab.api.auth.IUserIdentity;
import org.integratedmodelling.klab.api.auth.KlabHttpHeaders;
import org.integratedmodelling.klab.api.auth.Roles;
import org.integratedmodelling.klab.api.data.ILocator;
import org.integratedmodelling.klab.api.data.adapters.IResourceAdapter;
Expand Down Expand Up @@ -60,6 +61,7 @@
import org.integratedmodelling.klab.utils.NumberUtils;
import org.springframework.http.MediaType;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -79,8 +81,8 @@ public class EnginePublicController implements API.PUBLIC {
@RequestMapping(value = CREATE_CONTEXT, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public TicketResponse.Ticket contextRequest(@RequestBody ContextRequest request,
@RequestHeader(name = "Authorization") String session) {

@RequestHeader(name = KlabHttpHeaders.KLAB_AUTHORIZATION) String session) {
Session s = Authentication.INSTANCE.getIdentity(session, Session.class);
if (s == null) {
throw new KlabIllegalStateException("create context: invalid session ID");
Expand Down Expand Up @@ -109,7 +111,7 @@ public TicketResponse.Ticket contextRequest(@RequestBody ContextRequest request,
@RequestMapping(value = OBSERVE_IN_CONTEXT, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public TicketResponse.Ticket observationRequest(@RequestBody ObservationRequest request,
@RequestHeader(name = "Authorization") String session, @PathVariable String context) {
@RequestHeader(name = KlabHttpHeaders.KLAB_AUTHORIZATION) String session, @PathVariable String context) {

Session s = Authentication.INSTANCE.getIdentity(session, Session.class);

Expand Down Expand Up @@ -143,7 +145,7 @@ public TicketResponse.Ticket observationRequest(@RequestBody ObservationRequest

@RequestMapping(value = SUBMIT_ESTIMATE, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public TicketResponse.Ticket submitEstimate(@RequestHeader(name = "Authorization") String session,
public TicketResponse.Ticket submitEstimate(@RequestHeader(name = KlabHttpHeaders.KLAB_AUTHORIZATION) String session,
@PathVariable String estimate) {

Session s = Authentication.INSTANCE.getIdentity(session, Session.class);
Expand All @@ -163,6 +165,7 @@ public TicketResponse.Ticket submitEstimate(@RequestHeader(name = "Authorization
}

if (est.contextRequest != null) {
//TODO only 1 sessioin parameter
return contextRequest(est.contextRequest, session);
}

Expand All @@ -173,11 +176,11 @@ public TicketResponse.Ticket submitEstimate(@RequestHeader(name = "Authorization
MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_PDF_VALUE, MediaType.IMAGE_PNG_VALUE, "text/csv", "image/tiff",
"application/vnd.ms-excel", "application/octet-stream",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"})
public void exportData(@PathVariable String export, @RequestHeader(name = "Authorization") String session,
public void exportData(@PathVariable String export, @RequestHeader(name = KlabHttpHeaders.KLAB_AUTHORIZATION) String session,
@PathVariable String observation, @RequestHeader(name = "Accept") String format,
@RequestParam(required = false) String view, @RequestParam(required = false) String viewport,
@RequestParam(required = false) String locator, HttpServletResponse response) throws IOException {

Session s = Authentication.INSTANCE.getIdentity(session, Session.class);
if (s == null) {
throw new KlabIllegalStateException("observe in context: invalid session ID");
Expand Down Expand Up @@ -385,7 +388,7 @@ private void outputImage(IObservation obs, HttpServletResponse response, Export

@RequestMapping(value = TICKET_INFO, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public TicketResponse.Ticket getTicketInfo(@RequestHeader(name = "Authorization") String session,
public TicketResponse.Ticket getTicketInfo(@RequestHeader(name = KlabHttpHeaders.KLAB_AUTHORIZATION) String session,
@PathVariable String ticket) {

Session s = Authentication.INSTANCE.getIdentity(session, Session.class);
Expand Down
Loading

0 comments on commit 4009ad0

Please sign in to comment.