Merge branch 'refs/heads/feature/SUPPORT-8725_refactor' into develop

This commit is contained in:
adel.ka 2024-12-17 16:05:09 +03:00
commit 58a9af8509
12 changed files with 165 additions and 84 deletions

View file

@ -11,6 +11,7 @@ import ru.micord.ervu.dto.SubpoenaResponseDto;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RestController;
import proto.ervu.rp.summons.SummonsResponseData;
import ru.micord.ervu.exception.ProtobufParsingException;
import ru.micord.ervu.kafka.service.ReplyingKafkaService;
import ru.micord.ervu.security.webbpm.jwt.util.SecurityUtil;
@ -54,7 +55,7 @@ public class ErvuDataController {
return converter.convert(responseData);
}
catch (InvalidProtocolBufferException e) {
throw new RuntimeException("Failed to parse data", e);
throw new ProtobufParsingException("Failed to parse data", e);
}
}
}

View file

@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.RestController;
import rtl.pgs.ervu.proto.ExtractRegistry;
import rtl.pgs.ervu.proto.ResponseData;
import ru.micord.ervu.dto.ExtractRequestDto;
import ru.micord.ervu.exception.ProtobufParsingException;
import ru.micord.ervu.kafka.service.ReplyingKafkaService;
import ru.micord.ervu.security.webbpm.jwt.util.SecurityUtil;
@ -60,7 +61,7 @@ public class ExtractController {
.body(resource);
}
catch (InvalidProtocolBufferException e) {
throw new RuntimeException("Failed to parse data", e);
throw new ProtobufParsingException("Failed to parse data", e);
}
}
}

View file

@ -0,0 +1,19 @@
package ru.micord.ervu.exception;
/**
* @author Adel Kalimullin
*/
public class ProtobufParsingException extends RuntimeException{
public ProtobufParsingException(String message) {
super(message);
}
public ProtobufParsingException(String message, Throwable cause) {
super(message, cause);
}
public ProtobufParsingException(Throwable cause) {
super(cause);
}
}

View file

@ -0,0 +1,19 @@
package ru.micord.ervu.kafka.exception;
/**
* @author Adel Kalimullin
*/
public class KafkaMessageException extends RuntimeException {
public KafkaMessageException(String message) {
super(message);
}
public KafkaMessageException(String message, Throwable cause) {
super(message, cause);
}
public KafkaMessageException(Throwable cause) {
super(cause);
}
}

View file

@ -7,6 +7,7 @@ import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.kafka.requestreply.ReplyingKafkaTemplate;
import org.springframework.kafka.requestreply.RequestReplyFuture;
import ru.micord.ervu.kafka.exception.KafkaMessageException;
import ru.micord.ervu.kafka.service.ReplyingKafkaService;
/**
@ -22,10 +23,10 @@ public abstract class BaseReplyingKafkaService<T, V> implements ReplyingKafkaSer
try {
return Optional.ofNullable(replyFuture.get())
.map(ConsumerRecord::value)
.orElseThrow(() -> new RuntimeException("Kafka return result is null"));
.orElseThrow(() -> new KafkaMessageException("Kafka return result is null"));
}
catch (InterruptedException | ExecutionException e) {
throw new RuntimeException("Failed to get kafka response", e);
throw new KafkaMessageException("Failed to get kafka response", e);
}
}
protected abstract ReplyingKafkaTemplate<String, T, V> getTemplate();

View file

@ -0,0 +1,19 @@
package ru.micord.ervu.security.esia.exception;
/**
* @author Adel Kalimullin
*/
public class EsiaException extends RuntimeException{
public EsiaException(String message) {
super(message);
}
public EsiaException(String message, Throwable cause) {
super(message, cause);
}
public EsiaException(Throwable cause) {
super(cause);
}
}

View file

