org.pac4j.core.context.WebContext Java Examples

The following examples show how to use org.pac4j.core.context.WebContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClientAction.java    From springboot-shiro-cas-mybatis with MIT License 6 votes vote down vote up
/**
 * Prepare the data for the login page.
 *
 * @param context The current webflow context
 */
protected void prepareForLoginPage(final RequestContext context) {
    final HttpServletRequest request = WebUtils.getHttpServletRequest(context);
    final HttpServletResponse response = WebUtils.getHttpServletResponse(context);
    final HttpSession session = request.getSession();

    // web context
    final WebContext webContext = new J2EContext(request, response);

    // save parameters in web session
    final WebApplicationService service = WebUtils.getService(context);
    logger.debug("save service: {}", service);
    session.setAttribute(SERVICE, service);
    saveRequestParameter(request, session, THEME);
    saveRequestParameter(request, session, LOCALE);
    saveRequestParameter(request, session, METHOD);

    // for all clients, generate redirection urls
    for (final Client client : this.clients.findAllClients()) {
        final String key = client.getName() + "Url";
        final BaseClient baseClient = (BaseClient) client;
        final String redirectionUrl = baseClient.getRedirectionUrl(webContext);
        logger.debug("{} -> {}", key, redirectionUrl);
        context.getFlowScope().put(key, redirectionUrl);
    }
}
 
Example #2
Source File: ClientAction.java    From cas4.0.x-server-wechat with Apache License 2.0 6 votes vote down vote up
/**
 * Prepare the data for the login page.
 *
 * @param context The current webflow context
 */
protected void prepareForLoginPage(final RequestContext context) {
    final HttpServletRequest request = WebUtils.getHttpServletRequest(context);
    final HttpServletResponse response = WebUtils.getHttpServletResponse(context);
    final HttpSession session = request.getSession();

    // web context
    final WebContext webContext = new J2EContext(request, response);

    // save parameters in web session
    final Service service = (Service) context.getFlowScope().get(SERVICE);
    logger.info("save service: {}", service);
    session.setAttribute(SERVICE, service);
    saveRequestParameter(request, session, THEME);
    saveRequestParameter(request, session, LOCALE);
    saveRequestParameter(request, session, METHOD);

    // for all clients, generate redirection urls
    for (final Client client : this.clients.findAllClients()) {
        final String key = client.getName() + "Url";
        final BaseClient baseClient = (BaseClient) client;
        final String redirectionUrl = baseClient.getRedirectionUrl(webContext);
        logger.info("{} -> {}", key, redirectionUrl);
        context.getFlowScope().put(key, redirectionUrl);
    }
}
 
Example #3
Source File: Pac4jProducer.java    From jee-pac4j with Apache License 2.0 5 votes vote down vote up
/**
 * Factory method which produces a pac4j profile manager.
 *
 * @param webContext the web context to be used for building the profile manager
 * @return a profile manager associated with the current servlet request
 */
@Produces
ProfileManager getProfileManager(final WebContext webContext) {
    logger.trace("Producing a pac4j profile manager...");
    ProfileManager profileManager = new ProfileManager(webContext);
    logger.trace("Returning a pac4j profile manager.");
    return profileManager;
}
 
Example #4
Source File: KnoxSessionStore.java    From knox with Apache License 2.0 5 votes vote down vote up
@Override
public Object get(WebContext context, String key) {
    final Cookie cookie = ContextHelper.getCookie(context, PAC4J_SESSION_PREFIX + key);
    Object value = null;
    if (cookie != null) {
        value = uncompressDecryptBase64(cookie.getValue());
    }
    logger.debug("Get from session: {} = {}", key, value);
    return value;
}
 
Example #5
Source File: JaxRsUrlResolverTest.java    From jax-rs-pac4j with Apache License 2.0 5 votes vote down vote up
@Test
public void relative_url_with_non_jaxrs_context_is_left_unresolved() {
    WebContext context = mock(WebContext.class);
    
    JaxRsUrlResolver resolver = new JaxRsUrlResolver();
    String resolvedUrl = resolver.compute("/a/relative/url", context);
    assertThat(resolvedUrl, is("/a/relative/url"));
}
 
