Small fixes for login app

Jens Pelzetter 2021-01-06 14:46:50 +01:00
parent f0e152822d
commit 1530cd74c8
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.theming.mvc.ThemesMvc;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Optional;
import javax.enterprise.context.RequestScoped;
@ -36,13 +38,19 @@ import javax.inject.Inject;
import javax.mail.MessagingException;
import javax.mvc.Controller;
import javax.mvc.Models;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
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.Response;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;
/**
@ -63,6 +71,9 @@ public class LoginController {
@Inject
private Models models;
@Inject
private HttpServletRequest request;
@Inject
private Subject subject;
@ -76,25 +87,26 @@ public class LoginController {
@Path("/")
public String getLoginForm(
@Context final UriInfo uriInfo,
@QueryParam("return_url") final String redirectUrl
@QueryParam("returnUrl") @DefaultValue("") final String returnUrl
) {
models.put(
"emailIsPrimaryIdentifier", isEmailPrimaryIdentifier()
);
if (models.get("loginFailed") == null) {
models.put("loginFailed", false);
models.put("returnUrl", redirectUrl);
}
models.put("returnUrl", returnUrl);
return themesMvc.getMvcTemplate(uriInfo, "login-form");
}
@POST
@Path("/")
public String processLogin(
public Object processLogin(
@Context final UriInfo uriInfo,
@FormParam("login") final String login,
@FormParam("password") final String password,
@FormParam("rememberMe") final String rememberMeValue,
@FormParam("redirectUrl") @DefaultValue("") final String redirectUrl
@FormParam("returnUrl") @DefaultValue("") final String returnUrl
) {
final UsernamePasswordToken token = new UsernamePasswordToken(
login, password
@ -104,10 +116,26 @@ public class LoginController {
subject.login(token);
} catch (AuthenticationException ex) {
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
@ -141,4 +169,5 @@ public class LoginController {
);
return kernelConfig.emailIsPrimaryIdentifier();
}
}

View File

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