Skip to content

Commit 749ca70

Browse files
authored
Merge pull request #3 from MeroRai/PayaraRolesPermittedTest
Payara roles permitted test
2 parents 2121a62 + 96a26c5 commit 749ca70

File tree

10 files changed

+554
-0
lines changed

10 files changed

+554
-0
lines changed

payara/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
<modules>
1616
<module>jacc-per-app</module>
17+
<module>rolesPermitted</module>
1718
<module>oauth2</module>
1819
</modules>
1920

payara/rolesPermitted/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>org.vendoree</groupId>
7+
<artifactId>payara</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
11+
<artifactId>rolesPermitted</artifactId>
12+
<packaging>war</packaging>
13+
<name>Vendor EE: Payara - Private: Roles Permitted</name>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>fish.payara.api</groupId>
18+
<artifactId>payara-api</artifactId>
19+
<version>${payara.version}</version>
20+
<scope>provided</scope>
21+
</dependency>
22+
</dependencies>
23+
</project>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2018 Payara Foundation 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://github.com/payara/Payara/blob/master/LICENSE.txt
12+
* 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 glassfish/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* The Payara Foundation designates this particular file as subject to the "Classpath"
20+
* exception as provided by the Payara Foundation 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+
package org.vendoree.payara.rolespermitted;
41+
42+
import static java.util.Arrays.asList;
43+
import static javax.security.enterprise.identitystore.CredentialValidationResult.INVALID_RESULT;
44+
45+
import java.util.HashSet;
46+
47+
import javax.enterprise.context.ApplicationScoped;
48+
import javax.security.enterprise.credential.UsernamePasswordCredential;
49+
import javax.security.enterprise.identitystore.CredentialValidationResult;
50+
import javax.security.enterprise.identitystore.IdentityStore;
51+
52+
/**
53+
*
54+
* @author Susan Rai
55+
*/
56+
@ApplicationScoped
57+
public class IdentityStoreTest implements IdentityStore {
58+
59+
public CredentialValidationResult validate(UsernamePasswordCredential userLoginCredential) {
60+
61+
if (userLoginCredential.getCaller().equals("payara")
62+
&& userLoginCredential.getPasswordAsString().equals("fish")) {
63+
return new CredentialValidationResult("payara", new HashSet<>(asList("payaraAdmin", "payaraUser")));
64+
}
65+
66+
if (userLoginCredential.getCaller().equals("payara")
67+
&& userLoginCredential.getPasswordAsString().equals("user")) {
68+
return new CredentialValidationResult("payara", new HashSet<>(asList("payaraUser")));
69+
}
70+
71+
return INVALID_RESULT;
72+
}
73+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2018 Payara Foundation 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://github.com/payara/Payara/blob/master/LICENSE.txt
12+
* 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 glassfish/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* The Payara Foundation designates this particular file as subject to the "Classpath"
20+
* exception as provided by the Payara Foundation 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+
package org.vendoree.payara.rolespermitted;
41+
42+
import javax.enterprise.context.ApplicationScoped;
43+
import javax.inject.Inject;
44+
import javax.security.enterprise.AuthenticationException;
45+
import javax.security.enterprise.AuthenticationStatus;
46+
import javax.security.enterprise.authentication.mechanism.http.HttpAuthenticationMechanism;
47+
import javax.security.enterprise.authentication.mechanism.http.HttpMessageContext;
48+
import javax.security.enterprise.credential.UsernamePasswordCredential;
49+
import javax.security.enterprise.identitystore.CredentialValidationResult;
50+
import javax.servlet.http.HttpServletRequest;
51+
import javax.servlet.http.HttpServletResponse;
52+
53+
import static javax.security.enterprise.identitystore.CredentialValidationResult.Status.VALID;
54+
55+
/**
56+
*
57+
* @author Susan Rai
58+
*/
59+
@ApplicationScoped
60+
public class TestAuthenticationMechanism implements HttpAuthenticationMechanism {
61+
62+
@Inject
63+
IdentityStoreTest identityStoreTest;
64+
65+
@Override
66+
public AuthenticationStatus validateRequest(HttpServletRequest request, HttpServletResponse response, HttpMessageContext httpMessageContext) throws AuthenticationException {
67+
68+
String name = request.getParameter("username");
69+
String password = request.getParameter("password");
70+
71+
if (name == null && password == null) {
72+
return httpMessageContext.doNothing();
73+
} else {
74+
CredentialValidationResult loginResult = identityStoreTest.validate(new UsernamePasswordCredential(name, password));
75+
76+
if (loginResult.getStatus() == VALID) {
77+
return httpMessageContext.notifyContainerAboutLogin(
78+
loginResult.getCallerPrincipal(),
79+
loginResult.getCallerGroups());
80+
} else {
81+
return httpMessageContext.responseUnauthorized();
82+
}
83+
}
84+
85+
}
86+
87+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2018 Payara Foundation 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://github.com/payara/Payara/blob/master/LICENSE.txt
12+
* 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 glassfish/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* The Payara Foundation designates this particular file as subject to the "Classpath"
20+
* exception as provided by the Payara Foundation 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+
package org.vendoree.payara.rolespermitted;
41+
42+
import fish.payara.cdi.auth.roles.RolesPermitted;
43+
import java.security.Principal;
44+
import javax.enterprise.context.RequestScoped;
45+
import javax.inject.Inject;
46+
47+
/**
48+
*
49+
* @author Susan Rai
50+
*/
51+
@RequestScoped
52+
public class TestRolesPermitted {
53+
54+
@Inject
55+
Principal principal;
56+
57+
@RolesPermitted({"payaraAdmin"})
58+
public String getUserName() {
59+
return principal.getName();
60+
}
61+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2018 Payara Foundation 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://github.com/payara/Payara/blob/master/LICENSE.txt
12+
* 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 glassfish/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* The Payara Foundation designates this particular file as subject to the "Classpath"
20+
* exception as provided by the Payara Foundation 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+
package org.vendoree.payara.rolespermitted;
41+
42+
import java.io.IOException;
43+
import java.io.PrintWriter;
44+
import javax.inject.Inject;
45+
import javax.servlet.ServletException;
46+
import javax.servlet.annotation.WebServlet;
47+
import javax.servlet.http.HttpServlet;
48+
import javax.servlet.http.HttpServletRequest;
49+
import javax.servlet.http.HttpServletResponse;
50+
51+
/**
52+
*
53+
* @author Susan Rai
54+
*/
55+
@WebServlet("/testServlet")
56+
public class TestServlet extends HttpServlet {
57+
58+
private static final long serialVersionUID = 1L;
59+
60+
@Inject
61+
TestRolesPermitted testRolesPermitted;
62+
63+
/**
64+
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
65+
* methods.
66+
*
67+
* @param request servlet request
68+
* @param response servlet response
69+
* @throws ServletException if a servlet-specific error occurs
70+
* @throws IOException if an I/O error occurs
71+
*/
72+
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
73+
throws ServletException, IOException {
74+
response.setContentType("text/html;charset=UTF-8");
75+
76+
String userName = "nothing";
77+
try {
78+
userName = testRolesPermitted.getUserName();
79+
} catch (Exception ex) {
80+
System.out.println(ex.toString());
81+
}
82+
83+
try (PrintWriter out = response.getWriter()) {
84+
/* TODO output your page here. You may use following sample code. */
85+
out.println("<!DOCTYPE html>");
86+
out.println("<html>");
87+
out.println("<head>");
88+
out.println("<title>TestServlet</title>");
89+
out.println("</head>");
90+
out.println("<body>");
91+
out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>");
92+
out.println("<p>Username = " + userName + "</p>");
93+
out.println("<p> Does User belong to role \"payaraAdmin\" : " + request.isUserInRole("payaraAdmin"));
94+
out.println("</body>");
95+
out.println("</html>");
96+
}
97+
}
98+
99+
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
100+
/**
101+
* Handles the HTTP <code>GET</code> method.
102+
*
103+
* @param request servlet request
104+
* @param response servlet response
105+
* @throws ServletException if a servlet-specific error occurs
106+
* @throws IOException if an I/O error occurs
107+
*/
108+
@Override
109+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
110+
throws ServletException, IOException {
111+
processRequest(request, response);
112+
}
113+
114+
/**
115+
* Handles the HTTP <code>POST</code> method.
116+
*
117+
* @param request servlet request
118+
* @param response servlet response
119+
* @throws ServletException if a servlet-specific error occurs
120+
* @throws IOException if an I/O error occurs
121+
*/
122+
@Override
123+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
124+
throws ServletException, IOException {
125+
processRequest(request, response);
126+
}
127+
128+
/**
129+
* Returns a short description of the servlet.
130+
*
131+
* @return a String containing servlet description
132+
*/
133+
@Override
134+
public String getServletInfo() {
135+
return "Short description";
136+
}// </editor-fold>
137+
138+
}

0 commit comments

Comments
 (0)