Skip to content

Commit

Permalink
Add defaultClient and defaultClients configuration
Browse files Browse the repository at this point in the history
The first one is for setting the Config/Clients default client used by pac4j.
The second one is for setting a default value to Pac4JSecurity annotation.
  • Loading branch information
victornoel committed Jan 4, 2017
1 parent 5a10a8b commit 34b7619
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ pac4j:
class: org.pac4j.core.matching.ExcludedPathMatcher
excludePath: ^/user/session$
callbackUrl: /user/session
defaultClient: DirectBasicAuthClient
clients:
- org.pac4j.http.client.direct.DirectBasicAuthClient:
authenticator:
Expand Down Expand Up @@ -131,6 +132,15 @@ name of its class, but it can also be set explictly.
Their name can be used in `filter`'s `clients` as well as in the
`Pac4JSecurity` annotation.

- `defaultClient`: the name of one of the client configured via `clients`.
It will be used as the default pac4j `Client`. Pac4j exploits it in particular
when no client is specified during callback, but also when no clients are
specified on a security filter.

- `defaultClients`: the names (separated by commas) of some of the clients
configured via `clients`. They will be used as the default value for the
`clients` parameter of the `Pac4JSecurity` annotation.

To specify instances of `Client`, `Authenticator`, `PasswordEncoder`,
`CredentialsExtractor`, `ProfileCreator`, `AuthorizationGenerator`,
`Authorizer`, `Matcher`, `CallbackUrlResolver` and `RedirectActionBuilder`, it
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/pac4j/dropwizard/Pac4jBundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public final void run(T configuration, Environment environment)

environment.jersey()
.register(new ServletJaxRsContextFactoryProvider(config));
environment.jersey().register(new Pac4JSecurityFeature(config));
environment.jersey().register(new Pac4JSecurityFeature(config,
pac4j.getDefaultClients()));
environment.jersey()
.register(new Pac4JValueFactoryProvider.Binder());

Expand Down
51 changes: 51 additions & 0 deletions src/main/java/org/pac4j/dropwizard/Pac4jFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public class Pac4jFactory {
@NotNull
private List<Client> clients = new ArrayList<>();

private String defaultClient = null;

private String defaultClients = null;

@NotNull
private Map<String, Authorizer> authorizers = new HashMap<>();

Expand Down Expand Up @@ -116,6 +120,38 @@ public void setClients(List<Client> clients) {
this.clients = clients;
}

/**
* @since 1.1.0
*/
@JsonProperty
public String getDefaultClient() {
return defaultClient;
}

/**
* @since 1.1.0
*/
@JsonProperty
public void setDefaultClient(String defaultClient) {
this.defaultClient = defaultClient;
}

/**
* @since 1.1.0
*/
@JsonProperty
public String getDefaultClients() {
return defaultClients;
}

/**
* @since 1.1.0
*/
@JsonProperty
public void setDefaultClients(String defaultClients) {
this.defaultClients = defaultClients;
}

@JsonProperty
public Map<String, Authorizer> getAuthorizers() {
return authorizers;
Expand Down Expand Up @@ -170,6 +206,21 @@ public Config build() {
client.setAuthorizationGenerators(authorizationGenerators);
client.setClients(clients);

if (defaultClient != null) {
boolean found = false;
for (Client c : clients) {
if (defaultClient.equals(c.getName())) {
client.setDefaultClient(c);
found = true;
break;
}
}
if (!found) {
throw new IllegalArgumentException("Client '" + defaultClient
+ "' is not one of the configured clients");
}
}

config.setAuthorizers(authorizers);
config.setMatchers(matchers);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,33 @@ public void defaultsUnset() throws Exception {
.isEqualTo(new Clients().getClientNameParameter());
}

@Test(expected = IllegalArgumentException.class)
public void defaultClientTestNOk() throws Exception {
Pac4jFactory conf = getPac4jFactory("defaultClientNOk.yaml");

conf.build();
}

@Test
public void defaultClientTestOk() throws Exception {
Pac4jFactory conf = getPac4jFactory("defaultClientOk.yaml");

Config config = conf.build();
Clients clients = config.getClients();

assertThat(clients.getClients().get(0))
.isEqualTo(clients.getDefaultClient());
}

@Test
public void defaultClientTestOk2() throws Exception {
Pac4jFactory conf = getPac4jFactory("defaultClientOk2.yaml");

Config config = conf.build();
Clients clients = config.getClients();

assertThat(clients.getClients().get(0))
.isEqualTo(clients.getDefaultClient());
}

}
4 changes: 4 additions & 0 deletions src/test/resources/defaultClientNOk.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
clients:
- org.pac4j.http.client.direct.DirectBasicAuthClient:
name: basic
defaultClient: FormClient
4 changes: 4 additions & 0 deletions src/test/resources/defaultClientOk.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
clients:
- org.pac4j.http.client.direct.DirectBasicAuthClient:
name: basic
defaultClient: basic
3 changes: 3 additions & 0 deletions src/test/resources/defaultClientOk2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
clients:
- org.pac4j.http.client.direct.DirectBasicAuthClient: {}
defaultClient: DirectBasicAuthClient

0 comments on commit 34b7619

Please sign in to comment.