Skip to content

Commit f63cbbe

Browse files
committed
Improve sample project
1 parent afccf0b commit f63cbbe

3 files changed

Lines changed: 55 additions & 39 deletions

File tree

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<%@page import="com.onelogin.saml2.Auth"%>
2+
<%@page import="com.onelogin.saml2.servlet.ServletUtils"%>
23
<%@page import="java.util.Collection"%>
34
<%@page import="java.util.HashMap"%>
45
<%@page import="java.util.List"%>
@@ -25,21 +26,21 @@
2526
<body>
2627
<div class="container">
2728
<h1>A Java SAML Toolkit by OneLogin demo</h1>
28-
29+
2930
<!-- TODO Session support -->
30-
31+
3132
<%
3233
Auth auth = new Auth(request, response);
3334
auth.processResponse();
3435
3536
if (!auth.isAuthenticated()) {
3637
out.println("<div class=\"alert alert-danger\" role=\"alert\">Not authenticated</div>");
3738
}
38-
39+
3940
List<String> errors = auth.getErrors();
40-
out.println("<p>" + StringUtils.join(errors, ", ") + "</p>");
41-
41+
4242
if (!errors.isEmpty()) {
43+
out.println("<p>" + StringUtils.join(errors, ", ") + "</p>");
4344
if (auth.isDebugActive()) {
4445
String errorReason = auth.getLastErrorReason();
4546
if (errorReason != null && !errorReason.isEmpty()) {
@@ -50,45 +51,54 @@
5051
} else {
5152
Map<String, List<String>> attributes = auth.getAttributes();
5253
String nameId = auth.getNameId();
53-
54+
5455
session.setAttribute("attributes", attributes);
5556
session.setAttribute("nameId", nameId);
56-
57-
if (attributes.isEmpty()) {
58-
%>
59-
<div class="alert alert-danger" role="alert">You don't have any attributes</div>
60-
<%
61-
}
62-
else {
63-
%>
64-
<table class="table table-striped">
65-
<thead>
66-
<th>Name</th>
67-
<th>Values</th>
68-
</thead>
69-
<tbody>
70-
<%
71-
Collection<String> keys = attributes.keySet();
72-
for(String name :keys){
73-
out.println("<tr><td>" + name + "</td><td>");
74-
List<String> values = attributes.get(name);
75-
for(String value :values) {
76-
out.println("<li>" + value + "</li>");
57+
58+
String relayState = request.getParameter("RelayState");
59+
60+
if (relayState != null && relayState != ServletUtils.getSelfRoutedURLNoQuery(request) &&
61+
!relayState.contains("/dologin.jsp") ) { // We don't want to be redirected to login.jsp neither
62+
response.sendRedirect(request.getParameter("RelayState"));
63+
} else {
64+
65+
66+
if (attributes.isEmpty()) {
67+
%>
68+
<div class="alert alert-danger" role="alert">You don't have any attributes</div>
69+
<%
70+
}
71+
else {
72+
%>
73+
<table class="table table-striped">
74+
<thead>
75+
<th>Name</th>
76+
<th>Values</th>
77+
</thead>
78+
<tbody>
79+
<%
80+
Collection<String> keys = attributes.keySet();
81+
for(String name :keys){
82+
out.println("<tr><td>" + name + "</td><td>");
83+
List<String> values = attributes.get(name);
84+
for(String value :values) {
85+
out.println("<li>" + value + "</li>");
86+
}
87+
88+
out.println("</td></tr>");
7789
}
78-
79-
out.println("</td></tr>");
90+
%>
91+
</tbody>
92+
</table>
93+
<%
8094
}
81-
%>
82-
</tbody>
83-
</table>
84-
<%
95+
%>
96+
<a href="attrs.jsp" class="btn btn-primary">See user data stored at session</a>
97+
<a href="dologout.jsp" class="btn btn-primary">Logout</a>
98+
<%
8599
}
86-
%>
87-
<a href="attrs.jsp" class="btn btn-primary">See user data stored at session</a>
88-
<a href="dologout.jsp" class="btn btn-primary">Logout</a>
89-
<%
90100
}
91-
%>
101+
%>
92102
</div>
93103
</body>
94104
</html>

samples/java-saml-jspsample/src/main/webapp/dologin.jsp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
<body>
99
<%
1010
Auth auth = new Auth(request, response);
11-
auth.login();
11+
if (request.getParameter("attrs") == null) {
12+
auth.login();
13+
} else {
14+
String x = request.getPathInfo();
15+
auth.login("/java-saml-tookit-jspsample/attrs.jsp");
16+
}
1217
%>
1318
</body>
1419
</html>

samples/java-saml-jspsample/src/main/webapp/index.jsp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<div class="container">
2121
<h1>A Java SAML Toolkit by OneLogin demo</h1>
2222
<a href="dologin.jsp" class="btn btn-primary">Login</a>
23+
<a href="dologin.jsp?attrs=1" class="btn btn-primary">Login and access to attrs.jsp page </a>
2324
</div>
2425
</body>
2526
</html>

0 commit comments

Comments
 (0)