Skip to content

Conversation

SanderKondratjevNortal
Copy link

Signed-off-by: Sander Kondratjev [email protected]

Copy link

Copy link
Member

@mrts mrts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me now apart from the suggested changes, thanks for your great work!

As discussed, also consider making mobile code configurable both to make it easier to discern and possibly serve a real business need not to use it in certain circumstances.

import java.io.IOException;

public final class WebEidLoginPageGeneratingFilter extends OncePerRequestFilter {
private static final String LOGIN_PAGE_HTML = """
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, let's use Thymeleaf to avoid the somewhat fragile String.format() and keep HTML out of Java files. The HTML should be moved to resources/templates/webeid-login.html.

Comment on lines +100 to +102
public WebEidLoginPageGeneratingFilter(String path, String loginProcessingPath) {
this.requestMatcher = PathPatternRequestMatcher.withDefaults().matcher(HttpMethod.GET, path);
this.loginProcessingPath = loginProcessingPath;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Thymeleaf support:

Suggested change
public WebEidLoginPageGeneratingFilter(String path, String loginProcessingPath) {
this.requestMatcher = PathPatternRequestMatcher.withDefaults().matcher(HttpMethod.GET, path);
this.loginProcessingPath = loginProcessingPath;
public WebEidLoginPageGeneratingFilter(String path, String loginProcessingPath, ITemplateEngine templateEngine, JakartaServletWebApplication webApp) {
this.requestMatcher = PathPatternRequestMatcher.withDefaults().matcher(HttpMethod.GET, path);
this.loginProcessingPath = loginProcessingPath;
this.templateEngine = templateEngine;
this.webApp = webApp;

And add fields above:

    private final ITemplateEngine templateEngine;
    private final JakartaServletWebApplication webApp;

Comment on lines +124 to +129
private String generateHtml(CsrfToken csrf) {
return String.format(
LOGIN_PAGE_HTML,
loginProcessingPath,
csrf != null ? csrf.getHeaderName() : "X-CSRF-TOKEN",
csrf != null ? csrf.getToken() : ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private String generateHtml(CsrfToken csrf) {
return String.format(
LOGIN_PAGE_HTML,
loginProcessingPath,
csrf != null ? csrf.getHeaderName() : "X-CSRF-TOKEN",
csrf != null ? csrf.getToken() : ""
private String renderTemplate(HttpServletRequest request, HttpServletResponse response, CsrfToken csrf) {
IWebExchange exchange = webApp.buildExchange(request, response);
var locale = RequestContextUtils.getLocale(request);
var ctx = new WebContext(exchange, locale);
ctx.setVariable("loginProcessingPath", loginProcessingPath);
ctx.setVariable("csrfHeaderName", csrf != null ? csrf.getHeaderName() : "X-CSRF-TOKEN");
ctx.setVariable("csrfToken", csrf != null ? csrf.getToken() : "");
return templateEngine.process("webeid-login", ctx);

and add import org.springframework.web.servlet.support.RequestContextUtils.

Comment on lines +118 to +120
String html = generateHtml(csrf);

response.setContentType(MediaType.TEXT_HTML_VALUE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
String html = generateHtml(csrf);
response.setContentType(MediaType.TEXT_HTML_VALUE);
String html = renderTemplate(request, response, csrf);
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
response.setContentType(MediaType.TEXT_HTML_VALUE);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants