Skip to content

Commit 9464be8

Browse files
author
Vladimir Kotal
committed
add Mellon header decoder
fixes #2739
1 parent 7fc42cd commit 9464be8

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
24+
package opengrok.auth.plugin.decoders;
25+
26+
import opengrok.auth.plugin.entity.User;
27+
28+
import javax.servlet.http.HttpServletRequest;
29+
import java.util.logging.Level;
30+
import java.util.logging.Logger;
31+
32+
/**
33+
* Decode basic headers coming from the
34+
* <a href="https://github.com/Uninett/mod_auth_mellon">mod_auth_mellon</a> module
35+
* for Apache web server.
36+
*
37+
* This decoder assumes that the SAML Service Provider metadata was setup with
38+
* {@code <NameIDFormat>urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress</NameIDFormat>}
39+
* i.e. that Identity Provider will send back e-mail address of the authenticated user
40+
* and that the {@code mod_auth_mellon} is setup to create Apache environment variable
41+
* containing the e-mail address and the {@code mod_headers} Apache module is set to
42+
* pass the value of this variable in HTTP header called {@code MELLON_email}, i.e.:
43+
* {@code RequestHeader set email "%{MELLON_email}e" env=MELLON_email}
44+
*/
45+
public class MellonHeaderDecoder implements IUserDecoder {
46+
47+
private static final Logger LOGGER = Logger.getLogger(MellonHeaderDecoder.class.getName());
48+
49+
static String MELLON_EMAIL_HEADER = "MELLON_email";
50+
51+
@Override
52+
public User fromRequest(HttpServletRequest request) {
53+
String username = request.getHeader(MELLON_EMAIL_HEADER);
54+
if (username == null || username.isEmpty()) {
55+
LOGGER.log(Level.WARNING,
56+
"Can not construct an user: username could not be extracted");
57+
return null;
58+
}
59+
60+
return new User(username);
61+
}
62+
}

plugins/src/opengrok/auth/plugin/entity/User.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public class User {
3434
private boolean timeouted;
3535
private final Map<String, Object> attrs = new HashMap<>();
3636

37+
public User(String username) {
38+
this.username = username;
39+
}
40+
3741
public User(String username, String id, Date cookieTimestamp, boolean timeouted) {
3842
this.id = id;
3943
this.username = username;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
24+
package opengrok.auth.plugin.decoders;
25+
26+
import opengrok.auth.plugin.entity.User;
27+
import opengrok.auth.plugin.util.DummyHttpServletRequestUser;
28+
import org.junit.Assert;
29+
import org.junit.Before;
30+
import org.junit.Test;
31+
32+
import static opengrok.auth.plugin.decoders.MellonHeaderDecoder.MELLON_EMAIL_HEADER;
33+
34+
public class MellonDecoderTest {
35+
DummyHttpServletRequestUser dummyRequest;
36+
MellonHeaderDecoder decoder = new MellonHeaderDecoder();
37+
38+
@Before
39+
public void setUp() {
40+
dummyRequest = new DummyHttpServletRequestUser();
41+
dummyRequest.setHeader(MELLON_EMAIL_HEADER, "[email protected]");
42+
}
43+
44+
@Test
45+
public void testAll() {
46+
User result = decoder.fromRequest(dummyRequest);
47+
48+
Assert.assertNotNull(result);
49+
Assert.assertEquals("[email protected]", result.getUsername());
50+
Assert.assertNull(result.getId());
51+
Assert.assertFalse(result.isTimeouted());
52+
}
53+
}

0 commit comments

Comments
 (0)