-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
P4ADEV-1274 added parameters validation
- Loading branch information
Showing
12 changed files
with
259 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/it/gov/pagopa/payhub/auth/service/a2a/ClientCredentialService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package it.gov.pagopa.payhub.auth.service.a2a; | ||
|
||
import it.gov.pagopa.payhub.model.generated.AccessToken; | ||
|
||
public interface ClientCredentialService { | ||
AccessToken postToken(String clientId, String grantType, String scope, String clientSecret); | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/it/gov/pagopa/payhub/auth/service/a2a/ClientCredentialServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package it.gov.pagopa.payhub.auth.service.a2a; | ||
|
||
import it.gov.pagopa.payhub.model.generated.AccessToken; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@Slf4j | ||
public class ClientCredentialServiceImpl implements ClientCredentialService { | ||
|
||
private final ValidateClientCredentialsService validateClientCredentialsService; | ||
|
||
public ClientCredentialServiceImpl(ValidateClientCredentialsService validateClientCredentialsService) { | ||
this.validateClientCredentialsService = validateClientCredentialsService; | ||
} | ||
|
||
@Override | ||
public AccessToken postToken(String clientId, String grantType, String scope, String clientSecret) { | ||
validateClientCredentialsService.validate(clientId, grantType, scope, clientSecret); | ||
//TODO return real AccessToken implementation | ||
return new AccessToken("token", "bearer", 7); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/main/java/it/gov/pagopa/payhub/auth/service/a2a/ClientService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
package it.gov.pagopa.payhub.auth.service.a2a; | ||
|
||
import it.gov.pagopa.payhub.auth.model.Client; | ||
import it.gov.pagopa.payhub.model.generated.ClientDTO; | ||
import it.gov.pagopa.payhub.model.generated.ClientNoSecretDTO; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface ClientService { | ||
|
||
ClientDTO registerClient(String clientName, String organizationIpaCode); | ||
String getClientSecret(String organizationIpaCode, String clientId); | ||
List<ClientNoSecretDTO> getClients(String organizationIpaCode); | ||
Optional<Client> getClientByClientId(String clientId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/it/gov/pagopa/payhub/auth/service/a2a/ValidateClientCredentialsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package it.gov.pagopa.payhub.auth.service.a2a; | ||
|
||
import it.gov.pagopa.payhub.auth.exception.custom.InvalidExchangeClientException; | ||
import it.gov.pagopa.payhub.auth.exception.custom.InvalidExchangeRequestException; | ||
import it.gov.pagopa.payhub.auth.exception.custom.InvalidGrantTypeException; | ||
import it.gov.pagopa.payhub.auth.model.Client; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.util.StringUtils; | ||
|
||
@Service | ||
@Slf4j | ||
public class ValidateClientCredentialsService { | ||
private final ClientService clientService; | ||
public static final String ALLOWED_GRANT_TYPE = "client_credentials"; | ||
public static final String ALLOWED_SCOPE = "openid"; | ||
|
||
public ValidateClientCredentialsService(ClientService clientService) { | ||
this.clientService = clientService; | ||
} | ||
|
||
public void validate(String clientId, String grantType, String scope, String clientSecret) { | ||
validateClient(clientId); | ||
validateProtocolConfiguration(grantType, scope); | ||
validateClientSecret(clientSecret); | ||
log.info("authorization granted"); | ||
} | ||
|
||
//TODO Client will be used to verify clientSecret and assign roles with organizationIpaCode | ||
private Client validateClient(String clientId) { | ||
return clientService.getClientByClientId(clientId) | ||
.orElseThrow(() -> new InvalidExchangeClientException("Invalid clientId:"+ clientId)); | ||
} | ||
|
||
private void validateProtocolConfiguration(String grantType, String scope) { | ||
if (!ALLOWED_GRANT_TYPE.equals(grantType)) { | ||
throw new InvalidGrantTypeException("Invalid grantType " + grantType); | ||
} | ||
if (!ALLOWED_SCOPE.equals(scope)){ | ||
throw new InvalidExchangeRequestException("Invalid scope " + scope); | ||
} | ||
} | ||
|
||
private void validateClientSecret(String clientSecret) { | ||
if (!StringUtils.hasText(clientSecret)) { | ||
throw new InvalidExchangeRequestException("clientSecret is mandatory with client-credentials grant type"); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...test/java/it/gov/pagopa/payhub/auth/service/a2a/ValidateClientCredentialsServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package it.gov.pagopa.payhub.auth.service.a2a; | ||
|
||
import it.gov.pagopa.payhub.auth.exception.custom.InvalidExchangeClientException; | ||
import it.gov.pagopa.payhub.auth.exception.custom.InvalidExchangeRequestException; | ||
import it.gov.pagopa.payhub.auth.exception.custom.InvalidGrantTypeException; | ||
import it.gov.pagopa.payhub.auth.model.Client; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class ValidateClientCredentialsServiceTest { | ||
@Mock | ||
private ClientService clientServiceMock; | ||
private ValidateClientCredentialsService service; | ||
|
||
private static final String ALLOWED_CLIENT_ID = "CLIENTID"; | ||
private static final String ALLOWED_CLIENT_SECRET = "CLIENTSECRET"; | ||
|
||
@BeforeEach | ||
void setup(){ | ||
service = new ValidateClientCredentialsService(clientServiceMock); | ||
} | ||
|
||
@Test | ||
void givenValidRequestThenOk() { | ||
Mockito.doReturn(Optional.of(new Client())).when(clientServiceMock).getClientByClientId(ALLOWED_CLIENT_ID); | ||
assertDoesNotThrow(() -> | ||
service.validate(ALLOWED_CLIENT_ID, ValidateClientCredentialsService.ALLOWED_GRANT_TYPE, ValidateClientCredentialsService.ALLOWED_SCOPE, ALLOWED_CLIENT_SECRET)); | ||
} | ||
|
||
@Test | ||
void givenInvalidExchangeClientException() { | ||
assertThrows(InvalidExchangeClientException.class, () -> | ||
service.validate("UNEXPECTED_CLIENT_ID", ValidateClientCredentialsService.ALLOWED_GRANT_TYPE, ValidateClientCredentialsService.ALLOWED_SCOPE, ALLOWED_CLIENT_SECRET)); | ||
} | ||
|
||
@Test | ||
void givenInvalidGrantTypeException() { | ||
Mockito.doReturn(Optional.of(new Client())).when(clientServiceMock).getClientByClientId(ALLOWED_CLIENT_ID); | ||
assertThrows(InvalidGrantTypeException.class, () -> | ||
service.validate(ALLOWED_CLIENT_ID, "UNEXPECTED_GRANT_TYPE", ValidateClientCredentialsService.ALLOWED_SCOPE, ALLOWED_CLIENT_SECRET)); | ||
} | ||
|
||
@Test | ||
void givenInvalidScopeThenInvalidExchangeRequestException() { | ||
Mockito.doReturn(Optional.of(new Client())).when(clientServiceMock).getClientByClientId(ALLOWED_CLIENT_ID); | ||
assertThrows(InvalidExchangeRequestException.class, () -> | ||
service.validate(ALLOWED_CLIENT_ID, ValidateClientCredentialsService.ALLOWED_GRANT_TYPE, "UNEXPECTED_SCOPE", ALLOWED_CLIENT_SECRET)); | ||
} | ||
|
||
@Test | ||
void givenNullClientSecretThenInvalidExchangeRequestException() { | ||
Mockito.doReturn(Optional.of(new Client())).when(clientServiceMock).getClientByClientId(ALLOWED_CLIENT_ID); | ||
assertThrows(InvalidExchangeRequestException.class, () -> | ||
service.validate(ALLOWED_CLIENT_ID, ValidateClientCredentialsService.ALLOWED_GRANT_TYPE, ValidateClientCredentialsService.ALLOWED_SCOPE, null)); | ||
} | ||
|
||
} |
Oops, something went wrong.