SUPPORT-8407: Fix

This commit is contained in:
Eduard Tihomirov 2024-08-23 14:49:08 +03:00
parent 78bffeeb05
commit 679a3e1ce5
3 changed files with 33 additions and 4 deletions

View file

@ -24,7 +24,6 @@ import esia.config.FormUrlencoded;
import esia.model.EmployeeModel; import esia.model.EmployeeModel;
import esia.model.EsiaTokenResponse; import esia.model.EsiaTokenResponse;
import esia.model.OrganizationModel; import esia.model.OrganizationModel;
import esia.model.PersonModel;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -47,7 +46,7 @@ public class EsiaAuthService {
private EsiaConfig esiaConfig; private EsiaConfig esiaConfig;
@Autowired @Autowired
private UlDataService personalDataService; private UlDataService ulDataService;
public String generateAuthCodeUrl() { public String generateAuthCodeUrl() {
try { try {
@ -175,6 +174,10 @@ public class EsiaAuthService {
throw new RuntimeException(tokenResponse.getError_description()); throw new RuntimeException(tokenResponse.getError_description());
} }
String accessToken = tokenResponse.getAccess_token(); String accessToken = tokenResponse.getAccess_token();
boolean hasRole = ulDataService.checkRole(accessToken);
if (!hasRole) {
throw new RuntimeException("The user does not have the required role");
}
Cookie cookie = new Cookie("access_token", accessToken); Cookie cookie = new Cookie("access_token", accessToken);
cookie.setHttpOnly(true); cookie.setHttpOnly(true);
cookie.setSecure(true); cookie.setSecure(true);
@ -192,8 +195,8 @@ public class EsiaAuthService {
isAuthToken.setPath("/"); isAuthToken.setPath("/");
response.addCookie(isAuthToken); response.addCookie(isAuthToken);
EmployeeModel employeeModel = personalDataService.getPersonModel(accessToken); EmployeeModel employeeModel = ulDataService.getPersonModel(accessToken);
OrganizationModel organizationModel = personalDataService.getOrganizationModel(accessToken); OrganizationModel organizationModel = ulDataService.getOrganizationModel(accessToken);
return true; return true;
} }
catch (Exception e) { catch (Exception e) {

View file

@ -9,6 +9,8 @@ import esia.model.PersonModel;
*/ */
public interface UlDataService { public interface UlDataService {
boolean checkRole(String accessToken);
EmployeeModel getPersonModel(String accessToken); EmployeeModel getPersonModel(String accessToken);
PersonModel getChiefPersonModel(String accessToken); PersonModel getChiefPersonModel(String accessToken);

View file

@ -155,4 +155,28 @@ public class UlDataServiceImpl implements UlDataService {
} }
} }
@Override
public boolean checkRole(String accessToken) {
try {
EsiaAccessToken esiaAccessToken = readToken(accessToken);
String prsnId = esiaAccessToken.getSbj_id();
String url = esiaConfig.getEsiaBaseUri() + "rs/orgs/" + prsnId + "/grps?embed=(elements)";
HttpRequest getReq = HttpRequest.newBuilder(URI.create(url))
.header(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded")
.header("Authorization", "Bearer ".concat(accessToken))
.GET()
.timeout(Duration.ofSeconds(60))
.build();
HttpResponse<String> getResp = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(30))
.build()
.send(getReq, HttpResponse.BodyHandlers.ofString());
errorHandler(getResp);
}
catch (Exception e) {
throw new RuntimeException(e);
}
return false;
}
} }