Skip to content
This repository was archived by the owner on Nov 15, 2022. It is now read-only.

Commit ba24399

Browse files
vinayvishalyaminikb
authored andcommitted
NoBug : New devtests to validate behaviour around caller principal as defined in JSR 375 (#22192)
* Add Caller Principal devtests * Validate Caller Principal Glassfish maintains a single caller principal in Subject representing the user. In case of Glassfish, hence, both application caller principal and container caller principal are one and the same. These tests have been modified to assert this behavior. * Update copyright information
1 parent 40dfb64 commit ba24399

31 files changed

Lines changed: 1866 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
### app-bundled-ham-basic-login-config-form
2+
3+
**If an application bundles its own `HttpAuthenticationMechanism`, then for authentication, the container will rely on the bundled mechanism and will ignore the `login-config` element in deployment descriptor of the application.
4+
5+
In this sample app, the `BASIC` authentication mechanism defined in `HttpAuthenticationMechanism` takes precedencce over `FORM` authentication present in `login-config`.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<!--
2+
3+
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4+
5+
Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
6+
7+
The contents of this file are subject to the terms of either the GNU
8+
General Public License Version 2 only ("GPL") or the Common Development
9+
and Distribution License("CDDL") (collectively, the "License"). You
10+
may not use this file except in compliance with the License. You can
11+
obtain a copy of the License at
12+
https://oss.oracle.com/licenses/CDDL+GPL-1.1
13+
or LICENSE.txt. See the License for the specific
14+
language governing permissions and limitations under the License.
15+
16+
When distributing the software, include this License Header Notice in each
17+
file and include the License file at LICENSE.txt.
18+
19+
GPL Classpath Exception:
20+
Oracle designates this particular file as subject to the "Classpath"
21+
exception as provided by Oracle in the GPL Version 2 section of the License
22+
file that accompanied this code.
23+
24+
Modifications:
25+
If applicable, add the following below the License Header, with the fields
26+
enclosed by brackets [] replaced by your own identifying information:
27+
"Portions Copyright [year] [name of copyright owner]"
28+
29+
Contributor(s):
30+
If you wish your version of this file to be governed by only the CDDL or
31+
only the GPL Version 2, indicate your decision by adding "[Contributor]
32+
elects to include this software in this distribution under the [CDDL or GPL
33+
Version 2] license." If you don't indicate a single choice of license, a
34+
recipient has the option to distribute your version of this file under
35+
either the CDDL, the GPL Version 2 or to extend the choice of license to
36+
its licensees as provided above. However, if you add GPL Version 2 code
37+
and therefore, elected the GPL Version 2 license, then the option applies
38+
only if the new code is made subject to such option by the copyright
39+
holder.
40+
41+
-->
42+
43+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
45+
<modelVersion>4.0.0</modelVersion>
46+
<parent>
47+
<groupId>org.glassfish.soteria.test</groupId>
48+
<artifactId>soteria</artifactId>
49+
<version>5.0-SNAPSHOT</version>
50+
</parent>
51+
52+
<artifactId>app-bundled-ham-basic-login-config-form</artifactId>
53+
<packaging>war</packaging>
54+
55+
<build>
56+
<finalName>app-bundled-ham-basic-login-config-form</finalName>
57+
</build>
58+
59+
<properties>
60+
<failOnMissingWebXml>false</failOnMissingWebXml>
61+
</properties>
62+
<dependencies>
63+
<dependency>
64+
<groupId>org.glassfish.soteria.test</groupId>
65+
<artifactId>common</artifactId>
66+
<version>5.0-SNAPSHOT</version>
67+
<scope>test</scope>
68+
</dependency>
69+
</dependencies>
70+
71+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://oss.oracle.com/licenses/CDDL+GPL-1.1
12+
* or LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
41+
package org.glassfish.soteria.test;
42+
43+
import java.io.IOException;
44+
45+
import javax.inject.Inject;
46+
import javax.security.enterprise.SecurityContext;
47+
import javax.servlet.ServletException;
48+
import javax.servlet.annotation.HttpConstraint;
49+
import javax.servlet.annotation.ServletSecurity;
50+
import javax.servlet.annotation.WebServlet;
51+
import javax.servlet.http.HttpServlet;
52+
import javax.servlet.http.HttpServletRequest;
53+
import javax.servlet.http.HttpServletResponse;
54+
55+
@WebServlet(urlPatterns = "/basicAuthenticationServlet")
56+
@ServletSecurity(@HttpConstraint(rolesAllowed = "foo"))
57+
58+
public class BasicAuthenticationServlet extends HttpServlet {
59+
60+
@Override
61+
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
62+
response.getWriter().write("Authentication Mechanism:" + response.getHeader("Authentication Mechanism") +"\n");
63+
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://oss.oracle.com/licenses/CDDL+GPL-1.1
12+
* or LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
41+
package org.glassfish.soteria.test;
42+
43+
import javax.enterprise.context.RequestScoped;
44+
import javax.inject.Inject;
45+
import javax.security.enterprise.AuthenticationException;
46+
import javax.security.enterprise.AuthenticationStatus;
47+
import javax.security.enterprise.authentication.mechanism.http.HttpAuthenticationMechanism;
48+
import javax.security.enterprise.authentication.mechanism.http.HttpMessageContext;
49+
import javax.security.enterprise.credential.UsernamePasswordCredential;
50+
import javax.security.enterprise.identitystore.CredentialValidationResult;
51+
import javax.security.enterprise.identitystore.IdentityStoreHandler;
52+
import javax.servlet.http.HttpServletRequest;
53+
import javax.servlet.http.HttpServletResponse;
54+
55+
import static javax.security.enterprise.identitystore.CredentialValidationResult.Status.VALID;
56+
57+
@RequestScoped
58+
public class TestAuthenticationMechanism implements HttpAuthenticationMechanism {
59+
60+
@Inject
61+
private IdentityStoreHandler identityStoreHandler;
62+
63+
@Override
64+
public AuthenticationStatus validateRequest(HttpServletRequest request, HttpServletResponse response, HttpMessageContext httpMessageContext) throws AuthenticationException {
65+
66+
// Get the (caller) name and password from the request
67+
// NOTE: This is for the smallest possible example only. In practice
68+
// putting the password in a request query parameter is highly
69+
// insecure
70+
String name = request.getParameter("name");
71+
String password = request.getParameter("password");
72+
73+
if (name != null && password != null) {
74+
75+
// Delegate the {credentials in -> identity data out} function to
76+
// the Identity Store
77+
CredentialValidationResult result = identityStoreHandler.validate(
78+
new UsernamePasswordCredential(name, password));
79+
80+
if (result.getStatus() == VALID) {
81+
// Communicate the details of the authenticated user to the
82+
// container. In many cases the underlying handler will just store the details
83+
// and the container will actually handle the login after we return from
84+
// this method.
85+
response.addHeader("Authentication Mechanism", "TestAuthenticationMechanism");
86+
return httpMessageContext.notifyContainerAboutLogin(
87+
result.getCallerPrincipal(), result.getCallerGroups());
88+
} else {
89+
return httpMessageContext.responseUnauthorized();
90+
}
91+
}
92+
93+
return httpMessageContext.doNothing();
94+
}
95+
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://oss.oracle.com/licenses/CDDL+GPL-1.1
12+
* or LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
41+
package org.glassfish.soteria.test;
42+
43+
import static java.util.Arrays.asList;
44+
import static javax.security.enterprise.identitystore.CredentialValidationResult.INVALID_RESULT;
45+
46+
import java.util.HashSet;
47+
48+
import javax.enterprise.context.RequestScoped;
49+
import javax.security.enterprise.credential.UsernamePasswordCredential;
50+
import javax.security.enterprise.identitystore.CredentialValidationResult;
51+
import javax.security.enterprise.identitystore.IdentityStore;
52+
53+
@RequestScoped
54+
public class TestIdentityStore implements IdentityStore {
55+
56+
public CredentialValidationResult validate(UsernamePasswordCredential usernamePasswordCredential) {
57+
58+
if (usernamePasswordCredential.compareTo("reza", "secret1")) {
59+
return new CredentialValidationResult("reza", new HashSet<>(asList("foo", "bar")));
60+
}
61+
62+
return INVALID_RESULT;
63+
}
64+
65+
}

appserver/tests/appserv-tests/devtests/security/soteria/app-bundled-ham-basic-login-config-form/src/main/webapp/WEB-INF/beans.xml

Whitespace-only changes.

0 commit comments

Comments
 (0)