Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SELC-4590] feat: Added V2 API for tokenExchange and billing tokenExchange #390

Merged
merged 7 commits into from
Mar 28, 2024
222 changes: 222 additions & 0 deletions app/src/main/resources/swagger/api-docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2814,6 +2814,228 @@
} ]
}
},
"/v2/token/exchange" : {
"get" : {
"tags" : [ "token" ],
"summary" : "exchange",
"description" : "Service create an 'Identity Token' based on a Self Care session token",
"operationId" : "exchangeUsingGET_1",
"parameters" : [ {
"name" : "institutionId",
"in" : "query",
"description" : "Institution's unique internal identifier",
"required" : true,
"style" : "form",
"schema" : {
"type" : "string"
}
}, {
"name" : "productId",
"in" : "query",
"description" : "Product's unique identifier",
"required" : true,
"style" : "form",
"schema" : {
"type" : "string"
}
}, {
"name" : "environment",
"in" : "query",
"description" : "Back Office environment",
"required" : false,
"style" : "form",
"schema" : {
"type" : "string"
}
} ],
"responses" : {
"200" : {
"description" : "OK",
"content" : {
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/IdentityTokenResource"
}
}
}
},
"400" : {
"description" : "Bad Request",
"content" : {
"application/problem+json" : {
"schema" : {
"$ref" : "#/components/schemas/Problem"
}
}
}
},
"401" : {
"description" : "Unauthorized",
"content" : {
"application/problem+json" : {
"schema" : {
"$ref" : "#/components/schemas/Problem"
}
}
}
},
"404" : {
"description" : "Not Found",
"content" : {
"application/problem+json" : {
"schema" : {
"$ref" : "#/components/schemas/Problem"
}
}
}
},
"500" : {
"description" : "Internal Server Error",
"content" : {
"application/problem+json" : {
"schema" : {
"$ref" : "#/components/schemas/Problem"
}
}
}
}
},
"security" : [ {
"bearerAuth" : [ "global" ]
} ]
}
},
"/v2/token/exchange/fatturazione" : {
"get" : {
"tags" : [ "token" ],
"summary" : "billingToken",
"description" : "Service to create a 'Billing Token' based on a Self Care session token",
"operationId" : "billingTokenUsingGET_1",
"parameters" : [ {
"name" : "authenticated",
"in" : "query",
"required" : false,
"style" : "form",
"schema" : {
"type" : "boolean"
}
}, {
"name" : "authorities[0].authority",
"in" : "query",
"required" : false,
"style" : "form",
"schema" : {
"type" : "string"
}
}, {
"name" : "credentials",
"in" : "query",
"required" : false,
"style" : "form",
"schema" : {
"type" : "string"
}
}, {
"name" : "details",
"in" : "query",
"required" : false,
"style" : "form",
"schema" : {
"type" : "object"
}
}, {
"name" : "institutionId",
"in" : "query",
"description" : "Institution's unique internal identifier",
"required" : true,
"style" : "form",
"schema" : {
"type" : "string"
}
}, {
"name" : "name",
"in" : "query",
"required" : false,
"style" : "form",
"schema" : {
"type" : "string"
}
}, {
"name" : "principal",
"in" : "query",
"required" : false,
"style" : "form",
"schema" : {
"type" : "object"
}
}, {
"name" : "environment",
"in" : "query",
"description" : "Back Office environment",
"required" : false,
"style" : "form",
"schema" : {
"type" : "string"
}
} ],
"responses" : {
"200" : {
"description" : "OK",
"content" : {
"application/json" : {
"schema" : {
"type" : "string",
"format" : "uri"
}
}
}
},
"400" : {
"description" : "Bad Request",
"content" : {
"application/problem+json" : {
"schema" : {
"$ref" : "#/components/schemas/Problem"
}
}
}
},
"401" : {
"description" : "Unauthorized",
"content" : {
"application/problem+json" : {
"schema" : {
"$ref" : "#/components/schemas/Problem"
}
}
}
},
"404" : {
"description" : "Not Found",
"content" : {
"application/problem+json" : {
"schema" : {
"$ref" : "#/components/schemas/Problem"
}
}
}
},
"500" : {
"description" : "Internal Server Error",
"content" : {
"application/problem+json" : {
"schema" : {
"$ref" : "#/components/schemas/Problem"
}
}
}
}
},
"security" : [ {
"bearerAuth" : [ "global" ]
} ]
}
},
"/v1/users" : {
"post" : {
"tags" : [ "user" ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import it.pagopa.selfcare.dashboard.core.*;
import it.pagopa.selfcare.dashboard.web.model.mapper.*;
import it.pagopa.selfcare.dashboard.web.security.ExchangeTokenService;
import it.pagopa.selfcare.dashboard.web.security.ExchangeTokenServiceV2;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
Expand Down Expand Up @@ -45,9 +46,12 @@ class SwaggerConfigTest {
@MockBean
private InstitutionV2Service institutionV2Service;

;@MockBean
@MockBean
private PnPGInstitutionService pnPGInstitutionService;

@MockBean
private ExchangeTokenServiceV2 exchangeTokenServiceV2Mock;

@MockBean
private ExchangeTokenService exchangeTokenServiceMock;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public Institution findInstitutionById(String institutionId) {
Institution institution = msCoreConnector.getInstitution(institutionId);

if (userInstitution != null) {
boolean limited = userInstitution.getProducts().stream().anyMatch(prod -> SelfCareAuthority.ADMIN.equals(prod.getRole().getSelfCareAuthority()));
boolean limited = userInstitution.getProducts().stream().noneMatch(prod -> SelfCareAuthority.ADMIN.equals(prod.getRole().getSelfCareAuthority()));
if (limited) {
institution.getOnboarding().stream()
.filter(product -> userInstitution.getProducts().stream().anyMatch(prodUser -> product.getProductId().equals(prodUser.getProductId())))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package it.pagopa.selfcare.dashboard.web.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import it.pagopa.selfcare.commons.base.logging.LogUtils;
import it.pagopa.selfcare.commons.web.security.JwtAuthenticationToken;
import it.pagopa.selfcare.dashboard.web.model.ExchangedToken;
import it.pagopa.selfcare.dashboard.web.model.IdentityTokenResource;
import it.pagopa.selfcare.dashboard.web.security.ExchangeTokenServiceV2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.util.Optional;

@Slf4j
@RestController
@RequestMapping(value = "/v2/token", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(tags = "token")
public class TokenV2Controller {

private final ExchangeTokenServiceV2 exchangeTokenService;


@Autowired
public TokenV2Controller(ExchangeTokenServiceV2 exchangeTokenServiceV2) {
this.exchangeTokenService = exchangeTokenServiceV2;
}


@GetMapping(value = "exchange")
@ResponseStatus(HttpStatus.OK)
@ApiOperation(value = "${swagger.dashboard.token.api.exchange}", notes = "${swagger.dashboard.token.api.exchange}", nickname = "v2Exchange")
@PreAuthorize("hasPermission(new it.pagopa.selfcare.dashboard.web.security.ProductAclDomain(#institutionId, #productId), 'ANY')")
public IdentityTokenResource exchange(@ApiParam("${swagger.dashboard.institutions.model.id}")
@RequestParam("institutionId")
String institutionId,
@ApiParam("${swagger.dashboard.products.model.id}")
@RequestParam("productId")
String productId,
@ApiParam("${swagger.dashboard.product-backoffice-configurations.model.environment}")
@RequestParam(value = "environment", required = false)
Optional<String> environment) {

log.trace("exchange start");
log.debug("exchange institutionId = {}, productId = {}", institutionId, productId);

String token = exchangeTokenService.exchange(institutionId, productId, environment).getIdentityToken();
IdentityTokenResource identityToken = new IdentityTokenResource();
identityToken.setToken(token);


log.debug(LogUtils.CONFIDENTIAL_MARKER, "exchange result = {}", identityToken);
log.trace("exchange end");

return identityToken;
}

@GetMapping(value = "exchange/fatturazione")
@ResponseStatus(HttpStatus.OK)
@ApiOperation(value = "", notes = "${swagger.dashboard.token.api.billingToken}")
@PreAuthorize("hasPermission(#institutionId, 'InstitutionResource', 'ANY')")
public URI billingToken(@ApiParam("${swagger.dashboard.institutions.model.id}")
@RequestParam("institutionId")
String institutionId,
@ApiParam("${swagger.dashboard.product-backoffice-configurations.model.environment}")
@RequestParam(value = "environment", required = false)
Optional<String> environment,
JwtAuthenticationToken jwtAuthenticationToken) {

log.trace("billing exchange start");
log.debug("billing exchange institutionId = {}", institutionId);
log.info("env parameter: {}", environment);

final ExchangedToken exchangedToken = exchangeTokenService.retrieveBillingExchangedToken(institutionId);
final URI location = URI.create(exchangedToken.getBackOfficeUrl().replace("<IdentityToken>", exchangedToken.getIdentityToken()));
log.debug(LogUtils.CONFIDENTIAL_MARKER, "billing exchange result = {}", location);
log.trace("billing exchange end");

return location;
}

}
Loading