use of org.codice.ddf.security.handler.api.PKIAuthenticationTokenFactory in project ddf by codice.
the class GuestHandlerTest method testGetNormalizedToken.
/**
* This test ensures the proper functionality of GuestHandler's method,
* getNormalizedToken().
*/
@Test
public void testGetNormalizedToken() throws WSSecurityException {
GuestHandler handler = new GuestHandler();
PKIAuthenticationTokenFactory tokenFactory = new PKIAuthenticationTokenFactory();
handler.setTokenFactory(tokenFactory);
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
FilterChain chain = mock(FilterChain.class);
/**
* Note that the parameters are insignificant as GuestHandler
* does not use them.
*/
HandlerResult result = handler.getNormalizedToken(request, response, chain, true);
assertNotNull(result);
assertEquals(HandlerResult.Status.COMPLETED, result.getStatus());
assertTrue(result.getToken() instanceof GuestAuthenticationToken);
assertEquals("Guest", result.getToken().getCredentials());
assertEquals(null, result.getToken().getRealm());
assertEquals("null-GuestHandler", result.getSource());
}
use of org.codice.ddf.security.handler.api.PKIAuthenticationTokenFactory in project ddf by codice.
the class GuestHandlerTest method testHandleError.
@Test
public void testHandleError() throws ServletException, IOException {
GuestHandler handler = new GuestHandler();
PKIAuthenticationTokenFactory tokenFactory = new PKIAuthenticationTokenFactory();
handler.setTokenFactory(tokenFactory);
StringWriter writer = new StringWriter(1024);
PrintWriter printWriter = new PrintWriter(writer);
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getAttribute(anyString())).thenReturn("DDF");
HttpServletResponse response = mock(HttpServletResponse.class);
when(response.getWriter()).thenReturn(printWriter);
FilterChain chain = mock(FilterChain.class);
/**
* Note that the parameters are insignificant as GuestHandler
* does not use them.
*/
HandlerResult result = handler.handleError(request, response, chain);
assertNotNull(result);
assertEquals(HandlerResult.Status.REDIRECTED, result.getStatus());
assertNull(result.getToken());
assertEquals("DDF-GuestHandler", result.getSource());
assertEquals(GuestHandler.INVALID_MESSAGE, writer.toString());
}
use of org.codice.ddf.security.handler.api.PKIAuthenticationTokenFactory in project ddf by codice.
the class PKIHandlerTest method getPKIHandlerWithMockedCrl.
/**
* Creates a PKIHandler with a mocked CrlChecker that always returns true or false
*
* @param returnedValue Boolean value that the mocked CrlChecker will always return
* @return A PKIHandler with a mocked CrlChecker
*/
private PKIHandler getPKIHandlerWithMockedCrl(String signatureProperties, boolean returnedValue) {
PKIHandler handler = new PKIHandler();
PKIAuthenticationTokenFactory tokenFactory = new PKIAuthenticationTokenFactory();
tokenFactory.setSignaturePropertiesPath(signatureProperties);
tokenFactory.init();
handler.setTokenFactory(tokenFactory);
CrlChecker crlChecker = mock(CrlChecker.class);
when(crlChecker.passesCrlCheck(any())).thenReturn(returnedValue);
handler.crlChecker = crlChecker;
return handler;
}
use of org.codice.ddf.security.handler.api.PKIAuthenticationTokenFactory in project ddf by codice.
the class WssPKIHandlerTest method getWssHandlerWithMockedCrl.
/**
* Creates a WssPKIHandler with a mocked CrlChecker that always returns true or false
*
* @param returnedValue Boolean value that the mocked CrlChecker will always return
* @return A WssPKIHandler with a mocked CrlChecker
*/
private WssPKIHandler getWssHandlerWithMockedCrl(String signatureProperties, boolean returnedValue) {
WssPKIHandler handler = new WssPKIHandler();
PKIAuthenticationTokenFactory tokenFactory = new PKIAuthenticationTokenFactory();
tokenFactory.setSignaturePropertiesPath(signatureProperties);
tokenFactory.init();
handler.setTokenFactory(tokenFactory);
CrlChecker crlChecker = mock(CrlChecker.class);
when(crlChecker.passesCrlCheck(any())).thenReturn(returnedValue);
handler.crlChecker = crlChecker;
return handler;
}
use of org.codice.ddf.security.handler.api.PKIAuthenticationTokenFactory in project ddf by codice.
the class Security method getSystemSubject.
/**
* Gets the {@link Subject} associated with this system. Uses a cached subject since the subject
* will not change between calls.
*
* @return system's {@link Subject}
*/
public synchronized Subject getSystemSubject() {
if (!javaSubjectHasAdminRole()) {
SecurityLogger.audit("Unable to retrieve system subject.");
return null;
}
if (!tokenAboutToExpire(cachedSystemSubject)) {
return cachedSystemSubject;
}
KeyStore keyStore = getSystemKeyStore();
String alias = null;
Certificate cert = null;
try {
if (keyStore != null) {
if (keyStore.size() == 1) {
alias = keyStore.aliases().nextElement();
} else if (keyStore.size() > 1) {
alias = getCertificateAlias();
}
cert = keyStore.getCertificate(alias);
}
} catch (KeyStoreException e) {
LOGGER.warn("Unable to get certificate for alias [{}]", alias, e);
return null;
}
if (cert == null) {
LOGGER.warn("Unable to get certificate for alias [{}]", alias);
return null;
}
PKIAuthenticationTokenFactory pkiTokenFactory = createPKITokenFactory();
PKIAuthenticationToken pkiToken = pkiTokenFactory.getTokenFromCerts(new X509Certificate[] { (X509Certificate) cert }, PKIAuthenticationToken.DEFAULT_REALM);
if (pkiToken != null) {
SecurityManager securityManager = getSecurityManager();
if (securityManager != null) {
try {
cachedSystemSubject = securityManager.getSubject(pkiToken);
} catch (SecurityServiceException sse) {
LOGGER.warn("Unable to request subject for system user.", sse);
}
}
}
return cachedSystemSubject;
}
Aggregations