<commit_before><p align="center">
  <img src="https://pac4j.github.io/pac4j/img/logo-dropwizard.png" width="300" />
</p>

# dropwizard-pac4j

A [Dropwizard](http://www.dropwizard.io/) bundle for securing REST endpoints
using [pac4j](http://www.pac4j.org/).

## Usage

`dropwizard-pac4j` provides two components which must be integrated into
applications:

- A configuration factory populated by values from a `pac4j` section within an
  application's config file.
- A Dropwizard [bundle](http://www.dropwizard.io/1.0.2/docs/manual/core.html#bundles)
  which:
    - connects the values defined in the `pac4j` configuration section to the
      [`jax-rs-pac4j`](https://github.com/pac4j/jax-rs-pac4j/) library.
    - enables the use of the annotation provided in by the
      [`jax-rs-pac4j`](https://github.com/pac4j/jax-rs-pac4j/) library.

### Dependencies (`dropwizard-pac4j` + `pac4j-*` libraries)

You need to add a dependency on:

- the `dropwizard-pac4j` library (<em>groupId</em>: **org.pac4j**, *version*:
**1.0.0**)
- the appropriate `pac4j` [submodules](http://www.pac4j.org/docs/clients.html)
(<em>groupId</em>: **org.pac4j**, *version*: **1.9.4**): `pac4j-oauth` for
OAuth support (Facebook, Twitter...), `pac4j-cas` for CAS support, `pac4j-ldap`
for LDAP authentication, etc.

All released artifacts are available in the
[Maven central repository](http://search.maven.org/#search%7Cga%7C1%7Cpac4j).

### Installing the bundle

Add the bundle within the application class' `initialize` method, just like any
other bundle:

```java
public class MySecureApplication extends Application<MySecureConfiguration> {
    final Pac4jBundle<MySecureConfiguration> bundle = new Pac4jBundle<MySecureConfiguration>() {
        @Override
        public Pac4jFactory getPac4jFactory(MySecureConfiguration configuration) {
            return configuration.getPac4jFactory();
        }
    };

    @Override
    public void initialize(Bootstrap<TestConfiguration> bootstrap) {
        bootstrap.addBundle(bundle);
    }

    ...
```

It can be useful to store the bundle in its own field in order to be able to
access pac4j configuration as shown at the end of the next section.

### Configuring the bundle

Update the application's configuration class to expose accessor methods for
`Pac4jFactory`:

```java
public class MySecureConfiguration extends Configuration {
    @NotNull
    Pac4jFactory pac4jFactory = new Pac4jFactory();

    @JsonProperty("pac4j")
    public Pac4jFactory getPac4jFactory() {
        return pac4jFactory;
    }

    @JsonProperty("pac4j")
    public void setPac4jFactory(Pac4jFactory pac4jFactory) {
        this.pac4jFactory = pac4jFactory;
    }
}
```

Note that it is also possible to have `pac4jFactory` be nullable and in this
case, the bundle will be disabled.

Add a `pac4j` section to a Dropwizard application's configuration file:

```yaml
pac4j:
  # those protect the whole application at Jersey level
  globalFilters:
    - matchers: excludeUserSession
      authorizers: isAuthenticated
  matchers:
    # this let the /user/session url be handled by the annotations
    excludeUserSession:
      class: org.pac4j.core.matching.ExcludedPathMatcher
      excludePath: ^/user/session$
  callbackUrl: /user/session<commit_msg>Clarifies nulled factory behaviour<commit_after><p align="center">
  <img src="https://pac4j.github.io/pac4j/img/logo-dropwizard.png" width="300" />
</p>

# dropwizard-pac4j

A [Dropwizard](http://www.dropwizard.io/) bundle for securing REST endpoints
using [pac4j](http://www.pac4j.org/).

## Usage

`dropwizard-pac4j` provides two components which must be integrated into
applications:

- A configuration factory populated by values from a `pac4j` section within an
  application's config file.
- A Dropwizard [bundle](http://www.dropwizard.io/1.0.2/docs/manual/core.html#bundles)
  which:
    - connects the values defined in the `pac4j` configuration section to the
      [`jax-rs-pac4j`](https://github.com/pac4j/jax-rs-pac4j/) library.
    - enables the use of the annotation provided in by the
      [`jax-rs-pac4j`](https://github.com/pac4j/jax-rs-pac4j/) library.

### Dependencies (`dropwizard-pac4j` + `pac4j-*` libraries)

You need to add a dependency on:

- the `dropwizard-pac4j` library (<em>groupId</em>: **org.pac4j**, *version*:
**1.0.0**)
- the appropriate `pac4j` [submodules](http://www.pac4j.org/docs/clients.html)
(<em>groupId</em>: **org.pac4j**, *version*: **1.9.4**): `pac4j-oauth` for
OAuth support (Facebook, Twitter...), `pac4j-cas` for CAS support, `pac4j-ldap`
for LDAP authentication, etc.

All released artifacts are available in the
[Maven central repository](http://search.maven.org/#search%7Cga%7C1%7Cpac4j).

### Installing the bundle

Add the bundle within the application class' `initialize` method, just like any
other bundle:

```java
public class MySecureApplication extends Application<MySecureConfiguration> {
    final Pac4jBundle<MySecureConfiguration> bundle = new Pac4jBundle<MySecureConfiguration>() {
        @Override
        public Pac4jFactory getPac4jFactory(MySecureConfiguration configuration) {
            return configuration.getPac4jFactory();
        }
    };

    @Override
    public void initialize(Bootstrap<TestConfiguration> bootstrap) {
        bootstrap.addBundle(bundle);
    }

    ...
```

It can be useful to store the bundle in its own field in order to be able to
access pac4j configuration as shown at the end of the next section.

### Configuring the bundle

Update the application's configuration class to expose accessor methods for
`Pac4jFactory`:

```java
public class MySecureConfiguration extends Configuration {
    @NotNull
    Pac4jFactory pac4jFactory = new Pac4jFactory();

    @JsonProperty("pac4j")
    public Pac4jFactory getPac4jFactory() {
        return pac4jFactory;
    }

    @JsonProperty("pac4j")
    public void setPac4jFactory(Pac4jFactory pac4jFactory) {
        this.pac4jFactory = pac4jFactory;
    }
}
```

Note that it is also possible to have `pac4jFactory` be nullable and in this
case, pac4j won't be configured but pac4j's type will be readable in the
configuration. If the latter is not desired, do not use this bundle!

Add a `pac4j` section to a Dropwizard application's configuration file:

```yaml
pac4j:
  # those protect the whole application at Jersey level
  globalFilters:
    - matchers: excludeUserSession
      authorizers: isAuthenticated
  matchers:
    # this let the /user/session url be handled by the annotations
    excludeUserSession:
      class: org.pac4j.core.matching.ExcludedPathMatcher
      excludePath: ^/user/session$
  callbackUrl: /user/session
{'fraction_non_alphanumeric': 0.11047561709813365, 'fraction_numerical': 0.019867549668874173, 'mean_word_length': 4.060929169840061, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 6, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2688376', 'n_tokens_mistral': 2194, 'n_tokens_neox': 2044, 'n_words': 609}