Example usage for javax.security.jacc PolicyContext getContext

List of usage examples for javax.security.jacc PolicyContext getContext

Introduction

In this page you can find the example usage for javax.security.jacc PolicyContext getContext.

Prototype


public static Object getContext(String key) throws javax.security.jacc.PolicyContextException 

Source Link

Document

This method may be used by a Policy provider to activate the PolicyContextHandler registered to the context object key and cause it to return the corresponding policy context object from the container.

Usage

From source file:de.adorsys.oauth.loginmodule.OAuthClientIdLoginModule.java

private <T> T fromPolicyContext(Class<T> type) {
    try {/*from   www  . j  av a  2 s . c o m*/
        return (T) PolicyContext.getContext(type.getName());
    } catch (Exception e) {
        //
    }
    return null;
}

From source file:be.fedict.eid.applet.beta.SessionContextManagerBean.java

public SessionContextEntity getSessionContext() {
    HttpServletRequest httpServletRequest;
    try {/*  w  w w.  j a va  2  s.  co  m*/
        httpServletRequest = (HttpServletRequest) PolicyContext
                .getContext("javax.servlet.http.HttpServletRequest");
    } catch (PolicyContextException e) {
        throw new RuntimeException("JACC error: " + e.getMessage());
    }
    HttpSession httpSession = httpServletRequest.getSession();
    String httpSessionId = httpSession.getId();
    SessionContextEntity sessionContextEntity = getSessionContextEntity(httpSessionId);
    return sessionContextEntity;
}

From source file:adalid.jaas.google.GoogleRecaptcha.java

private static String getRequestParameter(String key) {
    if (key == null) {
        return null;
    }//from  w  w w . j  a v  a 2  s .  c  om
    Object request;
    try {
        request = PolicyContext.getContext(HttpServletRequest.class.getName());
    } catch (PolicyContextException ex) {
        logger.log(Level.SEVERE, ex.toString(), ex);
        return null;
    }
    if (request instanceof HttpServletRequest) {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String parameter = httpServletRequest.getParameter(key);
        logger.log(TRACE, "{0}: {1}", new Object[] { key, parameter });
        return parameter;
    }
    logger.log(TRACE, "HTTP Servlet Request: {0}", request);
    return null;
}

From source file:org.dcm4chex.archive.web.maverick.Dcm4cheeFormController.java

public static Subject getSubject() throws PolicyContextException {
    return (Subject) PolicyContext.getContext(SUBJECT_CONTEXT_KEY);
}

From source file:org.jboss.datavirt.commons.auth.jboss7.SAMLBearerTokenLoginModule.java

/**
 * Gets the current HTTP servlet request.
 * @throws PolicyContextException// w  w  w. ja  va 2  s  .c o m
 */
private HttpServletRequest getCurrentRequest() throws LoginException {
    HttpServletRequest request = HttpRequestThreadLocalValve.TL_request.get();
    if (request == null) {
        try {
            request = (HttpServletRequest) PolicyContext.getContext("javax.servlet.http.HttpServletRequest");
        } catch (Exception e) {
            request = null;
        }
    }
    if (request == null) {
        throw new LoginException("Failed to get current HTTP request.");
    }
    return request;
}

From source file:org.josso.jb5.agent.JBossCatalinaRealm.java

/**
 * Return <code>true</code> if the specified Principal has the specified
 * security role, within the context of this Realm; otherwise return
 * <code>false</code>.//w  ww .  j  a va 2s  . co m
 * 
 * For SSO security domain it creates a GenericPrincipal from 
 * the active authenticated subject before checking roles.
 * 
 * @param principal Principal for whom the role is to be checked
 * @param role Security role to be checked
 */
public boolean hasRole(Principal principal, String role) {
    boolean hasRole = false;

    logger.debug("hasRole(" + principal + "," + role + ")");

    try {
        SecurityContext sc = JBossSecurityAssociationActions.getSecurityContext();
        if (!isSSODomain(sc.getSecurityDomain())) {
            // This is not a SSO Security domain, let JBoss realm handle this ...
            return super.hasRole(principal, role);
        }

        //Subject callerSubject = JBossSecurityAssociationActions.getSubject();
        Subject activeSubject = (Subject) PolicyContext.getContext(SecurityConstants.SUBJECT_CONTEXT_KEY);

        logger.debug("Authenticated Subject: " + activeSubject);

        CatalinaSSOUser ssoUser = CatalinaSSOUser.newInstance(this, activeSubject);
        hasRole = super.hasRole(ssoUser, role);

    } catch (NullPointerException npe) {
        // Just in case ...
        if (logger.isDebugEnabled())
            logger.debug(npe);

        hasRole = super.hasRole(principal, role);

    } catch (PolicyContextException e) {
        logger.error(e, e);
    }

    return hasRole;
}

From source file:org.overlord.commons.auth.jboss7.SAMLBearerTokenLoginModule.java

/**
 * Gets the current HTTP servlet request.
 * @throws PolicyContextException//from w w w  . j av a  2 s  . c  o  m
 */
private HttpServletRequest getCurrentRequest() throws LoginException {
    HttpServletRequest request = HttpRequestThreadLocalValve.TL_request.get();
    if (request == null) {
        try {
            request = (HttpServletRequest) PolicyContext.getContext("javax.servlet.http.HttpServletRequest"); //$NON-NLS-1$
        } catch (Exception e) {
            request = null;
        }
    }
    if (request == null) {
        throw new LoginException("Failed to get current HTTP request."); //$NON-NLS-1$
    }
    return request;
}

