Small fixes for login app

Former-commit-id: 1530cd74c8
pull/8/head
Jens Pelzetter 2021-01-06 14:46:50 +01:00
parent f02721df90
commit 707562de6e
2 changed files with 60 additions and 31 deletions

View File

@ -29,6 +29,8 @@ import org.libreccm.security.User;
import org.libreccm.security.UserRepository; import org.libreccm.security.UserRepository;
import org.libreccm.theming.mvc.ThemesMvc; import org.libreccm.theming.mvc.ThemesMvc;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Optional; import java.util.Optional;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
@ -36,13 +38,19 @@ import javax.inject.Inject;
import javax.mail.MessagingException; import javax.mail.MessagingException;
import javax.mvc.Controller; import javax.mvc.Controller;
import javax.mvc.Models; import javax.mvc.Models;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DefaultValue; import javax.ws.rs.DefaultValue;
import javax.ws.rs.FormParam; import javax.ws.rs.FormParam;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.RedirectionException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo; import javax.ws.rs.core.UriInfo;
/** /**
@ -63,6 +71,9 @@ public class LoginController {
@Inject @Inject
private Models models; private Models models;
@Inject
private HttpServletRequest request;
@Inject @Inject
private Subject subject; private Subject subject;
@ -76,25 +87,26 @@ public class LoginController {
@Path("/") @Path("/")
public String getLoginForm( public String getLoginForm(
@Context final UriInfo uriInfo, @Context final UriInfo uriInfo,
@QueryParam("return_url") final String redirectUrl @QueryParam("returnUrl") @DefaultValue("") final String returnUrl
) { ) {
models.put( models.put(
"emailIsPrimaryIdentifier", isEmailPrimaryIdentifier() "emailIsPrimaryIdentifier", isEmailPrimaryIdentifier()
); );
if (models.get("loginFailed") == null) {
models.put("loginFailed", false); models.put("loginFailed", false);
models.put("returnUrl", redirectUrl); }
models.put("returnUrl", returnUrl);
return themesMvc.getMvcTemplate(uriInfo, "login-form"); return themesMvc.getMvcTemplate(uriInfo, "login-form");
} }
@POST @POST
@Path("/") @Path("/")
public String processLogin( public Object processLogin(
@Context final UriInfo uriInfo, @Context final UriInfo uriInfo,
@FormParam("login") final String login, @FormParam("login") final String login,
@FormParam("password") final String password, @FormParam("password") final String password,
@FormParam("rememberMe") final String rememberMeValue, @FormParam("rememberMe") final String rememberMeValue,
@FormParam("redirectUrl") @DefaultValue("") final String redirectUrl @FormParam("returnUrl") @DefaultValue("") final String returnUrl
) { ) {
final UsernamePasswordToken token = new UsernamePasswordToken( final UsernamePasswordToken token = new UsernamePasswordToken(
login, password login, password
@ -104,10 +116,26 @@ public class LoginController {
subject.login(token); subject.login(token);
} catch (AuthenticationException ex) { } catch (AuthenticationException ex) {
models.put("loginFailed", true); models.put("loginFailed", true);
return getLoginForm(uriInfo, redirectUrl); return getLoginForm(uriInfo, returnUrl);
} }
return String.format("redirect:%s", redirectUrl); try {
return Response.seeOther(
new URI(
request.getScheme(),
"",
request.getServerName(),
request.getServerPort(),
String.join(request.getContextPath(), returnUrl),
"",
""
)
).build();
} catch (URISyntaxException ex) {
throw new WebApplicationException(
Response.Status.INTERNAL_SERVER_ERROR
);
}
} }
@GET @GET
@ -141,4 +169,5 @@ public class LoginController {
); );
return kernelConfig.emailIsPrimaryIdentifier(); return kernelConfig.emailIsPrimaryIdentifier();
} }
} }

View File

@ -5,9 +5,6 @@
<link rel="stylesheet" href="${themeUrl}/style.css" /> <link rel="stylesheet" href="${themeUrl}/style.css" />
</head> </head>
<body> <body>
<pre>
${themeUrl}/style.css
</pre>
<main> <main>
<h1>${LoginMessages['login.title']}</h1> <h1>${LoginMessages['login.title']}</h1>
<#if (loginFailed)> <#if (loginFailed)>
@ -15,7 +12,6 @@
${LoginMessages['login.errors.failed']} ${LoginMessages['login.errors.failed']}
</div> </div>
</#if> </#if>
<pre>${mvc.uri('LoginController#processLogin')}</pre>
<form action="${mvc.uri('LoginController#processLogin')}" <form action="${mvc.uri('LoginController#processLogin')}"
method="post"> method="post">
<label for="login">${LoginMessages['login.screenname.label']}</label> <label for="login">${LoginMessages['login.screenname.label']}</label>
@ -29,6 +25,10 @@
required="true" required="true"
type="password" /> type="password" />
<input type="hidden"
name="returnUrl"
value="${returnUrl}" />
<button type="submit"> <button type="submit">
${LoginMessages['login.submit']} ${LoginMessages['login.submit']}
</button> </button>