use of feign.gson.GsonDecoder in project product-apim by wso2.
the class TestUtil method generateClient.
public static DCRClientInfo generateClient() throws APIManagementException {
DCRClientInfo dcrClientInfo = new DCRClientInfo();
dcrClientInfo.setClientName("apim-integration-test");
dcrClientInfo.setGrantTypes(Arrays.asList(new String[] { "password", "client_credentials" }));
try {
Response response = DCRMServiceStubFactory.getDCRMServiceStub(DYNAMIC_CLIENT_REGISTRATION_ENDPOINT, username, password, "wso2carbon").registerApplication(dcrClientInfo);
DCRClientInfo dcrClientInfoResponse = (DCRClientInfo) new GsonDecoder().decode(response, DCRClientInfo.class);
clientId = dcrClientInfoResponse.getClientId();
clientSecret = dcrClientInfoResponse.getClientSecret();
return dcrClientInfoResponse;
} catch (APIManagementException | IOException e) {
logger.error("Couldn't create client", e);
throw new APIManagementException("Couldn't create client", e);
}
}
use of feign.gson.GsonDecoder in project product-apim by wso2.
the class TestUtil method generateToken.
private static void generateToken(String username, String password, String scopes) throws APIManagementException {
if (StringUtils.isEmpty(clientId) | StringUtils.isEmpty(clientSecret)) {
generateClient();
}
OAuth2ServiceStubs.TokenServiceStub tokenServiceStub = getOauth2Client();
Response response = tokenServiceStub.generatePasswordGrantAccessToken(username, password, scopes, -1, clientId, clientSecret);
if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
// 200 - Success
logger.debug("A new access token is successfully generated.");
try {
OAuth2TokenInfo oAuth2TokenInfo = (OAuth2TokenInfo) new GsonDecoder().decode(response, OAuth2TokenInfo.class);
accessTokenInfo = new TokenInfo(oAuth2TokenInfo.getAccessToken(), System.currentTimeMillis() + oAuth2TokenInfo.getExpiresIn());
} catch (IOException e) {
throw new KeyManagementException("Error occurred while parsing token response", e, ExceptionCodes.ACCESS_TOKEN_GENERATION_FAILED);
}
}
}
use of feign.gson.GsonDecoder in project carbon-apimgt by wso2.
the class DefaultIdentityProviderImpl method getRoleNamesOfUser.
@Override
public List<String> getRoleNamesOfUser(String userId) throws IdentityProviderException {
List<String> roleNames = new ArrayList<>();
Response response = scimServiceStub.getUser(userId);
if (response == null) {
String errorMessage = "Error occurred while retrieving user with Id " + userId + ". Error : Response is null.";
log.error(errorMessage);
throw new IdentityProviderException(errorMessage, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
}
try {
if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
SCIMUser scimUser = (SCIMUser) new GsonDecoder().decode(response, SCIMUser.class);
if (scimUser != null) {
List<SCIMUser.SCIMUserGroups> roles = scimUser.getGroups();
if (roles != null) {
roles.forEach(role -> roleNames.add(role.getDisplay()));
String message = "Role names of user " + scimUser.getName() + " are successfully retrieved as " + StringUtils.join(roleNames, ", ") + ".";
if (log.isDebugEnabled()) {
log.debug(message);
}
}
} else {
String errorMessage = "Error occurred while retrieving user with user Id " + userId + " from SCIM endpoint. " + "Response body is null or empty.";
log.error(errorMessage);
throw new IdentityProviderException("Error occurred while retrieving user with user Id " + userId + " from SCIM endpoint. " + "Response body is null or empty.", ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
}
} else {
String errorMessage = "Error occurred while retrieving role names of user with Id " + userId + ". Error : " + getErrorMessage(response);
log.error(errorMessage);
throw new IdentityProviderException(errorMessage, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
}
} catch (IOException e) {
String errorMessage = "Error occurred while parsing response from SCIM endpoint.";
log.error(errorMessage);
throw new IdentityProviderException("Error occurred while parsing response from SCIM endpoint for ", e, ExceptionCodes.RESOURCE_RETRIEVAL_FAILED);
}
return roleNames;
}
use of feign.gson.GsonDecoder in project carbon-apimgt by wso2.
the class DefaultKeyManagerImpl method getTokenMetaData.
@Override
public AccessTokenInfo getTokenMetaData(String accessToken) throws KeyManagementException {
log.debug("Token introspection request is being sent.");
Response response;
try {
response = oAuth2ServiceStubs.getIntrospectionServiceStub().introspectToken(accessToken);
} catch (APIManagementException e) {
throw new KeyManagementException("Error occurred while introspecting access token.", e, ExceptionCodes.TOKEN_INTROSPECTION_FAILED);
}
if (response == null) {
throw new KeyManagementException("Error occurred while introspecting access token. " + "Response is null", ExceptionCodes.TOKEN_INTROSPECTION_FAILED);
}
if (response.status() == APIMgtConstants.HTTPStatusCodes.SC_200_OK) {
log.debug("Token introspection is successful");
try {
OAuth2IntrospectionResponse introspectResponse = (OAuth2IntrospectionResponse) new GsonDecoder().decode(response, OAuth2IntrospectionResponse.class);
AccessTokenInfo tokenInfo = new AccessTokenInfo();
boolean active = introspectResponse.isActive();
if (active) {
tokenInfo.setTokenValid(true);
tokenInfo.setAccessToken(accessToken);
tokenInfo.setScopes(introspectResponse.getScope());
tokenInfo.setConsumerKey(introspectResponse.getClientId());
tokenInfo.setIssuedTime(introspectResponse.getIat());
tokenInfo.setExpiryTime(introspectResponse.getExp());
if (StringUtils.isNotEmpty(introspectResponse.getUsername())) {
tokenInfo.setEndUserName(introspectResponse.getUsername());
}
long validityPeriod = introspectResponse.getExp() - introspectResponse.getIat();
tokenInfo.setValidityPeriod(validityPeriod);
} else {
tokenInfo.setTokenValid(false);
log.error("Invalid or expired access token received.");
tokenInfo.setErrorCode(KeyManagerConstants.KeyValidationStatus.API_AUTH_INVALID_CREDENTIALS);
}
return tokenInfo;
} catch (IOException e) {
throw new KeyManagementException("Error occurred while parsing token introspection response", e, ExceptionCodes.TOKEN_INTROSPECTION_FAILED);
}
} else {
throw new KeyManagementException("Token introspection request failed. HTTP error code: " + response.status() + " Error Response Body: " + response.body().toString(), ExceptionCodes.TOKEN_INTROSPECTION_FAILED);
}
}
use of feign.gson.GsonDecoder in project carbon-apimgt by wso2.
the class DefaultKeyManagerImpl method getOAuthApplicationInfo.
private OAuthApplicationInfo getOAuthApplicationInfo(Response response) throws IOException {
OAuthApplicationInfo oAuthApplicationInfoResponse = new OAuthApplicationInfo();
DCRClientInfo dcrClientInfoResponse = (DCRClientInfo) new GsonDecoder().decode(response, DCRClientInfo.class);
oAuthApplicationInfoResponse.setClientName(dcrClientInfoResponse.getClientName());
oAuthApplicationInfoResponse.setClientId(dcrClientInfoResponse.getClientId());
oAuthApplicationInfoResponse.setClientSecret(dcrClientInfoResponse.getClientSecret());
oAuthApplicationInfoResponse.setGrantTypes(dcrClientInfoResponse.getGrantTypes());
oAuthApplicationInfoResponse.setCallBackURL(dcrClientInfoResponse.getRedirectURIs().get(0));
return oAuthApplicationInfoResponse;
}
Aggregations