Example #6
Source File: JaxRsUrlResolverTest.java    From jax-rs-pac4j with Apache License 2.0 5 votes vote down vote up
@Test
public void null_url_with_non_jaxrs_context_resolves_as_null() {
    WebContext context = mock(WebContext.class);
    
    JaxRsUrlResolver resolver = new JaxRsUrlResolver();
    String resolvedUrl = resolver.compute(null, context);
    assertThat(resolvedUrl, is(nullValue()));
}
 
Example #7
Source File: JaxRsUrlResolver.java    From jax-rs-pac4j with Apache License 2.0 5 votes vote down vote up
@Override
public String compute(String url, WebContext context) {
    if (context instanceof JaxRsContext && url != null) {
        return ((JaxRsContext) context).getAbsolutePath(url, true);
    }
    return url;
}
 
Example #8
Source File: ClientAuthenticationHandler.java    From cas4.0.x-server-wechat with Apache License 2.0 5 votes vote down vote up
@Override
protected HandlerResult doAuthentication(final Credential credential) throws GeneralSecurityException, PreventedException {
    final ClientCredential clientCredentials = (ClientCredential) credential;
    logger.debug("clientCredentials : {}", clientCredentials);

    final String clientName = clientCredentials.getCredentials().getClientName();
    logger.debug("clientName : {}", clientName);

    // get client
    final Client<org.pac4j.core.credentials.Credentials, UserProfile> client = this.clients.findClient(clientName);
    logger.debug("client : {}", client);

    // web context
    final ServletExternalContext servletExternalContext = (ServletExternalContext) ExternalContextHolder.getExternalContext();
    final HttpServletRequest request = (HttpServletRequest) servletExternalContext.getNativeRequest();
    final HttpServletResponse response = (HttpServletResponse) servletExternalContext.getNativeResponse();
    final WebContext webContext = new J2EContext(request, response);

    // get user profile
    final UserProfile userProfile = client.getUserProfile(clientCredentials.getCredentials(), webContext);
    logger.debug("userProfile : {}", userProfile);

    if (userProfile != null && StringUtils.isNotBlank(userProfile.getTypedId())) {
        clientCredentials.setUserProfile(userProfile);
        return new HandlerResult(
                this,
                new BasicCredentialMetaData(credential),
                new SimplePrincipal(userProfile.getTypedId(), userProfile.getAttributes()));
    }

    throw new FailedLoginException("Provider did not produce profile for " + clientCredentials);
}
 
Example #9
Source File: AbstractClientAuthenticationHandler.java    From springboot-shiro-cas-mybatis with MIT License 5 votes vote down vote up
@Override
protected HandlerResult doAuthentication(final Credential credential) throws GeneralSecurityException, PreventedException {
    final ClientCredential clientCredentials = (ClientCredential) credential;
    logger.debug("clientCredentials : {}", clientCredentials);

    final Credentials credentials = clientCredentials.getCredentials();
    final String clientName = credentials.getClientName();
    logger.debug("clientName : {}", clientName);

    // get client
    final Client<Credentials, UserProfile> client = this.clients.findClient(clientName);
    logger.debug("client : {}", client);

    // web context
    final ServletExternalContext servletExternalContext = (ServletExternalContext) ExternalContextHolder.getExternalContext();
    final HttpServletRequest request = (HttpServletRequest) servletExternalContext.getNativeRequest();
    final HttpServletResponse response = (HttpServletResponse) servletExternalContext.getNativeResponse();
    final WebContext webContext = new J2EContext(request, response);
    
    // get user profile
    final UserProfile userProfile = client.getUserProfile(credentials, webContext);
    logger.debug("userProfile : {}", userProfile);

    if (userProfile != null) {
        return createResult(clientCredentials, userProfile);
    }

    throw new FailedLoginException("Provider did not produce a user profile for: " + clientCredentials);
}
 
