Skip to content

Commit 6001645

Browse files
committed
Pac4j authorizers ignored fix #1629
1 parent 7871755 commit 6001645

File tree

4 files changed

+246
-23
lines changed

4 files changed

+246
-23
lines changed

docs/asciidoc/modules/pac4j.adoc

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,57 @@ import io.jooby.pac4j.Pac4jModule
273273

274274
All routes under `/admin` will be protected by Pac4j.
275275

276+
=== Authorizer
277+
278+
Authorizers are registered and group by path. We do provide couple of ways to specific an authorizer:
279+
280+
.Manual configuration
281+
[source, java]
282+
-----
283+
import org.pac4j.core.config.Config;
284+
{
285+
286+
Config pac4j = new Config();
287+
288+
pac4j.addAuthorizer("test", new Authorizer<CommonProfile>() {
289+
@Override public boolean isAuthorized(WebContext context, List<CommonProfile> profiles) {
290+
return false;
291+
}
292+
});
293+
294+
install(
295+
new Pac4jModule(pac4j)
296+
.client("/api/*", "test", conf -> {...});
297+
);
298+
}
299+
-----
300+
301+
.Automatic configuration
302+
[source, java]
303+
-----
304+
{
305+
306+
install(
307+
new Pac4jModule()
308+
.client("/api/*", new MyTestAuthorizer(), conf -> {...});
309+
);
310+
}
311+
-----
312+
313+
.Registry (or dependency injection) integration
314+
[source, java]
315+
-----
316+
{
317+
318+
install(
319+
new Pac4jModule()
320+
.client("/api/*", MyTestAuthorizer.class, conf -> {...});
321+
);
322+
}
323+
-----
324+
325+
This last example ask application registry (dependency injection framework usually) to provisioning the `MyTestAuthorizer` authorizer.
326+
276327
=== Advanced Usage
277328

278329
You can customize default options by using the javadoc:pac4j.Pac4jOptions[] and/or providing your
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.jooby.internal.pac4j;
2+
3+
import io.jooby.Registry;
4+
import org.pac4j.core.authorization.authorizer.Authorizer;
5+
import org.pac4j.core.context.WebContext;
6+
7+
import java.util.List;
8+
9+
public class ForwardingAuthorizer implements Authorizer {
10+
private Registry registry;
11+
12+
private Class authorizerType;
13+
14+
public ForwardingAuthorizer(Class authorizerType) {
15+
this.authorizerType = authorizerType;
16+
}
17+
18+
@Override public boolean isAuthorized(WebContext context, List profiles) {
19+
return authorizer(authorizerType).isAuthorized(context, profiles);
20+
}
21+
22+
private Authorizer authorizer(Class authorizerType) {
23+
return (Authorizer) registry.require(authorizerType);
24+
}
25+
26+
public void setRegistry(Registry registry) {
27+
this.registry = registry;
28+
}
29+
}

modules/jooby-pac4j/src/main/java/io/jooby/internal/pac4j/SecurityFilterImpl.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,22 @@ public class SecurityFilterImpl implements Route.Decorator, Route.Handler {
3434

3535
private String authorizers;
3636

37-
public SecurityFilterImpl(String pattern, Config config, Pac4jOptions options, List<Client> clients) {
37+
public SecurityFilterImpl(String pattern, Config config, Pac4jOptions options,
38+
List<Client> clients, List<String> authorizers) {
3839
this.pattern = pattern;
3940
this.config = config;
4041
this.options = options;
4142
this.clients = clients.stream().map(it -> it.getName())
4243
.collect(Collectors.joining(Pac4jConstants.ELEMENT_SEPARATOR));
44+
authorizers.forEach(this::addAuthorizer);
4345
}
4446

45-
public SecurityFilterImpl addAuthorizer(String authorizer) {
47+
public void addAuthorizer(String authorizer) {
4648
if (authorizers == null) {
4749
authorizers = authorizer;
4850
} else {
4951
authorizers += Pac4jConstants.ELEMENT_SEPARATOR + authorizer;
5052
}
51-
return this;
5253
}
5354

5455
@Nonnull @Override public Route.Handler apply(@Nonnull Route.Handler next) {

0 commit comments

Comments
 (0)