Skip to content

Commit 5afe8e9

Browse files
NFC-47 Fix redirection from app after authentication
1 parent 858e7f3 commit 5afe8e9

File tree

5 files changed

+83
-13
lines changed

5 files changed

+83
-13
lines changed

example/src/main/java/eu/webeid/example/config/ApplicationConfiguration.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import eu.webeid.example.security.WebEidAjaxLoginProcessingFilter;
2727
import org.springframework.context.annotation.Bean;
2828
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.http.HttpMethod;
2930
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
3031
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
3132
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
@@ -43,20 +44,37 @@
4344
public class ApplicationConfiguration implements WebMvcConfigurer {
4445

4546
@Bean
46-
public SecurityFilterChain filterChain(HttpSecurity http, AuthTokenDTOAuthenticationProvider authTokenDTOAuthenticationProvider, AuthenticationConfiguration authConfig) throws Exception {
47+
SecurityFilterChain filterChain(
48+
HttpSecurity http,
49+
AuthTokenDTOAuthenticationProvider provider,
50+
AuthenticationConfiguration authConfig) throws Exception {
51+
52+
var filter = new WebEidAjaxLoginProcessingFilter("/auth/login", authConfig.getAuthenticationManager());
53+
4754
return http
48-
.authenticationProvider(authTokenDTOAuthenticationProvider)
49-
.addFilterBefore(new WebEidAjaxLoginProcessingFilter("/auth/login", authConfig.getAuthenticationManager()),
50-
UsernamePasswordAuthenticationFilter.class)
51-
.logout(logout -> logout.logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler()))
52-
.headers(headers -> headers.frameOptions(HeadersConfigurer.FrameOptionsConfig::sameOrigin))
53-
.build();
55+
.csrf(csrf -> csrf.ignoringRequestMatchers("/auth/login"))
56+
.authorizeHttpRequests(auth -> auth
57+
.requestMatchers("/", "/welcome", "/error").permitAll()
58+
.requestMatchers("/auth/challenge", "/auth/mobile/challenge").permitAll()
59+
.requestMatchers(HttpMethod.GET, "/auth/eid/login").permitAll()
60+
.requestMatchers(
61+
"/favicon.ico",
62+
"/css/**", "/js/**", "/images/**", "/webjars/**"
63+
).permitAll()
64+
.anyRequest().authenticated()
65+
)
66+
.authenticationProvider(provider)
67+
.addFilterBefore(filter, UsernamePasswordAuthenticationFilter.class)
68+
.headers(h -> h.frameOptions(HeadersConfigurer.FrameOptionsConfig::sameOrigin))
69+
.logout(l -> l.logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler()))
70+
.build();
5471
}
5572

5673
@Override
5774
public void addViewControllers(ViewControllerRegistry registry) {
5875
registry.addViewController("/").setViewName("index");
5976
registry.addViewController("/welcome").setViewName("welcome");
77+
registry.addViewController("/auth/eid/login").setViewName("eid-login");
6078
}
6179

6280
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,14 @@ public Authentication authenticate(Authentication auth) throws AuthenticationExc
7171
final List<GrantedAuthority> authorities = Collections.singletonList(USER_ROLE);
7272

7373
try {
74-
final String nonce = challengeNonceStore.getAndRemove().getBase64EncodedNonce();
74+
String nonce;
75+
if (authToken.getChallenge() != null && !authToken.getChallenge().isEmpty()) {
76+
nonce = authToken.getChallenge();
77+
LOG.info("Using challenge from token itself");
78+
} else {
79+
nonce = challengeNonceStore.getAndRemove().getBase64EncodedNonce();
80+
LOG.info("Using challenge from session store");
81+
}
7582
final X509Certificate userCertificate = tokenValidator.validate(authToken, nonce);
7683
return WebEidAuthentication.fromCertificate(userCertificate, authorities);
7784
} catch (AuthTokenException e) {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import eu.webeid.example.security.dto.AuthTokenDTO;
3030
import jakarta.servlet.FilterChain;
3131
import jakarta.servlet.ServletException;
32+
import jakarta.servlet.ServletRequest;
33+
import jakarta.servlet.ServletResponse;
3234
import jakarta.servlet.http.HttpServletRequest;
3335
import jakarta.servlet.http.HttpServletResponse;
3436
import org.slf4j.Logger;
@@ -90,4 +92,18 @@ protected void successfulAuthentication(HttpServletRequest request, HttpServletR
9092
super.successfulAuthentication(request, response, chain, authResult);
9193
securityContextRepository.saveContext(SecurityContextHolder.getContext(), request, response);
9294
}
95+
96+
@Override
97+
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
98+
throws IOException, ServletException {
99+
100+
HttpServletRequest request = (HttpServletRequest) req;
101+
102+
if (!HttpMethod.POST.matches(request.getMethod())) {
103+
chain.doFilter(req, res);
104+
return;
105+
}
106+
107+
super.doFilter(req, res, chain);
108+
}
93109
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Signing you in…</title>
6+
</head>
7+
<body>
8+
<script>
9+
(function () {
10+
const frag = location.hash ? location.hash.substring(1) : "";
11+
if (!frag) { location.replace("/?mobileAuthError=missing_payload"); return; }
12+
13+
let payload;
14+
try { payload = JSON.parse(atob(frag)); } catch (e) {
15+
location.replace("/?mobileAuthError=bad_payload"); return;
16+
}
17+
18+
const authToken = payload["auth-token"] ?? payload;
19+
20+
fetch("/auth/login", {
21+
method: "POST",
22+
headers: {"Content-Type": "application/json"},
23+
body: JSON.stringify({ "auth-token": authToken })
24+
}).then(r => {
25+
if (!r.ok) throw new Error("HTTP " + r.status);
26+
location.replace("/welcome");
27+
}).catch(() => location.replace("/?mobileAuthError=login_failed"));
28+
})();
29+
</script>
30+
Signing you in…
31+
</body>
32+
</html>

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,17 +279,14 @@ <h3><a id="for-developers"></a>For developers</h3>
279279
const {nonce} = await challengeResponse.json();
280280

281281
if (isMobileDevice()) {
282-
const loginUri = encodeURIComponent(window.location.origin + "/");
282+
const loginUri = encodeURIComponent(window.location.origin + "/auth/eid/login");
283283
const payload = {
284284
challenge: nonce,
285285
login_uri: loginUri
286286
};
287287
const encoded = btoa(JSON.stringify(payload));
288+
const eidAppUri = `web-eid-mobile://auth#${encoded}`;
288289

289-
// TODO: Replace with actual URL once DigiDoc app supports app links
290-
const eidAppUri = `TODO-MOBILE-EID-APP-LAUNCH-URL#${encoded}`;
291-
292-
alert("Opening eID app for authentication...\n\nIf nothing happens, the app may not yet support mobile login.");
293290
window.location.href = eidAppUri;
294291
return;
295292
}

0 commit comments

Comments
 (0)