use of io.crnk.core.engine.security.SecurityProvider in project crnk-framework by crnk-project.
the class CrnkFeatureTest method testSecurityRegistration.
private void testSecurityRegistration(boolean enabled) {
CrnkFeature feature = new CrnkFeature();
feature.setSecurityEnabled(enabled);
feature.securityContext = Mockito.mock(SecurityContext.class);
FeatureContext context = Mockito.mock(FeatureContext.class);
Mockito.when(context.getConfiguration()).thenReturn(Mockito.mock(Configuration.class));
feature.configure(context);
CrnkBoot boot = feature.getBoot();
if (enabled) {
SecurityProvider securityProvider = boot.getModuleRegistry().getSecurityProvider();
Assert.assertNotNull(securityProvider);
} else {
try {
Assert.assertNull(boot.getModuleRegistry().getSecurityProvider());
Assert.fail();
} catch (IllegalStateException e) {
Assert.assertTrue(e.getMessage().contains("exactly one security provide must be installed"));
}
}
}
use of io.crnk.core.engine.security.SecurityProvider in project crnk-framework by crnk-project.
the class JaxrsModuleTest method checkSecurityProviderRegistered.
@Test
public void checkSecurityProviderRegistered() {
SecurityContext securityContext = Mockito.mock(SecurityContext.class);
JaxrsModule module = new JaxrsModule(securityContext);
CrnkBoot boot = new CrnkBoot();
boot.addModule(module);
boot.boot();
SecurityProvider securityProvider = boot.getModuleRegistry().getSecurityProvider();
Assert.assertNotNull(securityProvider);
Mockito.when(securityContext.isUserInRole("admin")).thenReturn(true);
Assert.assertTrue(securityProvider.isUserInRole("admin"));
Assert.assertFalse(securityProvider.isUserInRole("other"));
}
use of io.crnk.core.engine.security.SecurityProvider in project crnk-framework by crnk-project.
the class SecurityModule method isUserInRole.
/**
* Checks whether the current user posses the provided role
*
* @param role to check
* @return true if in this role
*/
public boolean isUserInRole(String role) {
if (!isEnabled()) {
throw new IllegalStateException("security module is disabled");
}
checkInit();
SecurityProvider securityProvider = context.getSecurityProvider();
boolean contained = role == ALL_ROLE || securityProvider.isUserInRole(role);
LOGGER.debug("isUserInRole returns {} for role {}", contained, role);
return contained;
}
use of io.crnk.core.engine.security.SecurityProvider in project crnk-framework by crnk-project.
the class SecurityModuleTest method setup.
@Before
public void setup() {
// TODO simplify ones simple module is fixed
SimpleModule appModule = new SimpleModule("app") {
@Override
public void setupModule(ModuleContext context) {
super.setupModule(context);
context.addSecurityProvider(new SecurityProvider() {
@Override
public boolean isUserInRole(String role) {
return role.equals(allowedRule);
}
});
}
};
appModule.addRepository(new TaskRepository());
appModule.addRepository(new ProjectRepository());
Builder builder = SecurityConfig.builder();
builder.permitAll(ResourcePermission.GET);
builder.permitRole("taskRole", Task.class, ResourcePermission.ALL);
builder.permitRole("projectRole", "projects", ResourcePermission.POST);
SecurityConfig config = builder.build();
securityModule = SecurityModule.newServerModule(config);
Assert.assertSame(config, securityModule.getConfig());
ModuleRegistry moduleRegistry = new ModuleRegistry();
moduleRegistry.setServiceDiscovery(new EmptyServiceDiscovery());
moduleRegistry.setResourceRegistry(new ResourceRegistryImpl(new DefaultResourceRegistryPart(), moduleRegistry));
moduleRegistry.addModule(securityModule);
moduleRegistry.addModule(appModule);
moduleRegistry.addModule(new JacksonModule(new ObjectMapper(), false, ImmutableList.of(new OffsetLimitPagingBehavior())));
moduleRegistry.addModule(new CoreModule());
moduleRegistry.init(new ObjectMapper());
}
use of io.crnk.core.engine.security.SecurityProvider in project crnk-framework by crnk-project.
the class ServletModuleTest method testSecurityProviderInstalled.
@Test
public void testSecurityProviderInstalled() {
HttpRequestContextProvider provider = new HttpRequestContextProvider();
ServletModule module = new ServletModule(provider);
CrnkBoot boot = new CrnkBoot();
boot.addModule(module);
boot.boot();
SecurityProvider securityProvider = boot.getModuleRegistry().getSecurityProvider();
ServletContext servletContext = Mockito.mock(ServletContext.class);
MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
MockHttpServletResponse response = new MockHttpServletResponse();
request.addUserRole("guest");
request.addUserRole("admin");
provider.onRequestStarted(new HttpRequestContextBaseAdapter(new ServletRequestContext(servletContext, request, response, "api", HttpHeaders.DEFAULT_CHARSET)));
Assert.assertFalse(securityProvider.isUserInRole("doesNotExist"));
Assert.assertTrue(securityProvider.isUserInRole("guest"));
Assert.assertTrue(securityProvider.isUserInRole("admin"));
}
Aggregations