@ -25,9 +25,9 @@ public class EsiaAccessToken implements Serializable {
private String sid;
@JsonProperty("urn:esia:sbj_id")
private String sbj_id;
private String client_id;
private String sbjId;
@JsonProperty("client_id")
private String clientId;
private Long iat;
@ -71,20 +71,20 @@ public class EsiaAccessToken implements Serializable {
this.sid = sid;
}
public String getSbj_id() {
return sbj_id;
public String getSbjId() {
return sbjId;
}
public void setSbj_id(String sbj_id) {
this.sbj_id = sbj_id;
public void setSbjId(String sbjId) {
this.sbjId = sbjId;
}
public String getClient_id() {
return client_id;
public String getClientId() {
return clientId;
}
public void setClient_id(String client_id) {
this.client_id = client_id;
public void setClientId(String clientId) {
this.clientId = clientId;
}
public Long getIat() {

View file

@ -3,6 +3,7 @@ package ru.micord.ervu.security.esia.model;
import java.io.Serializable;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* @author Eduard Tihomirov
@ -12,44 +13,46 @@ public class EsiaTokenResponse implements Serializable {
private static final long serialVersionUID = 7655328287602576975L;
private String id_token;
private String access_token;
private String refresh_token;
@JsonProperty("id_token")
private String tokenId;
@JsonProperty("access_token")
private String accessToken;
@JsonProperty("refresh_token")
private String refreshToken;
private String state;
private String token_type;
private Long expires_in;
@JsonProperty("token_type")
private String tokenType;
@JsonProperty("expires_in")
private Long expiresIn;
private String error;
private String error_description;
@JsonProperty("error_description")
private String errorDescription;
public String getId_token() {
return id_token;
public String getTokenId() {
return tokenId;
}
public void setId_token(String id_token) {
this.id_token = id_token;
public void setTokenId(String tokenId) {
this.tokenId = tokenId;
}
public String getAccess_token() {
return access_token;
public String getAccessToken() {
return accessToken;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public String getRefresh_token() {
return refresh_token;
public String getRefreshToken() {
return refreshToken;
}
public void setRefresh_token(String refresh_token) {
this.refresh_token = refresh_token;
public void setRefreshToken(String refreshToken) {
this.refreshToken = refreshToken;
}
public String getState() {
@ -60,20 +63,20 @@ public class EsiaTokenResponse implements Serializable {
this.state = state;
}
public String getToken_type() {
return token_type;
public String getTokenType() {
return tokenType;
}
public void setToken_type(String token_type) {
this.token_type = token_type;
public void setTokenType(String tokenType) {
this.tokenType = tokenType;
}
public Long getExpires_in() {
return expires_in;
public Long getExpiresIn() {
return expiresIn;
}
public void setExpires_in(Long expires_in) {
this.expires_in = expires_in;
public void setExpiresIn(Long expiresIn) {
this.expiresIn = expiresIn;
}
public String getError() {
@ -84,11 +87,11 @@ public class EsiaTokenResponse implements Serializable {
this.error = error;
}
public String getError_description() {
return error_description;
public String getErrorDescription() {
return errorDescription;
}
public void setError_description(String error_description) {
this.error_description = error_description;
public void setErrorDescription(String errorDescription) {
this.errorDescription = errorDescription;
}
}

View file

@ -10,9 +10,6 @@ import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
@ -31,8 +28,8 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContext;
import ru.micord.ervu.security.esia.exception.EsiaException;
import ru.micord.ervu.kafka.model.Document;
import ru.micord.ervu.kafka.model.ErrorData;
import ru.micord.ervu.kafka.model.Person;
import ru.micord.ervu.kafka.model.Response;
import ru.micord.ervu.kafka.service.ReplyingKafkaService;
@ -126,7 +123,7 @@ public class EsiaAuthService {
return makeRequest(url, params);
}
catch (Exception e) {
throw new RuntimeException(e);
throw new EsiaException(e);
}
}
@ -146,7 +143,7 @@ public class EsiaAuthService {
return URLEncoder.encode(s, "UTF-8")
.replace("+", "%20");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
throw new EsiaException(e);
}
}
@ -218,21 +215,21 @@ public ResponseEntity<?> getEsiaTokensByCode(String esiaAuthCode, String error,
}
if (tokenResponse.getError() != null) {
throw new RuntimeException(tokenResponse.getError_description());
throw new EsiaException(tokenResponse.getErrorDescription());
}
String accessToken = tokenResponse.getAccess_token();
String accessToken = tokenResponse.getAccessToken();
String verifyResult = verifyToken(accessToken);
if (verifyResult != null) {
throw new RuntimeException(verifyResult);
throw new EsiaException(verifyResult);
}
String refreshToken = tokenResponse.getRefresh_token();
String refreshToken = tokenResponse.getRefreshToken();
EsiaAccessToken esiaAccessToken = personalDataService.readToken(accessToken);
String prnOid = esiaAccessToken.getSbj_id();
Long expiresIn = tokenResponse.getExpires_in();
String prnOid = esiaAccessToken.getSbjId();
Long expiresIn = tokenResponse.getExpiresIn();
TokensStore.addAccessToken(prnOid, accessToken, expiresIn);
TokensStore.addRefreshToken(prnOid, refreshToken, expiresIn);
Response ervuIdResponse = getErvuIdResponse(accessToken);
createTokenAndAddCookie(response, esiaAccessToken.getSbj_id(), ervuIdResponse.getErvuId(), expiresIn);
createTokenAndAddCookie(response, esiaAccessToken.getSbjId(), ervuIdResponse.getErvuId(), expiresIn);
if (ervuIdResponse.getErrorData() != null) {
return new ResponseEntity<>(
"Доступ запрещен. " + ervuIdResponse.getErrorData().getName(),
@ -297,24 +294,24 @@ public ResponseEntity<?> getEsiaTokensByCode(String esiaAuthCode, String error,
String responseString = postResp.body();
EsiaTokenResponse tokenResponse = objectMapper.readValue(responseString, EsiaTokenResponse.class);
if (tokenResponse != null && tokenResponse.getError() != null) {
throw new RuntimeException(tokenResponse.getError_description());
throw new EsiaException(tokenResponse.getErrorDescription());
}
String accessToken = tokenResponse.getAccess_token();
String accessToken = tokenResponse.getAccessToken();
String verifyResult = verifyToken(accessToken);
if (verifyResult != null) {
throw new RuntimeException(verifyResult);
throw new EsiaException(verifyResult);
}
String newRefreshToken = tokenResponse.getRefresh_token();
String newRefreshToken = tokenResponse.getRefreshToken();
EsiaAccessToken esiaAccessToken = personalDataService.readToken(accessToken);
String prnOid = esiaAccessToken.getSbj_id();
Long expiresIn = tokenResponse.getExpires_in();
String prnOid = esiaAccessToken.getSbjId();
Long expiresIn = tokenResponse.getExpiresIn();
TokensStore.addAccessToken(prnOid, accessToken, expiresIn);
TokensStore.addRefreshToken(prnOid, newRefreshToken, expiresIn);
Response ervuIdResponse = getErvuIdResponse(accessToken);
createTokenAndAddCookie(response, esiaAccessToken.getSbj_id(), ervuIdResponse.getErvuId(), expiresIn);
createTokenAndAddCookie(response, esiaAccessToken.getSbjId(), ervuIdResponse.getErvuId(), expiresIn);
}
catch (Exception e) {
throw new RuntimeException(e);
throw new EsiaException(e);
}
}
@ -340,13 +337,13 @@ public ResponseEntity<?> getEsiaTokensByCode(String esiaAuthCode, String error,
}
catch (Exception e) {
throw new RuntimeException(e);
throw new EsiaException(e);
}
}
private void errorHandler(HttpResponse httpResponse) {
if (httpResponse.statusCode() != 200) {
throw new RuntimeException(httpResponse.statusCode() + " " + httpResponse.body());
throw new EsiaException(httpResponse.statusCode() + " " + httpResponse.body());
}
}
@ -365,7 +362,7 @@ public ResponseEntity<?> getEsiaTokensByCode(String esiaAuthCode, String error,
return makeRequest(url, params);
}
catch (Exception e) {
throw new RuntimeException(e);
throw new EsiaException(e);
}
}
@ -379,7 +376,7 @@ public ResponseEntity<?> getEsiaTokensByCode(String esiaAuthCode, String error,
return objectMapper.readValue(kafkaResponse, Response.class);
}
catch (Exception e) {
throw new RuntimeException(e);
throw new EsiaException(e);
}
}
@ -430,8 +427,8 @@ public ResponseEntity<?> getEsiaTokensByCode(String esiaAuthCode, String error,
if (!esiaHeader.getTyp().equals("JWT")) {
return "Token invalid. Token type: " + esiaHeader.getTyp() + " invalid";
}
if (!esiaAccessToken.getClient_id().equals(esiaConfig.getClientId())) {
return "Token invalid. Token clientId: " + esiaAccessToken.getClient_id() + " invalid";
if (!esiaAccessToken.getClientId().equals(esiaConfig.getClientId())) {
return "Token invalid. Token clientId: " + esiaAccessToken.getClientId() + " invalid";
}
if (!esiaAccessToken.getIss().equals(esiaConfig.getEsiaIssuerUrl())) {
return "Token invalid. Token issuer:" + esiaAccessToken.getIss() + " invalid";
@ -471,7 +468,7 @@ public ResponseEntity<?> getEsiaTokensByCode(String esiaAuthCode, String error,
return response;
}
catch (Exception e) {
throw new RuntimeException(e);
throw new EsiaException(e);
}
}
}

View file

@ -8,6 +8,7 @@ import java.time.Duration;
import java.util.Base64;
import com.fasterxml.jackson.databind.ObjectMapper;
import ru.micord.ervu.security.esia.exception.EsiaException;
import ru.micord.ervu.security.esia.config.EsiaConfig;
import ru.micord.ervu.security.esia.model.EsiaAccessToken;
import ru.micord.ervu.security.esia.model.EsiaHeader;
@ -33,7 +34,7 @@ public class EsiaPersonalDataService implements PersonalDataService {
public PersonModel getPersonModel(String accessToken) {
try {
EsiaAccessToken esiaAccessToken = readToken(accessToken);
String prnsId = esiaAccessToken.getSbj_id();
String prnsId = esiaAccessToken.getSbjId();
PersonModel personModel = getPersonData(prnsId, accessToken);
personModel.setPassportModel(
getPassportModel(prnsId, accessToken, personModel.getrIdDoc()));
@ -41,7 +42,7 @@ public class EsiaPersonalDataService implements PersonalDataService {
return personModel;
}
catch (Exception e) {
throw new RuntimeException(e);
throw new EsiaException(e);
}
}
@ -62,7 +63,7 @@ public class EsiaPersonalDataService implements PersonalDataService {
return objectMapper.readValue(getRespDoc.body(), PassportModel.class);
}
catch (Exception e) {
throw new RuntimeException(e);
throw new EsiaException(e);
}
}
@ -82,7 +83,7 @@ public class EsiaPersonalDataService implements PersonalDataService {
return objectMapper.readValue(getResp.body(), PersonModel.class);
}
catch (Exception e) {
throw new RuntimeException(e);
throw new EsiaException(e);
}
}
@ -101,7 +102,7 @@ public class EsiaPersonalDataService implements PersonalDataService {
return esiaAccessToken;
}
catch (Exception e) {
throw new RuntimeException(e);
throw new EsiaException(e);
}
}
@ -120,7 +121,7 @@ public class EsiaPersonalDataService implements PersonalDataService {
return esiaHeader;
}
catch (Exception e) {
throw new RuntimeException(e);
throw new EsiaException(e);
}
}
}

View file

@ -0,0 +1,19 @@
package ru.micord.ervu.security.exception;
/**
* @author Adel Kalimullin
*/
public class UnauthorizedException extends RuntimeException{
public UnauthorizedException(String message) {
super(message);
}
public UnauthorizedException(String message, Throwable cause) {
super(message, cause);
}
public UnauthorizedException(Throwable cause) {
super(cause);
}
}

View file

@ -15,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import ru.micord.ervu.security.esia.token.TokensStore;
import ru.micord.ervu.security.exception.UnauthorizedException;
import ru.micord.ervu.security.webbpm.jwt.model.Token;
import ru.cg.webbpm.modules.resources.api.ResourceMetadataUtils;
@ -100,7 +101,7 @@ public class JwtTokenService {
return ids[0];
}
else {
throw new RuntimeException("Failed to get auth data. User unauthorized.");
throw new UnauthorizedException("Failed to get auth data. User unauthorized.");
}
}
}