use of org.wso2.carbon.identity.application.common.model.IdentityProvider in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testDeleteApiWithZeroSubscriptions.
@Test(description = "Delete API with zero Subscriptions")
public void testDeleteApiWithZeroSubscriptions() throws APIManagementException, LifecycleException, SQLException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
List<String> roleIdsOfUser = new ArrayList<>();
roleIdsOfUser.add(ADMIN_ROLE_ID);
APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI();
API api = apiBuilder.build();
String uuid = api.getId();
String lifecycleId = api.getLifecycleInstanceId();
Mockito.when(apiSubscriptionDAO.getSubscriptionCountByAPI(uuid)).thenReturn(0L);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
APIGateway gateway = Mockito.mock(APIGateway.class);
IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
LabelDAO labelDao = Mockito.mock(LabelDAO.class);
KeyManager keyManager = Mockito.mock(KeyManager.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(ALTERNATIVE_USER, identityProvider, keyManager, apiDAO, null, apiSubscriptionDAO, null, apiLifecycleManager, labelDao, null, null, null, null, gateway);
Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api);
Mockito.when(apiDAO.getApiSwaggerDefinition(api.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
Mockito.when(identityProvider.getIdOfUser(ALTERNATIVE_USER)).thenReturn(USER_ID);
Mockito.when(identityProvider.getRoleIdsOfUser(USER_ID)).thenReturn(roleIdsOfUser);
Mockito.when(identityProvider.getRoleName(SampleTestObjectCreator.DEVELOPER_ROLE_ID)).thenReturn(DEVELOPER_ROLE);
Mockito.when(identityProvider.getRoleName(SampleTestObjectCreator.ADMIN_ROLE_ID)).thenReturn(ADMIN_ROLE);
Mockito.when(apiDAO.getApiSwaggerDefinition(api.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
apiPublisher.deleteAPI(uuid);
Mockito.verify(apiDAO, Mockito.times(1)).getAPI(uuid);
Mockito.verify(apiLifecycleManager, Mockito.times(1)).removeLifecycle(lifecycleId);
Mockito.verify(apiDAO, Mockito.times(1)).deleteAPI(uuid);
}
use of org.wso2.carbon.identity.application.common.model.IdentityProvider in project carbon-apimgt by wso2.
the class APIStoreImplTestCase method testSelfSignUpErrorCase.
@Test(description = "User Self Signup Error Case", expectedExceptions = IdentityProviderException.class)
public void testSelfSignUpErrorCase() throws Exception {
IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
APIStoreImpl apiStore = getApiStoreImpl(identityProvider);
User user = new User();
Mockito.doThrow(IdentityProviderException.class).when(identityProvider).registerUser(user);
apiStore.selfSignUp(user);
}
use of org.wso2.carbon.identity.application.common.model.IdentityProvider in project carbon-apimgt by wso2.
the class APIStoreImplTestCase method testAddCompositeApi.
@Test(description = "Add Composite API")
public void testAddCompositeApi() throws APIManagementException {
CompositeAPI.Builder apiBuilder = SampleTestObjectCreator.createUniqueCompositeAPI();
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
APISubscriptionDAO apiSubscriptionDAO = Mockito.mock(APISubscriptionDAO.class);
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APIGateway apiGateway = Mockito.mock(APIGateway.class);
IdentityProvider idp = Mockito.mock(IdentityProvider.class);
KeyManager km = Mockito.mock(KeyManager.class);
APIStore apiStore = getApiStoreImpl(idp, km, apiDAO, apiSubscriptionDAO, gatewaySourceGenerator, apiGateway);
apiStore.addCompositeApi(apiBuilder);
Mockito.verify(apiDAO, Mockito.times(1)).addApplicationAssociatedAPI(apiBuilder.build());
}
use of org.wso2.carbon.identity.application.common.model.IdentityProvider in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testReplaceGroupNamesWithIdWithInvalidRoles.
@Test(description = "Update API when there is a list of invalid roles specified for permission")
public void testReplaceGroupNamesWithIdWithInvalidRoles() throws APIManagementException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
IdentityProvider identityProvider = Mockito.mock(IdentityProvider.class);
String permissionString = "[{\"groupId\" : \"developer\", \"permission\" : [\"READ\",\"UPDATE\"]}," + "{\"groupId\" : \"invalid_role\", \"permission\" : [\"READ\",\"UPDATE\",\"DELETE\"]}]";
String errorMessage = "There are invalid roles in the permission string";
API.APIBuilder api = SampleTestObjectCreator.createDefaultAPI().apiPermission(permissionString);
String uuid = api.getId();
GatewaySourceGenerator gatewaySourceGenerator = Mockito.mock(GatewaySourceGenerator.class);
APIGateway gateway = Mockito.mock(APIGateway.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(identityProvider, apiDAO, apiLifecycleManager, gatewaySourceGenerator, gateway);
Mockito.when(apiDAO.getAPI(uuid)).thenReturn(api.lifeCycleStatus(APIStatus.CREATED.getStatus()).build());
Mockito.when(identityProvider.getRoleId("invalid_role")).thenThrow(new IdentityProviderException(errorMessage, ExceptionCodes.ROLE_DOES_NOT_EXIST));
Mockito.when(identityProvider.getRoleId(DEVELOPER_ROLE)).thenReturn(DEVELOPER_ROLE_ID);
Mockito.when(apiDAO.isAPIContextExists(api.getContext())).thenReturn(true);
String configString = SampleTestObjectCreator.createSampleGatewayConfig();
Mockito.when(apiDAO.getGatewayConfigOfAPI(uuid)).thenReturn(configString);
Mockito.when(apiDAO.getApiSwaggerDefinition(api.getId())).thenReturn(SampleTestObjectCreator.apiDefinition);
try {
apiPublisher.updateAPI(api.lifeCycleStatus(APIStatus.CREATED.getStatus()).id(uuid));
} catch (APIManagementException e) {
Assert.assertEquals(e.getMessage(), "There are invalid roles in the permission string");
}
}
use of org.wso2.carbon.identity.application.common.model.IdentityProvider in project carbon-apimgt by wso2.
the class AuthenticatorAPI method authenticate.
/**
* This method authenticate the user for store app.
*/
@OPTIONS
@POST
@Path("/token/{appName}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes({ MediaType.APPLICATION_FORM_URLENCODED, MediaType.MULTIPART_FORM_DATA })
public Response authenticate(@Context Request request, @PathParam("appName") String appName, @FormDataParam("username") String userName, @FormDataParam("password") String password, @FormDataParam("assertion") String assertion, @FormDataParam("grant_type") String grantType, @FormDataParam("validity_period") String validityPeriod, @FormDataParam("remember_me") boolean isRememberMe, @FormDataParam("scopes") String scopesList) {
try {
AuthenticatorService authenticatorService = AuthenticatorAPIFactory.getInstance().getService();
IdentityProvider identityProvider = APIManagerFactory.getInstance().getIdentityProvider();
AuthResponseBean authResponseBean;
Map<String, NewCookie> cookies = new HashMap<>();
String refreshToken = null;
if (AuthenticatorConstants.REFRESH_GRANT.equals(grantType)) {
String environmentName = APIMConfigurationService.getInstance().getEnvironmentConfigurations().getEnvironmentLabel();
refreshToken = AuthUtil.extractTokenFromHeaders(request, AuthenticatorConstants.REFRESH_TOKEN_2, environmentName);
if (refreshToken == null) {
ErrorDTO errorDTO = new ErrorDTO();
errorDTO.setCode(ExceptionCodes.INVALID_AUTHORIZATION_HEADER.getErrorCode());
errorDTO.setMessage(ExceptionCodes.INVALID_AUTHORIZATION_HEADER.getErrorMessage());
return Response.status(Response.Status.UNAUTHORIZED).entity(errorDTO).build();
}
}
Map<String, String> contextPaths = AuthUtil.getContextPaths(appName);
AccessTokenInfo accessTokenInfo = authenticatorService.getTokens(appName, grantType, userName, password, refreshToken, Long.parseLong(validityPeriod), null, assertion, identityProvider);
authResponseBean = authenticatorService.getResponseBeanFromTokenInfo(accessTokenInfo);
authenticatorService.setupAccessTokenParts(cookies, authResponseBean, accessTokenInfo.getAccessToken(), contextPaths, false);
String refreshTokenNew = accessTokenInfo.getRefreshToken();
// Refresh token is not set to cookie if remember me is not set.
if (refreshTokenNew != null && (AuthenticatorConstants.REFRESH_GRANT.equals(grantType) || (AuthenticatorConstants.PASSWORD_GRANT.equals(grantType) && isRememberMe))) {
authenticatorService.setupRefreshTokenParts(cookies, refreshTokenNew, contextPaths);
return Response.ok(authResponseBean, MediaType.APPLICATION_JSON).cookie(cookies.get(AuthenticatorConstants.Context.REST_API_CONTEXT), cookies.get(AuthenticatorConstants.Context.LOGOUT_CONTEXT), cookies.get(AuthenticatorConstants.Context.APP_CONTEXT), cookies.get(AuthenticatorConstants.Context.LOGIN_CONTEXT)).header(AuthenticatorConstants.REFERER_HEADER, (request.getHeader(AuthenticatorConstants.X_ALT_REFERER_HEADER) != null && request.getHeader(AuthenticatorConstants.X_ALT_REFERER_HEADER).equals(request.getHeader(AuthenticatorConstants.REFERER_HEADER))) ? "" : request.getHeader(AuthenticatorConstants.X_ALT_REFERER_HEADER) != null ? request.getHeader(AuthenticatorConstants.X_ALT_REFERER_HEADER) : "").build();
} else {
return Response.ok(authResponseBean, MediaType.APPLICATION_JSON).cookie(cookies.get(AuthenticatorConstants.Context.REST_API_CONTEXT), cookies.get(AuthenticatorConstants.Context.LOGOUT_CONTEXT)).header(AuthenticatorConstants.REFERER_HEADER, (request.getHeader(AuthenticatorConstants.X_ALT_REFERER_HEADER) != null && request.getHeader(AuthenticatorConstants.X_ALT_REFERER_HEADER).equals(request.getHeader(AuthenticatorConstants.REFERER_HEADER))) ? "" : request.getHeader(AuthenticatorConstants.X_ALT_REFERER_HEADER) != null ? request.getHeader(AuthenticatorConstants.X_ALT_REFERER_HEADER) : "").build();
}
} catch (APIManagementException e) {
ErrorDTO errorDTO = AuthUtil.getErrorDTO(e.getErrorHandler(), null);
log.error(e.getMessage(), e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
Aggregations