Skip to content

Commit 14b29c8

Browse files
NFC-47 Review findings. Add init endpoint, drop /mobile/challenge, update UI.
1 parent 7140015 commit 14b29c8

File tree

4 files changed

+53
-37
lines changed

4 files changed

+53
-37
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,16 @@ public SecurityFilterChain filterChain(HttpSecurity http, AuthTokenDTOAuthentica
5050
var filter = new WebEidAjaxLoginProcessingFilter("/auth/login", authConfig.getAuthenticationManager());
5151

5252
return http
53-
.csrf(csrf -> csrf.ignoringRequestMatchers("/auth/login"))
53+
.csrf(csrf -> csrf.ignoringRequestMatchers("/auth/login", "/auth/mobile/auth/init"))
5454
.authorizeHttpRequests(auth -> auth
5555
.requestMatchers("/", "/error").permitAll()
56-
.requestMatchers("/auth/challenge", "/auth/mobile/challenge").permitAll()
5756
.requestMatchers(HttpMethod.GET, "/auth/eid/login").permitAll()
58-
.requestMatchers(
59-
"/favicon.ico",
60-
"/css/", "/files/", "/img/", "/js/"
61-
).permitAll()
57+
.requestMatchers("/auth/challenge").permitAll()
58+
.requestMatchers(HttpMethod.POST, "/auth/mobile/auth/init").permitAll()
59+
.requestMatchers("/favicon.ico", "/css/**", "/files/**", "/img/**", "/js/**", "/webjars/**").permitAll()
6260
.anyRequest().authenticated()
6361
)
64-
.authenticationProvider(provider)
62+
.authenticationProvider(authTokenDTOAuthenticationProvider)
6563
.addFilterBefore(new WebEidLoginPageGeneratingFilter(), UsernamePasswordAuthenticationFilter.class)
6664
.addFilterBefore(filter, UsernamePasswordAuthenticationFilter.class)
6765
.headers(h -> h.frameOptions(HeadersConfigurer.FrameOptionsConfig::sameOrigin))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package eu.webeid.example.service.dto;
2+
3+
public record MobileAuthInitResponse(String eidAuthUri) {}

example/src/main/java/eu/webeid/example/web/rest/ChallengeController.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,58 @@
2222

2323
package eu.webeid.example.web.rest;
2424

25+
import com.fasterxml.jackson.core.JsonProcessingException;
26+
import com.fasterxml.jackson.databind.ObjectMapper;
2527
import eu.webeid.example.service.dto.ChallengeDTO;
28+
import eu.webeid.example.service.dto.MobileAuthInitResponse;
2629
import eu.webeid.security.challenge.ChallengeNonceGenerator;
30+
import jakarta.servlet.http.HttpServletRequest;
2731
import org.springframework.web.bind.annotation.GetMapping;
32+
import org.springframework.web.bind.annotation.PostMapping;
2833
import org.springframework.web.bind.annotation.RequestMapping;
2934
import org.springframework.web.bind.annotation.RestController;
35+
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
36+
37+
import java.nio.charset.StandardCharsets;
38+
import java.util.Base64;
39+
import java.util.Map;
3040

3141
@RestController
3242
@RequestMapping("auth")
3343
public class ChallengeController {
3444

3545
private final ChallengeNonceGenerator challengeNonceGenerator;
46+
private final ObjectMapper mapper = new ObjectMapper();
3647

3748
public ChallengeController(ChallengeNonceGenerator challengeNonceGenerator) {
3849
this.challengeNonceGenerator = challengeNonceGenerator;
3950
}
4051

4152
@GetMapping("challenge")
4253
public ChallengeDTO challenge() {
43-
return generateChallenge();
54+
final ChallengeDTO challenge = new ChallengeDTO();
55+
challenge.setNonce(challengeNonceGenerator.generateAndStoreNonce().getBase64EncodedNonce());
56+
return challenge;
4457
}
4558

46-
@GetMapping("mobile/challenge")
47-
public ChallengeDTO mobileChallenge() {
48-
return generateChallenge();
49-
}
59+
@PostMapping("mobile/auth/init")
60+
public MobileAuthInitResponse initMobileAuth(HttpServletRequest request) throws JsonProcessingException {
61+
String nonce = challengeNonceGenerator.generateAndStoreNonce().getBase64EncodedNonce();
5062

51-
private ChallengeDTO generateChallenge() {
52-
ChallengeDTO challenge = new ChallengeDTO();
53-
challenge.setNonce(challengeNonceGenerator.generateAndStoreNonce().getBase64EncodedNonce());
54-
return challenge;
63+
String loginUri = ServletUriComponentsBuilder
64+
.fromCurrentContextPath()
65+
.path("/auth/eid/login")
66+
.build()
67+
.toUriString();
68+
69+
String payloadJson = mapper.writeValueAsString(Map.of(
70+
"challenge", nonce,
71+
"login_uri", loginUri
72+
));
73+
74+
String encoded = Base64.getEncoder().encodeToString(payloadJson.getBytes(StandardCharsets.UTF_8));
75+
String eidAppUri = "web-eid-mobile://auth#" + encoded;
76+
77+
return new MobileAuthInitResponse(eidAppUri);
5578
}
5679
}

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

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -264,32 +264,24 @@ <h3><a id="for-developers"></a>For developers</h3>
264264
authButton.disabled = true;
265265

266266
try {
267-
const challengeUrl = isMobileDevice()
268-
? "/auth/mobile/challenge"
269-
: "/auth/challenge";
267+
if (isMobileDevice()) {
268+
const resp = await fetch("/auth/mobile/auth/init", {
269+
method: "POST",
270+
headers: { "Content-Type": "application/json" },
271+
});
272+
await checkHttpError(resp);
273+
const { eidAuthUri } = await resp.json();
274+
window.location.href = eidAuthUri;
275+
return;
276+
}
270277

271-
const challengeResponse = await fetch(challengeUrl, {
272-
method: "GET",
273-
headers: {
274-
"Content-Type": "application/json"
275-
}
278+
const challengeResponse = await fetch("/auth/challenge", {
279+
method: "GET",
280+
headers: { "Content-Type": "application/json" }
276281
});
277282
await checkHttpError(challengeResponse);
278283
const {nonce} = await challengeResponse.json();
279284

280-
if (isMobileDevice()) {
281-
const loginUri = encodeURIComponent(window.location.origin + "/auth/eid/login");
282-
const payload = {
283-
challenge: nonce,
284-
login_uri: loginUri
285-
};
286-
const encoded = btoa(JSON.stringify(payload));
287-
const eidAppUri = `web-eid-mobile://auth#${encoded}`;
288-
289-
window.location.href = eidAppUri;
290-
return;
291-
}
292-
293285
const authToken = await webeid.authenticate(nonce, {lang});
294286

295287
const authTokenResponse = await fetch("/auth/login", {

0 commit comments

Comments
 (0)