From source file:org.overlord.security.eval.jaxrs.auth.SAMLBearerTokenLoginModule.java

/**
 * @see org.jboss.security.auth.spi.AbstractServerLoginModule#login()
 *//*from   w ww . j  a va 2 s  .c o  m*/
@Override
public boolean login() throws LoginException {
    System.out.println("LOGIN called: " + getClass().getSimpleName());
    InputStream is = null;
    try {
        HttpServletRequest request = (HttpServletRequest) PolicyContext
                .getContext("javax.servlet.http.HttpServletRequest");
        System.out.println("Request: " + request);
        String authorization = request.getHeader("Authorization");
        System.out.println("Authorization Header: " + authorization);
        if (authorization != null && authorization.startsWith("Basic")) {
            String b64Data = authorization.substring(6);
            byte[] dataBytes = Base64.decodeBase64(b64Data);
            String data = new String(dataBytes, "UTF-8");
            System.out.println("DATA: " + data);
            if (data.startsWith("SAML-BEARER-TOKEN:")) {
                String assertionData = data.substring(18);
                System.out.println("Assertion DATA: " + assertionData);
                SAMLAssertionParser parser = new SAMLAssertionParser();
                is = new ByteArrayInputStream(assertionData.getBytes("UTF-8"));
                XMLEventReader xmlEventReader = XMLInputFactory.newInstance().createXMLEventReader(is);
                Object parsed = parser.parse(xmlEventReader);
                System.out.println("Parsed Object: " + parsed.getClass());
                AssertionType assertion = (AssertionType) parsed;
                if (validateAssertion(assertion, request) && consumeAssertion(assertion)) {
                    System.out.println("SAML assertion login passed, setting loginOk = true");
                    loginOk = true;
                    return true;
                }
            }
        }
    } catch (LoginException le) {
        throw le;
    } catch (Exception e) {
        e.printStackTrace();
        loginOk = false;
        return false;
    } finally {
        IOUtils.closeQuietly(is);
    }
    return super.login();
}

From source file:org.tolven.web.RegisterAction.java

public String getUserRoleString() throws PolicyContextException {
    Subject subject = (Subject) PolicyContext.getContext("javax.security.auth.Subject.container");
    StringBuffer buff = new StringBuffer();
    if (subject != null) {
        for (Principal principal : subject.getPrincipals()) {
            if (principal instanceof Group && "Roles".equals(principal.getName())) {
                java.util.Enumeration<?> enumeration = ((Group) principal).members();
                Principal rolePrincipal = null;
                while (enumeration.hasMoreElements()) {
                    rolePrincipal = (Principal) enumeration.nextElement();
                    buff.append(rolePrincipal.getName());
                    if (enumeration.hasMoreElements()) {
                        buff.append(", ");
                    }/* w w w. j a v  a2  s.  c o  m*/
                }
                break;
            }
        }
    }
    return buff.toString();
}

From source file:test.be.fedict.eid.applet.InsecureClientEnvironmentWarningServiceImpl.java

public void checkSecureClientEnvironment(String javaVersion, String javaVendor, String osName, String osArch,
        String osVersion, String userAgent, String navigatorAppName, String navigatorAppVersion,
        String navigatorUserAgent, String remoteAddress, Integer sslKeySize, String sslCipherSuite,
        List<String> readerList) throws InsecureClientEnvironmentException {
    LOG.debug("insecure warning");

    LOG.debug("java version: " + javaVersion);
    LOG.debug("java vendor: " + javaVendor);
    LOG.debug("OS name: " + osName);
    LOG.debug("OS arch: " + osArch);
    LOG.debug("OS version: " + osVersion);
    LOG.debug("user agent: " + userAgent);
    LOG.debug("navigator app name: " + navigatorAppName);
    LOG.debug("navigator app version: " + navigatorAppVersion);
    LOG.debug("navigator user agent: " + navigatorUserAgent);
    LOG.debug("remote address: " + remoteAddress);
    LOG.debug("ssl key size: " + sslKeySize);
    LOG.debug("ssl cipher suite: " + sslCipherSuite);
    LOG.debug("readers: " + readerList);

    HttpServletRequest httpServletRequest;
    try {//from  w w w  .  j  a  v a  2 s  . c  o m
        httpServletRequest = (HttpServletRequest) PolicyContext
                .getContext("javax.servlet.http.HttpServletRequest");
    } catch (PolicyContextException e) {
        throw new RuntimeException("JACC error: " + e.getMessage());
    }

    HttpSession httpSession = httpServletRequest.getSession();
    httpSession.setAttribute("ClientJavaVersion", javaVersion);
    httpSession.setAttribute("ClientJavaVendor", javaVendor);
    httpSession.setAttribute("ClientOSName", osName);
    httpSession.setAttribute("ClientOSArch", osArch);
    httpSession.setAttribute("ClientOSVersion", osVersion);
    httpSession.setAttribute("ClientReaders", readerList.toString());
    httpSession.setAttribute("ClientUserAgent", userAgent);
    httpSession.setAttribute("ClientSslCipherSuite", sslCipherSuite);
    httpSession.setAttribute("ClientRemoteAddress", remoteAddress);
    httpSession.setAttribute("ClientSslKeySize", sslKeySize);
    httpSession.setAttribute("ClientNavigatorUserAgent", navigatorUserAgent);
    httpSession.setAttribute("ClientNavigatorAppName", navigatorAppName);
    httpSession.setAttribute("ClientNavigatorAppVersion", navigatorAppVersion);

    throw new InsecureClientEnvironmentException(true);
}