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;
/** /**
@ -53,48 +61,52 @@ import javax.ws.rs.core.UriInfo;
@Path("/") @Path("/")
@RequestScoped @RequestScoped
public class LoginController { public class LoginController {
@Inject @Inject
private ChallengeManager challengeManager; private ChallengeManager challengeManager;
@Inject @Inject
private ConfigurationManager confManager; private ConfigurationManager confManager;
@Inject @Inject
private Models models; private Models models;
@Inject
private HttpServletRequest request;
@Inject @Inject
private Subject subject; private Subject subject;
@Inject @Inject
private ThemesMvc themesMvc; private ThemesMvc themesMvc;
@Inject @Inject
private UserRepository userRepository; private UserRepository userRepository;
@GET @GET
@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()
); );
models.put("loginFailed", false); if (models.get("loginFailed") == null) {
models.put("returnUrl", redirectUrl); models.put("loginFailed", false);
}
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
@ -102,20 +114,36 @@ public class LoginController {
token.setRememberMe("on".equals(rememberMeValue)); token.setRememberMe("on".equals(rememberMeValue));
try { try {
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);
}
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
);
} }
return String.format("redirect:%s", redirectUrl);
} }
@GET @GET
@Path("/recover-password") @Path("/recover-password")
public String getRecoverPasswordForm(@Context final UriInfo uriInfo) { public String getRecoverPasswordForm(@Context final UriInfo uriInfo) {
return themesMvc.getMvcTemplate(uriInfo, "login-recover-password"); return themesMvc.getMvcTemplate(uriInfo, "login-recover-password");
} }
@POST @POST
@Path("/recover-password") @Path("/recover-password")
public String recoverPassword( public String recoverPassword(
@ -125,20 +153,21 @@ public class LoginController {
final Optional<User> user = userRepository.findByEmailAddress(email); final Optional<User> user = userRepository.findByEmailAddress(email);
if (user.isPresent()) { if (user.isPresent()) {
try { try {
challengeManager.sendPasswordRecover(user.get()); challengeManager.sendPasswordRecover(user.get());
} catch(MessagingException ex) { } catch (MessagingException ex) {
models.put("failedToSendRecoverMessage", true); models.put("failedToSendRecoverMessage", true);
return getRecoverPasswordForm(uriInfo); return getRecoverPasswordForm(uriInfo);
} }
} }
return themesMvc.getMvcTemplate(uriInfo, "login-password-recovered"); return themesMvc.getMvcTemplate(uriInfo, "login-password-recovered");
} }
private boolean isEmailPrimaryIdentifier() { private boolean isEmailPrimaryIdentifier() {
final KernelConfig kernelConfig = confManager.findConfiguration( final KernelConfig kernelConfig = confManager.findConfiguration(
KernelConfig.class KernelConfig.class
); );
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>