Example #10
Source File: WeiXinClient.java    From cas4.0.x-server-wechat with Apache License 2.0 4 votes vote down vote up
protected boolean hasBeenCancelled(WebContext context) {
    return false;
}
 
Example #11
Source File: WeiXinClientOauth.java    From cas4.0.x-server-wechat with Apache License 2.0 4 votes vote down vote up
protected boolean hasBeenCancelled(WebContext context) {
    return false;
}
 
Example #12
Source File: MockFacebookClient.java    From cas4.0.x-server-wechat with Apache License 2.0 4 votes vote down vote up
@Override
protected OAuthCredentials retrieveCredentials(final WebContext context) {
    return new OAuthCredentials("fakeVerifier", getName());
}
 
Example #13
Source File: Pac4jFactory.java    From dropwizard-pac4j with Apache License 2.0 4 votes vote down vote up
@JsonProperty
public Function<WebContext, ProfileManager> getProfileManagerFactory() {
    return profileManagerFactory;
}
 
Example #14
Source File: JaxRsAjaxRequestResolver.java    From jax-rs-pac4j with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isAjax(WebContext context) {
    return true;
}
 
Example #15
Source File: ClientAction.java    From cas4.0.x-server-wechat with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected Event doExecute(final RequestContext context) throws Exception {
    final HttpServletRequest request = WebUtils.getHttpServletRequest(context);
    final HttpServletResponse response = WebUtils.getHttpServletResponse(context);
    final HttpSession session = request.getSession();

    // web context
    final WebContext webContext = new J2EContext(request, response);

    // get client
    //final String clientName = request.getParameter(this.clients.getClientNameParameter());
    final String clientName = request.getParameter("state");
    //logger.debug("clientName : {}", clientName);
    logger.info("clientName : {}", clientName);

    // it's an authentication
    if (StringUtils.isNotBlank(clientName)) {
        // get client
        final BaseClient<Credentials, CommonProfile> client =
                (BaseClient<Credentials, CommonProfile>) this.clients
                .findClient(clientName);
        logger.info("client : {}", client);

        // Only supported protocols
        final Mechanism mechanism = client.getMechanism();
        logger.info("mechanism == " + mechanism.name());
        if (!SUPPORTED_PROTOCOLS.contains(mechanism)) {
            throw new TechnicalException("Only CAS, OAuth, OpenID and SAML protocols are supported: " + client);
        }

        // get credentials
        final Credentials credentials;
        try {
            credentials = client.getCredentials(webContext);
            logger.info("credentials : {}", credentials);
        } catch (final RequiresHttpAction e) {
            logger.info("requires http action : {}", e);
            response.flushBuffer();
            ExternalContext externalContext = ExternalContextHolder.getExternalContext();
            externalContext.recordResponseComplete();
            return new Event(this, "stop");
        }

        // retrieve parameters from web session
        final Service service = (Service) session.getAttribute(SERVICE);
        context.getFlowScope().put(SERVICE, service);
        logger.info("retrieve service: {}", service);
        if (service != null) {
            request.setAttribute(SERVICE, service.getId());
        }
        restoreRequestAttribute(request, session, THEME);
        restoreRequestAttribute(request, session, LOCALE);
        restoreRequestAttribute(request, session, METHOD);

        // credentials not null -> try to authenticate
        if (credentials != null) {
            logger.info("credentials is not null : {}", credentials);
            WebUtils.putTicketGrantingTicketInRequestScope(context,
                    this.centralAuthenticationService.createTicketGrantingTicket(new ClientCredential(credentials)));
            return success();
        }
    }

    // no or aborted authentication : go to login page
    prepareForLoginPage(context);
    return error();
}
 
Example #16
Source File: MockFacebookClient.java    From springboot-shiro-cas-mybatis with MIT License 4 votes vote down vote up
@Override
protected FacebookProfile retrieveUserProfile(final OAuthCredentials credentials, final WebContext context) {
    return facebookProfile;
}
 
