-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #6205 - Make OpenID alwaysSaveUri configuration available via i…
…nit param. Signed-off-by: Lachlan Roberts <[email protected]>
- Loading branch information
1 parent
9176d83
commit 6d4b827
Showing
3 changed files
with
169 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,8 +35,10 @@ | |
import org.junit.jupiter.api.Test; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
@SuppressWarnings("unchecked") | ||
public class OpenIdAuthenticationTest | ||
{ | ||
public static final String CLIENT_ID = "testClient101"; | ||
|
@@ -55,6 +57,7 @@ public void setup() throws Exception | |
|
||
server = new Server(); | ||
connector = new ServerConnector(server); | ||
connector.setPort(8080); | ||
server.addConnector(connector); | ||
ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS); | ||
|
||
|
@@ -122,30 +125,29 @@ public void stop() throws Exception | |
@Test | ||
public void testLoginLogout() throws Exception | ||
{ | ||
openIdProvider.setUser(new OpenIdProvider.User("123456789", "Alice")); | ||
|
||
String appUriString = "http://localhost:" + connector.getLocalPort(); | ||
|
||
// Initially not authenticated | ||
ContentResponse response = client.GET(appUriString + "/"); | ||
assertThat(response.getStatus(), is(HttpStatus.OK_200)); | ||
String[] content = response.getContentAsString().split("[\r\n]+"); | ||
assertThat(content.length, is(1)); | ||
assertThat(content[0], is("not authenticated")); | ||
String content = response.getContentAsString(); | ||
assertThat(content, containsString("not authenticated")); | ||
|
||
// Request to login is success | ||
response = client.GET(appUriString + "/login"); | ||
assertThat(response.getStatus(), is(HttpStatus.OK_200)); | ||
content = response.getContentAsString().split("[\r\n]+"); | ||
assertThat(content.length, is(1)); | ||
assertThat(content[0], is("success")); | ||
content = response.getContentAsString(); | ||
assertThat(content, containsString("success")); | ||
|
||
// Now authenticated we can get info | ||
response = client.GET(appUriString + "/"); | ||
assertThat(response.getStatus(), is(HttpStatus.OK_200)); | ||
content = response.getContentAsString().split("[\r\n]+"); | ||
assertThat(content.length, is(3)); | ||
assertThat(content[0], is("userId: 123456789")); | ||
assertThat(content[1], is("name: Alice")); | ||
assertThat(content[2], is("email: [email protected]")); | ||
content = response.getContentAsString(); | ||
assertThat(content, containsString("userId: 123456789")); | ||
assertThat(content, containsString("name: Alice")); | ||
assertThat(content, containsString("email: [email protected]")); | ||
|
||
// Request to admin page gives 403 as we do not have admin role | ||
response = client.GET(appUriString + "/admin"); | ||
|
@@ -154,17 +156,18 @@ public void testLoginLogout() throws Exception | |
// We are no longer authenticated after logging out | ||
response = client.GET(appUriString + "/logout"); | ||
assertThat(response.getStatus(), is(HttpStatus.OK_200)); | ||
content = response.getContentAsString().split("[\r\n]+"); | ||
assertThat(content.length, is(1)); | ||
assertThat(content[0], is("not authenticated")); | ||
content = response.getContentAsString(); | ||
assertThat(content, containsString("not authenticated")); | ||
} | ||
|
||
public static class LoginPage extends HttpServlet | ||
{ | ||
@Override | ||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException | ||
{ | ||
response.setContentType("text/html"); | ||
response.getWriter().println("success"); | ||
response.getWriter().println("<br><a href=\"/\">Home</a>"); | ||
} | ||
} | ||
|
||
|
@@ -183,7 +186,7 @@ public static class AdminPage extends HttpServlet | |
@Override | ||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException | ||
{ | ||
Map<String, Object> userInfo = (Map)request.getSession().getAttribute(OpenIdAuthenticator.CLAIMS); | ||
Map<String, Object> userInfo = (Map<String, Object>)request.getSession().getAttribute(OpenIdAuthenticator.CLAIMS); | ||
response.getWriter().println(userInfo.get("sub") + ": success"); | ||
} | ||
} | ||
|
@@ -193,18 +196,20 @@ public static class HomePage extends HttpServlet | |
@Override | ||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException | ||
{ | ||
response.setContentType("text/plain"); | ||
response.setContentType("text/html"); | ||
Principal userPrincipal = request.getUserPrincipal(); | ||
if (userPrincipal != null) | ||
{ | ||
Map<String, Object> userInfo = (Map)request.getSession().getAttribute(OpenIdAuthenticator.CLAIMS); | ||
response.getWriter().println("userId: " + userInfo.get("sub")); | ||
response.getWriter().println("name: " + userInfo.get("name")); | ||
response.getWriter().println("email: " + userInfo.get("email")); | ||
Map<String, Object> userInfo = (Map<String, Object>)request.getSession().getAttribute(OpenIdAuthenticator.CLAIMS); | ||
response.getWriter().println("userId: " + userInfo.get("sub") + "<br>"); | ||
response.getWriter().println("name: " + userInfo.get("name") + "<br>"); | ||
response.getWriter().println("email: " + userInfo.get("email") + "<br>"); | ||
response.getWriter().println("<br><a href=\"/logout\">Logout</a>"); | ||
} | ||
else | ||
{ | ||
response.getWriter().println("not authenticated"); | ||
response.getWriter().println("<br><a href=\"/login\">Login</a>"); | ||
} | ||
} | ||
} | ||
|
@@ -214,8 +219,9 @@ public static class ErrorPage extends HttpServlet | |
@Override | ||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException | ||
{ | ||
response.setContentType("text/plain"); | ||
response.setContentType("text/html"); | ||
response.getWriter().println("not authorized"); | ||
response.getWriter().println("<br><a href=\"/\">Home</a>"); | ||
} | ||
} | ||
} |
Oops, something went wrong.