Skip to content

Commit 8fd809e

Browse files
NFC-47 Show errors for mobile. Fix index.html to correct auth uri. Fix CSRF header and token order in login page HTML template. Use records AuthPayload and AuthUri instead of Map.of.
1 parent 94a0378 commit 8fd809e

File tree

4 files changed

+37
-19
lines changed

4 files changed

+37
-19
lines changed

example/src/main/java/eu/webeid/example/security/WebEidMobileAuthInitFilter.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
package eu.webeid.example.security;
2424

25+
import com.fasterxml.jackson.annotation.JsonProperty;
2526
import com.fasterxml.jackson.databind.ObjectMapper;
2627
import eu.webeid.security.challenge.ChallengeNonceGenerator;
2728
import jakarta.servlet.FilterChain;
@@ -39,7 +40,6 @@
3940
import java.io.IOException;
4041
import java.nio.charset.StandardCharsets;
4142
import java.util.Base64;
42-
import java.util.Map;
4343

4444
public final class WebEidMobileAuthInitFilter extends OncePerRequestFilter {
4545
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
@@ -67,14 +67,17 @@ protected void doFilterInternal(@NonNull HttpServletRequest request,
6767
String loginUri = ServletUriComponentsBuilder.fromCurrentContextPath()
6868
.path(loginPath).build().toUriString();
6969

70-
String payloadJson = OBJECT_MAPPER.writeValueAsString(Map.of(
71-
"challenge", challenge.getBase64EncodedNonce(),
72-
"login_uri", loginUri
73-
));
70+
String payloadJson = OBJECT_MAPPER.writeValueAsString(
71+
new AuthPayload(challenge.getBase64EncodedNonce(), loginUri)
72+
);
7473
String encoded = Base64.getEncoder().encodeToString(payloadJson.getBytes(StandardCharsets.UTF_8));
7574
String eidAuthUri = "web-eid-mobile://auth#" + encoded;
7675

7776
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
78-
OBJECT_MAPPER.writeValue(response.getWriter(), Map.of("auth_uri", eidAuthUri));
77+
OBJECT_MAPPER.writeValue(response.getWriter(), new AuthUri(eidAuthUri));
7978
}
79+
80+
record AuthPayload(String challenge, @JsonProperty("login_uri") String loginUri) ;
81+
82+
record AuthUri(@JsonProperty("auth_uri") String authUri) ;
8083
}

example/src/main/java/eu/webeid/example/security/ui/WebEidLoginPageGeneratingFilter.java

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,29 @@ public WebEidLoginPageGeneratingFilter(String path) {
5151
<title>Signing you in…</title>
5252
</head>
5353
<body>
54-
<script>
54+
<div id="error-message" class="alert alert-danger" style="display: none;" role="alert">
55+
<div class="message"></div>
56+
<pre class="details"></pre>
57+
</div>
58+
59+
<script type="module">
60+
import { showErrorMessage } from "/js/errors.js";
5561
(function () {
5662
const frag = location.hash ? location.hash.substring(1) : "";
57-
if (!frag) { location.replace("/?mobileAuthError=missing_payload"); return; }
63+
if (!frag) { showErrorMessage({ code: "MISSING_PAYLOAD", message: "Missing payload" }); return; }
5864
5965
let payload;
6066
try { payload = JSON.parse(atob(frag)); } catch (e) {
6167
console.error("Failed to parse payload", e);
62-
location.replace("/?mobileAuthError=bad_payload");
68+
showErrorMessage({ code: "BAD_PAYLOAD", message: "Failed to parse mobile payload" });
69+
return;
70+
}
71+
72+
if (payload["error"]) {
73+
showErrorMessage({
74+
code: "MOPP_ERROR",
75+
message: payload["message"] ?? "Authentication failed in mobile app"
76+
});
6377
return;
6478
}
6579
@@ -80,7 +94,7 @@ public WebEidLoginPageGeneratingFilter(String path) {
8094
})
8195
.catch(e => {
8296
console.error("Login failed", e);
83-
window.location.replace("/?mobileAuthError=login_failed");
97+
showErrorMessage({ code: "LOGIN_FAILED", message: e.message });
8498
});
8599
})();
86100
</script>
@@ -102,16 +116,17 @@ protected void doFilterInternal(@NonNull HttpServletRequest request, @NonNull Ht
102116
csrf = (CsrfToken) request.getAttribute("_csrf");
103117
}
104118

105-
String html = generateHtml(
106-
csrf != null ? csrf.getToken() : "",
107-
csrf != null ? csrf.getHeaderName() : "X-CSRF-TOKEN"
108-
);
119+
String html = generateHtml(csrf);
109120

110121
response.setContentType(MediaType.TEXT_HTML_VALUE + ";charset=UTF-8");
111122
response.getWriter().write(html);
112123
}
113124

114-
private String generateHtml(String csrfToken, String csrfHeaderName) {
115-
return String.format(LOGIN_PAGE_HTML, csrfToken, csrfHeaderName);
125+
private String generateHtml(CsrfToken csrf) {
126+
return String.format(
127+
LOGIN_PAGE_HTML,
128+
csrf != null ? csrf.getHeaderName() : "X-CSRF-TOKEN",
129+
csrf != null ? csrf.getToken() : ""
130+
);
116131
}
117132
}

example/src/main/resources/templates/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ <h3><a id="for-developers"></a>For developers</h3>
275275
});
276276

277277
await checkHttpError(resp);
278-
const { eidAuthUri } = await resp.json();
279-
window.location.href = eidAuthUri;
278+
const { auth_uri } = await resp.json();
279+
window.location.href = auth_uri;
280280
return;
281281
}
282282

example/src/test/java/eu/webeid/example/AuthenticationRestControllerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void challengeReturnsNonceWithExpectedBase64Length() throws Exception {
7070

7171
@Test
7272
void mobileInitBuildsDeepLinkWithEmbeddedChallenge() throws Exception {
73-
MvcResult result = mvc.perform(post("/auth/mobile/auth/init")
73+
MvcResult result = mvc.perform(post("/auth/mobile/init")
7474
.with(csrf())
7575
.accept(MediaType.APPLICATION_JSON))
7676
.andExpect(status().isOk())

0 commit comments

Comments
 (0)