feature/implement-external-login-api #30
@@ -5,6 +5,8 @@ import com.vaessl.app.dto.ConnectionResponse;
|
|||||||
|
|
||||||
public interface ConnectionProvider {
|
public interface ConnectionProvider {
|
||||||
|
|
||||||
|
void checkCredentials(ConnectionRequest request);
|
||||||
|
|
||||||
String getServiceType();
|
String getServiceType();
|
||||||
|
|
||||||
ConnectionResponse authenticate(ConnectionRequest request);
|
ConnectionResponse authenticate(ConnectionRequest request);
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import org.springframework.stereotype.Service;
|
|||||||
|
|
||||||
import com.vaessl.app.dto.ConnectionRequest;
|
import com.vaessl.app.dto.ConnectionRequest;
|
||||||
import com.vaessl.app.dto.ConnectionResponse;
|
import com.vaessl.app.dto.ConnectionResponse;
|
||||||
import com.vaessl.app.exception.ProviderNotFoundException;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class ConnectionService {
|
public class ConnectionService {
|
||||||
@@ -27,9 +26,7 @@ public class ConnectionService {
|
|||||||
|
|
||||||
ConnectionProvider provider = providerRegistry.get(request.serviceType());
|
ConnectionProvider provider = providerRegistry.get(request.serviceType());
|
||||||
|
|
||||||
if (provider == null) {
|
provider.checkCredentials(request);
|
||||||
throw new ProviderNotFoundException();
|
|
||||||
}
|
|
||||||
|
|
||||||
ConnectionResponse response = provider.authenticate(request);
|
ConnectionResponse response = provider.authenticate(request);
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package com.vaessl.app.connection;
|
package com.vaessl.app.connection;
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@@ -9,6 +11,7 @@ import org.springframework.web.client.RestClient;
|
|||||||
|
|
||||||
import com.vaessl.app.dto.ConnectionRequest;
|
import com.vaessl.app.dto.ConnectionRequest;
|
||||||
import com.vaessl.app.dto.ConnectionResponse;
|
import com.vaessl.app.dto.ConnectionResponse;
|
||||||
|
import com.vaessl.app.exception.EmptyCredentialsException;
|
||||||
|
|
||||||
import static com.vaessl.app.connection.Endpoint.*;
|
import static com.vaessl.app.connection.Endpoint.*;
|
||||||
|
|
||||||
@@ -24,6 +27,22 @@ public class HomeBoxConnectionProvider implements ConnectionProvider {
|
|||||||
this.cRepository = cRepository;
|
this.cRepository = cRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void checkCredentials(ConnectionRequest request) {
|
||||||
|
if (request.username() == null || request.password() == null) {
|
||||||
|
List<String> missingFields = new ArrayList<>();
|
||||||
|
|
||||||
|
if (request.username() == null) {
|
||||||
|
missingFields.add("username");
|
||||||
|
}
|
||||||
|
if (request.password() == null) {
|
||||||
|
missingFields.add("password");
|
||||||
|
|
||||||
|
}
|
||||||
|
throw new EmptyCredentialsException(List.copyOf(missingFields));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getServiceType() {
|
public String getServiceType() {
|
||||||
return "HOMEBOX";
|
return "HOMEBOX";
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.vaessl.app.exception;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class EmptyCredentialsException extends RuntimeException {
|
||||||
|
|
||||||
|
private final List<String> missingFields;
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.vaessl.app.exception;
|
package com.vaessl.app.exception;
|
||||||
|
|
||||||
import org.springframework.http.ProblemDetail;
|
import org.springframework.http.ProblemDetail;
|
||||||
|
import org.springframework.validation.FieldError;
|
||||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
@@ -10,13 +11,18 @@ import org.springframework.web.client.ResourceAccessException;
|
|||||||
|
|
||||||
import static com.vaessl.app.exception.ErrorMessage.*;
|
import static com.vaessl.app.exception.ErrorMessage.*;
|
||||||
|
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@RestControllerAdvice
|
@RestControllerAdvice
|
||||||
public class GlobalExceptionHandler {
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||||
public ProblemDetail handleEmptyCredentialInput(MethodArgumentNotValidException e) {
|
public ProblemDetail handleEmptyCredentialInput(MethodArgumentNotValidException e) {
|
||||||
|
|
||||||
|
String defaultMessages = e.getBindingResult().getFieldErrors().stream().map(FieldError::getDefaultMessage).collect(Collectors.joining(", "));
|
||||||
|
|
||||||
return ProblemDetail.forStatusAndDetail(BAD_REQUEST_EMPTY_FIELDS.getStatus(),
|
return ProblemDetail.forStatusAndDetail(BAD_REQUEST_EMPTY_FIELDS.getStatus(),
|
||||||
BAD_REQUEST_EMPTY_FIELDS.getMessage());
|
BAD_REQUEST_EMPTY_FIELDS.getMessage() + " [" + defaultMessages + "]");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(HttpClientErrorException.Unauthorized.class)
|
@ExceptionHandler(HttpClientErrorException.Unauthorized.class)
|
||||||
@@ -41,8 +47,9 @@ public class GlobalExceptionHandler {
|
|||||||
SERVER_ERROR_GENERAL.getMessage() + e.getStatusText());
|
SERVER_ERROR_GENERAL.getMessage() + e.getStatusText());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(ProviderNotFoundException.class)
|
@ExceptionHandler(EmptyCredentialsException.class)
|
||||||
public ProblemDetail handleWrongServiceType(ProviderNotFoundException e) {
|
public ProblemDetail handleEmptyCredentials(EmptyCredentialsException e) {
|
||||||
return ProblemDetail.forStatusAndDetail(WRONG_SERVICE_TYPE.getStatus(), WRONG_SERVICE_TYPE.getMessage());
|
return ProblemDetail.forStatusAndDetail(BAD_REQUEST_EMPTY_FIELDS.getStatus(),
|
||||||
|
BAD_REQUEST_EMPTY_FIELDS.getMessage() + " " + e.getMissingFields());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
package com.vaessl.app.exception;
|
|
||||||
|
|
||||||
public class ProviderNotFoundException extends RuntimeException {
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user