Skip to content

Commit b042461

Browse files
author
Vladimir Kotal
committed
configurable decoder for UserPlugin
fixes #2737
1 parent 7f50307 commit b042461

File tree

2 files changed

+51
-20
lines changed

2 files changed

+51
-20
lines changed

plugins/src/opengrok/auth/plugin/UserPlugin.java

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
*/
2323
package opengrok.auth.plugin;
2424

25+
import java.lang.reflect.Constructor;
26+
import java.lang.reflect.InvocationTargetException;
2527
import java.util.Map;
28+
import java.util.logging.Level;
29+
import java.util.logging.Logger;
2630
import javax.servlet.http.HttpServletRequest;
27-
import opengrok.auth.plugin.decoders.FakeOSSOHeaderDecoder;
28-
import opengrok.auth.plugin.decoders.OSSOHeaderDecoder;
2931
import opengrok.auth.plugin.decoders.IUserDecoder;
3032
import opengrok.auth.plugin.entity.User;
3133
import org.opengrok.indexer.authorization.IAuthorizationPlugin;
@@ -39,43 +41,70 @@
3941
*/
4042
public class UserPlugin implements IAuthorizationPlugin {
4143

42-
private static final String FAKE_PARAM = "fake";
44+
private static final Logger LOGGER = Logger.getLogger(UserPlugin.class.getName());
45+
46+
private static final String DECODER_CLASS_PARAM = "decoder";
4347

4448
public static final String REQUEST_ATTR = "opengrok-user-plugin-user";
4549

46-
private IUserDecoder decoder = new OSSOHeaderDecoder();
50+
private IUserDecoder decoder;
51+
52+
public UserPlugin() {
53+
}
54+
55+
// for testing
56+
protected UserPlugin(IUserDecoder decoder) {
57+
this.decoder = decoder;
58+
}
59+
60+
private IUserDecoder getDecoder(String name) throws ClassNotFoundException, NoSuchMethodException,
61+
IllegalAccessException, InvocationTargetException, InstantiationException {
62+
Class<?> clazz = Class.forName(name);
63+
Constructor<?> constructor = clazz.getConstructor();
64+
Object instance = constructor.newInstance();
65+
return (IUserDecoder)instance;
66+
}
4767

4868
@Override
4969
public void load(Map<String, Object> parameters) {
50-
Boolean fake;
70+
String decoder_name;
71+
if ((decoder_name = (String) parameters.get(DECODER_CLASS_PARAM)) == null) {
72+
throw new NullPointerException(String.format("missing " +
73+
"parameter '%s' in %s configuration",
74+
DECODER_CLASS_PARAM, UserPlugin.class.getName()));
75+
}
5176

52-
if ((fake = (Boolean) parameters.get(FAKE_PARAM)) != null
53-
&& fake) {
54-
decoder = new FakeOSSOHeaderDecoder();
77+
LOGGER.log(Level.INFO, "loading decoder: {0}", decoder_name);
78+
try {
79+
decoder = getDecoder(decoder_name);
80+
} catch (ClassNotFoundException|NoSuchMethodException|IllegalAccessException|
81+
InvocationTargetException|InstantiationException e) {
82+
throw new RuntimeException("cannot load decoder " + decoder_name, e);
5583
}
5684
}
5785

5886
@Override
5987
public void unload() {
6088
}
6189

62-
@Override
63-
public boolean isAllowed(HttpServletRequest request, Project project) {
90+
private User getUser(HttpServletRequest request) {
6491
User user;
65-
if ((user = (User) request.getAttribute(REQUEST_ATTR)) == null) {
92+
93+
if ((user = (User)request.getAttribute(REQUEST_ATTR))==null) {
6694
user = decoder.fromRequest(request);
6795
request.setAttribute(REQUEST_ATTR, user);
6896
}
69-
return user != null;
97+
98+
return user;
99+
}
100+
101+
@Override
102+
public boolean isAllowed(HttpServletRequest request, Project project) {
103+
return getUser(request) != null;
70104
}
71105

72106
@Override
73107
public boolean isAllowed(HttpServletRequest request, Group group) {
74-
User user;
75-
if ((user = (User) request.getAttribute(REQUEST_ATTR)) == null) {
76-
user = decoder.fromRequest(request);
77-
request.setAttribute(REQUEST_ATTR, user);
78-
}
79-
return user != null;
108+
return getUser(request) != null;
80109
}
81110
}

plugins/test/opengrok/auth/plugin/UserPluginTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
package opengrok.auth.plugin;
2424

2525
import javax.servlet.http.HttpServletRequest;
26+
27+
import opengrok.auth.plugin.decoders.OSSOHeaderDecoder;
2628
import opengrok.auth.plugin.entity.User;
2729
import opengrok.auth.plugin.util.DummyHttpServletRequestUser;
2830
import org.junit.Assert;
@@ -37,11 +39,11 @@
3739
*/
3840
public class UserPluginTest {
3941

40-
UserPlugin plugin;
42+
private UserPlugin plugin;
4143

4244
@Before
4345
public void setUp() {
44-
plugin = new UserPlugin();
46+
plugin = new UserPlugin(new OSSOHeaderDecoder());
4547
}
4648

4749
@Test

0 commit comments

Comments
 (0)