Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sabi-93 Problem, entering a measurement after waiting some time (sess… #96

Merged
merged 1 commit into from
Aug 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions devops/sabi_docker_sdk/sabi-backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ RUN chmod 600 /opt/config/application.properties

# http port
EXPOSE 8080
# JVM debug port

# Enable JVM debug port
EXPOSE 8880
ENV JAVA_TOOL_OPTIONS -Xdebug -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8880

WORKDIR /opt

# Start with debugging support
ENTRYPOINT ["/bin/java", "-Xdebug", "-Xrunjdwp:transport=dt_socket,server=y,address=8880,suspend=n", "-jar", "/opt/sabi-service-1.0-SNAPSHOT.jar"]
# Start our app
ENTRYPOINT ["/bin/java", "-jar", "/opt/sabi-service-1.0-SNAPSHOT.jar"]

Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ protected void doFilterInternal(HttpServletRequest req,

if (authentication != null) {
SecurityContextHolder.getContext().setAuthentication(authentication);
// NOTICE we also generate a new Token to renew tokens TTL.
// This is necessary to extend the "backend-session" if users-client session continues.
// it's in the responsibility of the client to continue with the renewed token (the old token will be still
// valid until its TTL expired - for the cases where the client missed the cycle due to network problems.)
String userID = (String) authentication.getPrincipal();
TokenAuthenticationService.addAuthentication(res,userID);
chain.doFilter(req, res);
} else {
// don't continue the chain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ static Authentication getAuthentication(HttpServletRequest pRequest) {
* @return users identified by his email or null in case the token was not valid.
*/
public static String extractUserFromToken(String token) {
String user = null;
String userID = null;
try {
user = Jwts.parser()
userID = Jwts.parser()
.setSigningKey(SECRET)
.parseClaimsJws(token.replace(HttpHeader.TOKEN_PREFIX, ""))
.getBody()
Expand All @@ -88,7 +88,7 @@ public static String extractUserFromToken(String token) {
System.out.println("Token was: "+token);
e.printStackTrace();
}
return user;
return userID;
}

// --------------------------- CONSTRUCTORS ---------------------------
Expand Down
2 changes: 1 addition & 1 deletion sabi-server/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ spring:
# for local development I recommend to use fakeSMTP (under linux) or smtp4dev (under windows)
mail:
host: localhost
username: sabi@bluewhale.de
password: YouWillNeverKnow
port: 2525
properties:
Expand All @@ -90,4 +91,3 @@ spring:
auth: true
starttls:
enable: true
username: no-reply@sabi-project.net
Original file line number Diff line number Diff line change
Expand Up @@ -374,5 +374,4 @@ public void testUpdateMeasurement() throws Exception {
assertThat("Value not updated?", returnedMeasurement.getMeasuredValue() == updatedMeasurementEntity.getMeasuredValue());
}


}
2 changes: 1 addition & 1 deletion sabi-webclient/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>de.bluewhale</groupId>
<artifactId>sabi-webclient</artifactId>
<version>0.8.7-SNAPSHOT</version>
<version>0.8.8-SNAPSHOT</version>
<packaging>jar</packaging>
<name>sabi-webclient</name>
<description>A JSF based webclient for sabi.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import de.bluewhale.sabi.model.MeasurementTo;
import de.bluewhale.sabi.model.ParameterTo;
import de.bluewhale.sabi.model.UnitTo;
import de.bluewhale.sabi.webclient.CDIBeans.UserSession;
import de.bluewhale.sabi.webclient.rest.exceptions.MeasurementMessageCodes;
import de.bluewhale.sabi.webclient.utils.RestHelper;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -52,6 +53,9 @@ public class MeasurementServiceImpl implements MeasurementService {
@Autowired
private ObjectMapper objectMapper; // json mapper

@Autowired
private UserSession userSession;

@Override
public @NotNull List<UnitTo> getAvailableMeasurementUnits(@NotNull String JWTBackendAuthtoken) throws BusinessException {

Expand All @@ -68,6 +72,7 @@ public class MeasurementServiceImpl implements MeasurementService {
try {
// Notice the that the controller defines a list, the resttemplate will get it as array.
responseEntity = restTemplate.exchange(listMeasurementUnitsUri, HttpMethod.GET, requestEntity, String.class);
renewBackendToken(responseEntity);
} catch (RestClientException e) {
log.error(String.format("Couldn't reach %s", listMeasurementUnitsUri), e.getLocalizedMessage());
e.printStackTrace();
Expand Down Expand Up @@ -106,6 +111,7 @@ public ParameterTo getParameterFor(Integer selectedUnitId, String JWTBackendAuth
try {
// Notice the that the controller defines a list, the resttemplate will get it as array.
responseEntity = restTemplate.exchange(listUnitsParameterUri, HttpMethod.GET, requestEntity, String.class);
renewBackendToken(responseEntity);
} catch (RestClientException e) {
log.error(String.format("Couldn't reach %s", listUnitsParameterUri), e.getLocalizedMessage());
e.printStackTrace();
Expand Down Expand Up @@ -142,6 +148,7 @@ public ParameterTo getParameterFor(Integer selectedUnitId, String JWTBackendAuth
try {
// Notice the that the controller defines a list, the resttemplate will get it as array.
responseEntity = restTemplate.exchange(listOfUsersMeasurements, HttpMethod.GET, requestEntity, String.class);
renewBackendToken(responseEntity);
} catch (RestClientException e) {
log.error(String.format("Couldn't reach %s", listOfUsersMeasurements), e.getLocalizedMessage());
e.printStackTrace();
Expand Down Expand Up @@ -180,6 +187,7 @@ public List<MeasurementTo> getMeasurementsForUsersTankFilteredByUnit(String JWTB
try {
// Notice the that the controller defines a list, the resttemplate will get it as array.
responseEntity = restTemplate.exchange(apiURL, HttpMethod.GET, requestEntity, String.class);
renewBackendToken(responseEntity);
} catch (RestClientException e) {
log.error("Couldn't reach {} Reason: {}", apiURL, e.getLocalizedMessage());
e.printStackTrace();
Expand Down Expand Up @@ -224,6 +232,7 @@ public void save(MeasurementTo measurement, String JWTBackendAuthtoken) throws B
// initial creation (POST)
try {
responseEntity = restTemplate.postForEntity(saveMeasurmentURI, requestEntity, String.class);
renewBackendToken(responseEntity);
} catch (RestClientException e) {
log.error("Couldn't reach backend {} to add a measurement reason was {}", saveMeasurmentURI, e.getLocalizedMessage());
throw BusinessException.with(CommonMessageCodes.NETWORK_PROBLEM);
Expand All @@ -245,6 +254,7 @@ public void save(MeasurementTo measurement, String JWTBackendAuthtoken) throws B
// update case (PUT)
try {
responseEntity = restTemplate.exchange(saveMeasurmentURI, HttpMethod.PUT, requestEntity, String.class);
renewBackendToken(responseEntity);
} catch (RestClientException e) {
log.error("Couldn't backend {} to update a measurement reason was {}", saveMeasurmentURI, e.getLocalizedMessage());
throw BusinessException.with(CommonMessageCodes.NETWORK_PROBLEM);
Expand All @@ -261,4 +271,11 @@ public void save(MeasurementTo measurement, String JWTBackendAuthtoken) throws B
}
}
}

private void renewBackendToken(ResponseEntity<String> responseEntity) {
if( responseEntity.getHeaders().containsKey(HttpHeaders.AUTHORIZATION) ) {
userSession.setSabiBackendToken(responseEntity.getHeaders().getFirst(HttpHeaders.AUTHORIZATION));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import de.bluewhale.sabi.exception.CommonExceptionCodes;
import de.bluewhale.sabi.exception.Message;
import de.bluewhale.sabi.model.AquariumTo;
import de.bluewhale.sabi.webclient.CDIBeans.UserSession;
import de.bluewhale.sabi.webclient.rest.exceptions.TankMessageCodes;
import de.bluewhale.sabi.webclient.utils.RestHelper;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -47,6 +48,8 @@ public class TankServiceImpl implements TankService {
@Autowired
private ObjectMapper objectMapper; // json mapper

@Autowired
private UserSession userSession;

@Override
public @NotNull List<AquariumTo> getUsersTanks(@NotNull String JWTBackendAuthtoken) throws BusinessException {
Expand All @@ -62,6 +65,7 @@ public class TankServiceImpl implements TankService {

try {
responseEntity = restTemplate.exchange(listTankUri, HttpMethod.GET, requestEntity, String.class);
renewBackendToken(responseEntity);
} catch (RestClientException e) {
log.error(String.format("Couldn't reach %s",listTankUri),e.getLocalizedMessage());
e.printStackTrace();
Expand All @@ -76,11 +80,10 @@ public class TankServiceImpl implements TankService {
e.printStackTrace();
throw new BusinessException(CommonExceptionCodes.INTERNAL_ERROR);
}

return tankList;

}


@Override
public void deleteTankById(@NotNull Long tankId, @NotNull String JWTBackendAuthtoken) throws BusinessException {
String tankUri = sabiBackendUrl + "/api/tank/"+tankId;
Expand All @@ -93,6 +96,7 @@ public void deleteTankById(@NotNull Long tankId, @NotNull String JWTBackendAutht

try {
responseEntity = restTemplate.exchange(tankUri, HttpMethod.DELETE, requestEntity, String.class);
renewBackendToken(responseEntity);
} catch (RestClientException e) {
log.error(String.format("Couldn't reach %s",tankUri),e.getLocalizedMessage());
throw new BusinessException(CommonExceptionCodes.NETWORK_ERROR);
Expand Down Expand Up @@ -135,6 +139,7 @@ public void save(AquariumTo tank, String JWTBackendAuthtoken) throws BusinessExc
// Save case
try {
responseEntity = restTemplate.postForEntity(createTankURI, requestEntity, String.class);
renewBackendToken(responseEntity);
} catch (RestClientException e) {
log.error(String.format("Couldn't reach %s",createTankURI),e.getLocalizedMessage());
throw new BusinessException(CommonExceptionCodes.NETWORK_ERROR);
Expand All @@ -153,6 +158,7 @@ public void save(AquariumTo tank, String JWTBackendAuthtoken) throws BusinessExc
// update case
try {
responseEntity = restTemplate.exchange(updateTankURI, HttpMethod.PUT, requestEntity, String.class);
renewBackendToken(responseEntity);
} catch (RestClientException e) {
log.error(String.format("Couldn't reach %s",updateTankURI),e.getLocalizedMessage());
throw new BusinessException(CommonExceptionCodes.NETWORK_ERROR);
Expand All @@ -171,4 +177,10 @@ public void save(AquariumTo tank, String JWTBackendAuthtoken) throws BusinessExc

}
}

private void renewBackendToken(ResponseEntity<String> responseEntity) {
if( responseEntity.getHeaders().containsKey(HttpHeaders.AUTHORIZATION) ) {
userSession.setSabiBackendToken(responseEntity.getHeaders().getFirst(HttpHeaders.AUTHORIZATION));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ private UserProfileTo requestUserProfile(String sabiBackendToken) {

try {
responseEntity = restTemplate.exchange(userProfileURL, HttpMethod.GET, requestEntity, String.class);
renewBackendToken(responseEntity);
userProfileTo = objectMapper.readValue(responseEntity.getBody(), UserProfileTo.class);

} catch (Exception e) {
Expand Down Expand Up @@ -185,6 +186,7 @@ public void updateUsersProfile(@NotNull UserProfileTo pUserProfile, @NotNull Str

RestTemplate restTemplate = new RestTemplate();
responseEntity = restTemplate.exchange(updateUserProfileURL, HttpMethod.PUT, requestEntity, String.class);
renewBackendToken(responseEntity);

} catch (RestClientException e) {
log.error("Couldn't reach {} reason {}", updateUserProfileURL, e.getMessage());
Expand All @@ -198,4 +200,11 @@ public void updateUsersProfile(@NotNull UserProfileTo pUserProfile, @NotNull Str


}

private void renewBackendToken(ResponseEntity<String> responseEntity) {
if( responseEntity.getHeaders().containsKey(HttpHeaders.AUTHORIZATION) ) {
userSession.setSabiBackendToken(responseEntity.getHeaders().getFirst(HttpHeaders.AUTHORIZATION));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static HttpHeaders buildHttpHeader() {
*/
public static HttpHeaders prepareAuthedHttpHeader(String JWTBackendAuthtoken) {
HttpHeaders headers = buildHttpHeader();
headers.add(AUTH_TOKEN, TOKEN_PREFIX + JWTBackendAuthtoken);
headers.add(AUTH_TOKEN, JWTBackendAuthtoken);
return headers;
}

Expand Down