Example #17
Source File: MockFacebookClient.java    From springboot-shiro-cas-mybatis with MIT License 4 votes vote down vote up
@Override
protected OAuthCredentials retrieveCredentials(final WebContext context) {
    return new OAuthCredentials("fakeVerifier", getName());
}
 
Example #18
Source File: KnoxSessionStore.java    From knox with Apache License 2.0 4 votes vote down vote up
@Override
public String getOrCreateSessionId(WebContext context) {
    return null;
}
 
Example #19
Source File: KnoxSessionStore.java    From knox with Apache License 2.0 4 votes vote down vote up
@Override
public SessionStore buildFromTrackableSession(WebContext arg0, Object arg1) {
    return null;
}
 
Example #20
Source File: KnoxSessionStore.java    From knox with Apache License 2.0 4 votes vote down vote up
@Override
public boolean destroySession(WebContext arg0) {
    return false;
}
 
Example #21
Source File: KnoxSessionStore.java    From knox with Apache License 2.0 4 votes vote down vote up
@Override
public Object getTrackableSession(WebContext arg0) {
    return null;
}
 
Example #22
Source File: KnoxSessionStore.java    From knox with Apache License 2.0 4 votes vote down vote up
@Override
public boolean renewSession(final WebContext context) {
    return false;
}
 
Example #23
Source File: ClientAction.java    From springboot-shiro-cas-mybatis with MIT License 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected Event doExecute(final RequestContext context) throws Exception {
    final HttpServletRequest request = WebUtils.getHttpServletRequest(context);
    final HttpServletResponse response = WebUtils.getHttpServletResponse(context);
    final HttpSession session = request.getSession();

    // web context
    final WebContext webContext = new J2EContext(request, response);

    // get client
    final String clientName = request.getParameter(this.clients.getClientNameParameter());
    logger.debug("clientName: {}", clientName);

    // it's an authentication
    if (StringUtils.isNotBlank(clientName)) {
        // get client
        final BaseClient<Credentials, CommonProfile> client =
                (BaseClient<Credentials, CommonProfile>) this.clients
                .findClient(clientName);
        logger.debug("client: {}", client);

        // Only supported protocols
        final Mechanism mechanism = client.getMechanism();
        if (!SUPPORTED_PROTOCOLS.contains(mechanism)) {
            throw new TechnicalException("Only CAS, OAuth, OpenID and SAML protocols are supported: " + client);
        }

        // get credentials
        final Credentials credentials;
        try {
            credentials = client.getCredentials(webContext);
            logger.debug("credentials: {}", credentials);
        } catch (final RequiresHttpAction e) {
            logger.debug("requires http action: {}", e);
            response.flushBuffer();
            final ExternalContext externalContext = ExternalContextHolder.getExternalContext();
            externalContext.recordResponseComplete();
            return new Event(this, "stop");
        }

        // retrieve parameters from web session
        final Service service = (Service) session.getAttribute(SERVICE);
        context.getFlowScope().put(SERVICE, service);
        logger.debug("retrieve service: {}", service);
        if (service != null) {
            request.setAttribute(SERVICE, service.getId());
        }
        restoreRequestAttribute(request, session, THEME);
        restoreRequestAttribute(request, session, LOCALE);
        restoreRequestAttribute(request, session, METHOD);

        // credentials not null -> try to authenticate
        if (credentials != null) {
            final TicketGrantingTicket tgt = 
                    this.centralAuthenticationService.createTicketGrantingTicket(new ClientCredential(credentials));
            WebUtils.putTicketGrantingTicketInScopes(context, tgt);
            return success();
        }
    }

    // no or aborted authentication : go to login page
    prepareForLoginPage(context);
    return error();
}
 
Example #24
Source File: Pac4jFactory.java    From dropwizard-pac4j with Apache License 2.0 2 votes vote down vote up
/**
 * @since 2.0.0
 * @param profileManagerFactory
 *            a class implementing a function from context to profile
 *            manager
 */
@JsonProperty
public void setProfileManagerFactory(
        Function<WebContext, ProfileManager> profileManagerFactory) {
    this.profileManagerFactory = profileManagerFactory;
}