use of ddf.security.common.SecurityTokenHolder in project ddf by codice.
the class LogoutService method getActionProviders.
@GET
@Path("/actions")
public Response getActionProviders(@Context HttpServletRequest request) throws SecurityServiceException {
HttpSession session = httpSessionFactory.getOrCreateSession(request);
Map<String, SecurityToken> realmTokenMap = ((SecurityTokenHolder) session.getAttribute(SecurityConstants.SAML_ASSERTION)).getRealmTokenMap();
Map<String, Subject> realmSubjectMap = new HashMap<>();
for (Map.Entry<String, SecurityToken> entry : realmTokenMap.entrySet()) {
realmSubjectMap.put(entry.getKey(), securityManager.getSubject(entry.getValue()));
}
List<Map<String, String>> realmToPropMaps = new ArrayList<>();
for (ActionProvider actionProvider : logoutActionProviders) {
Action action = actionProvider.getAction(realmSubjectMap);
if (action != null) {
String realm = StringUtils.substringAfterLast(action.getId(), ".");
//if the user is logged in and isn't a guest, add them
if (realmTokenMap.get(realm) != null) {
Map<String, String> actionProperties = new HashMap<>();
String displayName = SubjectUtils.getName(realmSubjectMap.get(realm), "", true);
if (displayName != null && !displayName.equals(SubjectUtils.GUEST_DISPLAY_NAME)) {
actionProperties.put("title", action.getTitle());
actionProperties.put("realm", realm);
actionProperties.put("auth", displayName);
actionProperties.put("description", action.getDescription());
actionProperties.put("url", action.getUrl().toString());
realmToPropMaps.add(actionProperties);
}
}
}
}
return Response.ok(new ByteArrayInputStream(toJson(realmToPropMaps).getBytes(StandardCharsets.UTF_8))).build();
}
use of ddf.security.common.SecurityTokenHolder in project ddf by codice.
the class TestLogoutService method initialize.
@BeforeClass
public static void initialize() {
Map<String, SecurityToken> realmTokenMap = new HashMap<>();
realmTokenMap.put("karaf", new SecurityToken());
realmTokenMap.put("ldap", new SecurityToken());
sessionFactory = mock(SessionFactory.class);
HttpSession httpSession = mock(HttpSession.class);
SecurityTokenHolder securityTokenHolder = mock(SecurityTokenHolder.class);
sm = mock(SecurityManager.class);
when(sessionFactory.getOrCreateSession(null)).thenReturn(httpSession);
when(httpSession.getAttribute(SecurityConstants.SAML_ASSERTION)).thenReturn(securityTokenHolder);
when(securityTokenHolder.getRealmTokenMap()).thenReturn(realmTokenMap);
}
use of ddf.security.common.SecurityTokenHolder in project ddf by codice.
the class LoginFilter method renewSecurityToken.
private SAMLAuthenticationToken renewSecurityToken(HttpSession session, SAMLAuthenticationToken savedToken) throws ServletException, WSSecurityException {
if (session != null) {
SecurityAssertion savedAssertion = new SecurityAssertionImpl(((SecurityToken) savedToken.getCredentials()));
if (savedAssertion.getIssuer() != null && !savedAssertion.getIssuer().equals(SystemBaseUrl.getHost())) {
return null;
}
if (savedAssertion.getNotOnOrAfter() == null) {
return null;
}
long afterMil = savedAssertion.getNotOnOrAfter().getTime();
long timeoutMillis = (afterMil - System.currentTimeMillis());
if (timeoutMillis <= 0) {
throw new InvalidSAMLReceivedException("SAML assertion has expired.");
}
if (timeoutMillis <= 60000) {
// within 60 seconds
try {
LOGGER.debug("Attempting to refresh user's SAML assertion.");
Subject subject = securityManager.getSubject(savedToken);
LOGGER.debug("Refresh of user assertion successful");
for (Object principal : subject.getPrincipals()) {
if (principal instanceof SecurityAssertion) {
SecurityToken token = ((SecurityAssertion) principal).getSecurityToken();
SAMLAuthenticationToken samlAuthenticationToken = new SAMLAuthenticationToken((java.security.Principal) savedToken.getPrincipal(), token, savedToken.getRealm());
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Setting session token - class: {} classloader: {}", token.getClass().getName(), token.getClass().getClassLoader());
}
((SecurityTokenHolder) session.getAttribute(SecurityConstants.SAML_ASSERTION)).addSecurityToken(savedToken.getRealm(), token);
LOGGER.debug("Saved new user assertion to session.");
return samlAuthenticationToken;
}
}
} catch (SecurityServiceException e) {
LOGGER.debug("Unable to refresh user's SAML assertion. User will log out prematurely.", e);
session.invalidate();
} catch (Exception e) {
LOGGER.info("Unhandled exception occurred.", e);
session.invalidate();
}
}
}
return null;
}
use of ddf.security.common.SecurityTokenHolder in project ddf by codice.
the class LoginFilter method getSecurityToken.
private SecurityToken getSecurityToken(HttpSession session, String realm) {
if (session.getAttribute(SecurityConstants.SAML_ASSERTION) == null) {
LOGGER.debug("Security token holder missing from session. New session created improperly.");
return null;
}
SecurityTokenHolder tokenHolder = ((SecurityTokenHolder) session.getAttribute(SecurityConstants.SAML_ASSERTION));
SecurityToken token = tokenHolder.getSecurityToken(realm);
if (token != null) {
SecurityAssertionImpl assertion = new SecurityAssertionImpl(token);
if (!assertion.isPresentlyValid()) {
LOGGER.debug("Session SAML token is invalid. Removing from session.");
tokenHolder.remove(realm);
return null;
}
}
return token;
}
use of ddf.security.common.SecurityTokenHolder in project ddf by codice.
the class LoginFilterTest method testValidUsernameToken.
@Test
public void testValidUsernameToken() throws IOException, XMLStreamException, ServletException, ParserConfigurationException, SAXException, SecurityServiceException {
FilterConfig filterConfig = mock(FilterConfig.class);
LoginFilter loginFilter = new LoginFilter();
loginFilter.setSessionFactory(sessionFactory);
ddf.security.service.SecurityManager securityManager = mock(ddf.security.service.SecurityManager.class);
loginFilter.setSecurityManager(securityManager);
loginFilter.init(filterConfig);
HttpServletRequest servletRequest = mock(HttpServletRequest.class);
HttpServletResponse servletResponse = mock(HttpServletResponse.class);
FilterChain filterChain = mock(FilterChain.class);
UPAuthenticationToken token = new UPAuthenticationToken("foo", "bar");
HandlerResult result = new HandlerResult(HandlerResult.Status.COMPLETED, token);
when(servletRequest.getAttribute("ddf.security.token")).thenReturn(result);
HttpSession session = mock(HttpSession.class);
when(servletRequest.getSession(true)).thenReturn(session);
when(session.getAttribute(SecurityConstants.SAML_ASSERTION)).thenReturn(new SecurityTokenHolder());
when(sessionFactory.getOrCreateSession(servletRequest)).thenReturn(session);
Subject subject = mock(Subject.class, RETURNS_DEEP_STUBS);
when(securityManager.getSubject(token)).thenReturn(subject);
SecurityAssertion assertion = mock(SecurityAssertion.class);
SecurityToken securityToken = mock(SecurityToken.class);
when(assertion.getSecurityToken()).thenReturn(securityToken);
when(subject.getPrincipals().asList()).thenReturn(Arrays.asList(assertion));
when(securityToken.getToken()).thenReturn(readDocument("/good_saml.xml").getDocumentElement());
loginFilter.doFilter(servletRequest, servletResponse, filterChain);
}
Aggregations