Skip to content

Latest commit

 

History

435 Commits

Folders and files

Repository files navigation

undertow-pac4j

Maven Central Build status Java 17+ Undertow 2.4 Apache 2 license

undertow-pac4j is the Undertow implementation of pac4j, the security engine for Java. If it is useful to you, please ⭐ star pac4j on GitHub: it helps other developers discover it!

The undertow-pac4j project is an easy and powerful security library for Undertow web applications which supports authentication and authorization, but also application logout and advanced features like CSRF protection. It's based on Java 17, Undertow 2.4 and on the pac4j security engine v6. It's available under the Apache 2 license.

Main concepts and components:

  1. A client represents an authentication mechanism. It performs the login process and returns a user profile. An indirect client is for web application authentication while a direct client is for web services authentication:

▸ OpenID Connect - SAML - CAS - OAuth - HTTP - LDAP - SQL - JWT - MongoDB - Kerberos - IP address - REST API

  1. An authorizer is meant to check authorizations on the authenticated user profile(s) or on the current web context:

▸ Roles / permissions - Anonymous / remember-me / (fully) authenticated - Profile type, attribute - CORS - CSRF - Security headers - IP address, HTTP method

  1. A matcher defines whether the SecurityHandler must be applied and can be used for additional web processing

  2. The SecurityHandler protects an url by checking that the user is authenticated and that the authorizations are valid, according to the clients and authorizers configuration. If the user is not authenticated, it performs authentication for direct clients or starts the login process for indirect clients

  3. The CallbackHandler finishes the login process for an indirect client

  4. The LogoutHandler logs out the user from the application and triggers the logout at the identity provider level.

Quick start (OpenID Connect)

Add Undertow 2.4 (it's a provided dependency), undertow-pac4j and the OpenID Connect module:

<dependency>
    <groupId>io.undertow</groupId>
    <artifactId>undertow-core</artifactId>
    <version>2.4.3.Final</version>
</dependency>
<dependency>
    <groupId>org.pac4j</groupId>
    <artifactId>undertow-pac4j</artifactId>
    <version>6.1.0</version>
</dependency>
<dependency>
    <groupId>org.pac4j</groupId>
    <artifactId>pac4j-oidc</artifactId>
    <version>6.5.9</version>
</dependency>

Then define your identity provider and register the handlers, wrapped by the Undertow session handler:

final var oidc = new OidcConfiguration()
    .setDiscoveryURI("https://www.casserverpac4j.dev/oidc/.well-known/openid-configuration")
    .setClientId("myclient")
    .setSecret("mysecret")
    .setAllowUnsignedIdTokens(true); // only for this demo server
final var config = new Config("http://localhost:8080/callback", new OidcClient(oidc));

final var path = new PathHandler();
path.addExactPath("/protected", SecurityHandler.build(protectedHandler, config, "OidcClient"));
path.addExactPath("/callback", CallbackHandler.build(config));
path.addExactPath("/logout", new LogoutHandler(config, "/"));

Undertow.builder()
    .addHttpListener(8080, "localhost")
    .setHandler(new SessionAttachmentHandler(path, new InMemorySessionManager("sessions"), new SessionCookieConfig()))
    .build()
    .start();

That's it: /protected now requires an OpenID Connect login, and the authenticated user is available as a Pac4jAccount via exchange.getSecurityContext().getAuthenticatedAccount(). Read the full guide for more details (session, logout, profiles...).

Usage

2) Define:

- the callback configuration, only for web applications

Demo

The demo webapp: undertow-pac4j-demo is available for tests and implement many authentication mechanisms: Facebook, Twitter, form, basic auth, CAS, SAML, OpenID Connect, JWT...

Versions

The latest released version is the Maven Central. The next version is under development.

See the release notes.

See the migration guide as well.

Need help?

You can use the mailing lists or the commercial support.

Releases

Packages

Used by

Contributors

Languages