Skip to content

Commit 7ea942c

Browse files
aarmamSanderKondratjevNortal
authored andcommitted
NFC-47 Authentication flow fixes and updates
Signed-off-by: Sander Kondratjev [email protected]
1 parent befb92b commit 7ea942c

File tree

2 files changed

+41
-39
lines changed

2 files changed

+41
-39
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@
4141
import org.springframework.security.web.util.matcher.RequestMatcher;
4242
import org.springframework.web.filter.OncePerRequestFilter;
4343
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
44+
import org.springframework.web.util.UriComponentsBuilder;
4445

4546
import java.io.IOException;
4647
import java.nio.charset.StandardCharsets;
4748
import java.util.Base64;
4849

4950
public final class WebEidMobileAuthInitFilter extends OncePerRequestFilter {
51+
private static final String WEB_EID_MOBILE_AUTH_PATH = "auth";
5052
private static final ObjectWriter OBJECT_WRITER = new ObjectMapper().writer();
5153
private final RequestMatcher requestMatcher;
5254
private final ChallengeNonceGenerator nonceGenerator;
@@ -79,10 +81,20 @@ protected void doFilterInternal(@NonNull HttpServletRequest request,
7981
webEidMobileProperties.requestSigningCert() ? Boolean.TRUE : null)
8082
);
8183
String encoded = Base64.getEncoder().encodeToString(payloadJson.getBytes(StandardCharsets.UTF_8));
82-
String eidAuthUri = "web-eid-mobile://auth#" + encoded;
84+
String authUri = getAuthUri(encoded);
8385

8486
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
85-
OBJECT_WRITER.writeValue(response.getWriter(), new AuthUri(eidAuthUri));
87+
OBJECT_WRITER.writeValue(response.getWriter(), new AuthUri(authUri));
88+
}
89+
90+
private String getAuthUri(String encodedPayload) {
91+
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(webEidMobileProperties.baseRequestUri());
92+
if (webEidMobileProperties.baseRequestUri().startsWith("http")) {
93+
builder.pathSegment(WEB_EID_MOBILE_AUTH_PATH);
94+
} else {
95+
builder.host(WEB_EID_MOBILE_AUTH_PATH);
96+
}
97+
return builder.fragment(encodedPayload).toUriString();
8698
}
8799

88100
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)

example/src/main/resources/templates/webeid-login.html

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,57 +13,47 @@
1313
</div>
1414

1515
<script type="module" th:inline="javascript">
16-
import { showErrorMessage } from "/js/errors.js";
16+
import {showErrorMessage, checkHttpError} from "/js/errors.js";
1717

1818
// Using an async IIFE for mobile WebView compatibility:
1919
// top-level await is not supported in some mobile browsers/WebViews.
2020
(async function () {
21-
const frag = location.hash ? location.hash.substring(1) : "";
22-
if (!frag) {
23-
showErrorMessage({ code: "UNKNOWN_ERROR", message: "Missing authentication payload" });
24-
return;
21+
const fragment = window.location.hash.slice(1);
22+
if (!fragment) {
23+
throw new Error("Missing authentication payload");
2524
}
2625

2726
let payload;
2827
try {
29-
payload = JSON.parse(atob(frag));
28+
payload = JSON.parse(atob(fragment));
3029
} catch (e) {
31-
console.error("Failed to parse payload", e);
32-
showErrorMessage({ code: "UNKNOWN_ERROR", message: "Failed to parse authentication payload" });
33-
return;
30+
console.error(e)
31+
throw new Error("Failed to parse the authentication response");
3432
}
3533

3634
if (payload.error) {
37-
showErrorMessage({
38-
code: payload.code ?? "UNKNOWN_ERROR",
39-
message: payload.message ?? "Authentication failed"
40-
});
41-
return;
35+
const error = new Error(payload.message ?? "Authentication failed");
36+
error.code = payload.code;
37+
throw error;
4238
}
4339

44-
const authToken = payload["auth-token"];
45-
46-
try {
47-
const response = await fetch(/*[[${loginProcessingPath}]]*/, {
48-
method: "POST",
49-
headers: {
50-
"Content-Type": "application/json",
51-
"X-CSRF-TOKEN": /*[[${csrfToken}]]*/
52-
},
53-
body: JSON.stringify(authToken),
54-
credentials: "include"
55-
});
56-
57-
if (!response.ok) {
58-
throw new Error("HTTP " + response.status);
59-
}
60-
61-
window.location.replace("/welcome");
62-
} catch (error) {
63-
console.error(error);
64-
showErrorMessage(error);
65-
}
66-
})();
40+
const authToken = payload["auth_token"];
41+
const response = await fetch(/*[[${loginProcessingPath}]]*/, {
42+
method: "POST",
43+
headers: {
44+
"Content-Type": "application/json",
45+
"X-CSRF-TOKEN": /*[[${csrfToken}]]*/
46+
},
47+
body: JSON.stringify(authToken),
48+
credentials: "include"
49+
});
50+
await checkHttpError(response);
51+
52+
window.location.replace("/welcome");
53+
})().catch((error) => {
54+
console.error(error);
55+
showErrorMessage(error);
56+
});
6757
</script>
6858
</body>
6959
</html>

0 commit comments

Comments
 (0)