Commit 4e5edcde authored by shishir suman's avatar shishir suman
Browse files

code changes for logout

No related merge requests found
Showing with 72 additions and 1 deletion
+72 -1
......@@ -124,4 +124,13 @@ public class UserController {
public ResponseEntity<String> getUserByAttribute(@RequestBody JsonNode body) throws SQLException, IOException, URISyntaxException {
return userHandler.getUserByAttribute(body);
}
@GetMapping(value = "/{userId}/logout", produces = "application/json")
public ResponseEntity<?> logout(@PathVariable String userId) {
try {
return userHandler.logout(userId);
} catch (IOException e) {
throw new RuntimeException("Error in terminating session");
}
}
}
package com.tarento.upsmf.userManagement.exception;
import com.tarento.upsmf.userManagement.utility.ErrorCode;
public class LogoutFailedException extends CustomException {
public LogoutFailedException(String message) {
super(message);
}
public LogoutFailedException(String message, String description) {
super(message, description);
}
public LogoutFailedException(String message, ErrorCode errorCode) {
super(message, errorCode);
}
public LogoutFailedException(String message, ErrorCode errorCode, String description) {
super(message, errorCode, description);
}
}
......@@ -196,4 +196,8 @@ public class UserHandler {
public ResponseEntity<String> getUserByAttribute(JsonNode body) throws SQLException, IOException, URISyntaxException {
return userService.getUserListByAttribute(body);
}
public ResponseEntity<String> logout(String userId) throws IOException {
return userService.logout(userId);
}
}
......@@ -294,4 +294,7 @@ public class UserService {
return result;
}
public ResponseEntity<String> logout(String userId) throws IOException {
return keycloakUserCredentialPersister.usrLogout(userId);
}
}
\ No newline at end of file
......@@ -9,6 +9,7 @@ public enum ErrorCode {
RC_UM_101("Token Inaccessibility - RC User Management"),
RC_UM_201("Invalid OTP - RC User Management"),
RC_UM_301("Login failed - RC User Management"),
RC_UM_302("Error in terminating user session"),
RC_UM_0("Undefined - RC User Management"),
CE_UM_001("User creation failed - Central User Management"),
......
......@@ -2,8 +2,10 @@ package com.tarento.upsmf.userManagement.utility;
import com.fasterxml.jackson.databind.JsonNode;
import com.tarento.upsmf.userManagement.exception.LoginFailedException;
import com.tarento.upsmf.userManagement.exception.LogoutFailedException;
import org.apache.http.HttpHeaders;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
......@@ -13,11 +15,11 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.UUID;
@Component
@PropertySource({ "classpath:application.properties" })
......@@ -44,6 +46,8 @@ public class KeycloakUserCredentialPersister {
private String USER_LOGIN;
private String USER_LOGOUT;
@PostConstruct
public void init(){
environment = env;
......@@ -51,6 +55,7 @@ public class KeycloakUserCredentialPersister {
OTP_MAIL_ENDPOINT = getPropertyValue("otp.mail.endpoint");
USER_CREATE_MAIL_ENDPOINT = getPropertyValue("user.create.mail.endpoint");
USER_LOGIN = getPropertyValue("user.login");
USER_LOGOUT = getPropertyValue("user.logout");
}
public static String getPropertyValue(String property){
......@@ -146,4 +151,30 @@ public class KeycloakUserCredentialPersister {
}
}
public ResponseEntity<String> usrLogout(String userId) {
try {
logger.info("login user endpoint {}. ", USER_LOGIN);
HttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(USER_LOGOUT.concat("/").concat(userId));
JsonNode adminToken = sunbirdRCKeycloakTokenRetriever.getAdminToken();
String authToken = adminToken.get("access_token").asText();
httpGet.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
httpGet.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + authToken);
logger.info("payload logout user with header {}", httpGet);
org.apache.http.HttpResponse response = httpClient.execute(httpGet);
logger.info("Response from server {}", response);
String responseBody = EntityUtils.toString(response.getEntity());
if (response.getStatusLine().getStatusCode() == 500) {
logger.error("Error while trying to logout in RC User Management -- code -- 500");
throw new LogoutFailedException("Error in terminating session", ErrorCode.RC_UM_302,
responseBody);
}
return ResponseEntity.ok(responseBody);
} catch (Exception e) {
logger.error("Error while terminating session");
throw new LogoutFailedException("Error while terminating session", ErrorCode.RC_UM_302, e.getMessage());
}
}
}
......@@ -35,4 +35,6 @@ spring.jpa.hibernate.ddl-auto=none
otp.mail.endpoint = /api/v1/keycloak/mail/sendOTP
user.create.mail.endpoint =/api/v1/keycloak/mail/userCreate
user.login =/api/v1/login
user.logout =/api/v1/logout
aes_key_for_payment_success =XXXXXXXXXXXXXXXXX
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment