1717package com .helioauth .passkeys .api .controller ;
1818
1919import com .fasterxml .jackson .core .JsonProcessingException ;
20+ import com .helioauth .passkeys .api .domain .ClientApplication ;
2021import com .helioauth .passkeys .api .generated .api .SignInApi ;
2122import com .helioauth .passkeys .api .generated .api .SignUpApi ;
2223import com .helioauth .passkeys .api .generated .models .SignInFinishRequest ;
2930import com .helioauth .passkeys .api .generated .models .SignUpStartResponse ;
3031import com .helioauth .passkeys .api .service .UserSignInService ;
3132import com .helioauth .passkeys .api .service .UserSignupService ;
33+ import com .helioauth .passkeys .api .service .dto .UserSignupStartRequest ;
3234import com .helioauth .passkeys .api .service .exception .SignInFailedException ;
3335import lombok .RequiredArgsConstructor ;
3436import lombok .extern .slf4j .Slf4j ;
3537import org .springframework .http .HttpStatus ;
3638import org .springframework .http .ResponseEntity ;
39+ import org .springframework .security .core .Authentication ;
40+ import org .springframework .security .core .context .SecurityContextHolder ;
3741import org .springframework .web .bind .annotation .CrossOrigin ;
3842import org .springframework .web .bind .annotation .RequestBody ;
3943import org .springframework .web .bind .annotation .RestController ;
4650 */
4751@ Slf4j
4852@ RestController
49- @ CrossOrigin (origins = "*" )
5053@ RequiredArgsConstructor
54+ @ CrossOrigin (origins = "*" )
5155public class CredentialsController implements SignUpApi , SignInApi {
5256
5357 private final UserSignInService userSignInService ;
5458 private final UserSignupService userSignupService ;
5559
5660 public ResponseEntity <SignUpStartResponse > postSignupStart (@ RequestBody @ Valid SignUpStartRequest request ) {
61+ Authentication authentication = SecurityContextHolder .getContext ().getAuthentication ();
62+ if (authentication == null || !(authentication .getPrincipal () instanceof ClientApplication clientApp )) {
63+ log .error ("Signup start request received without valid ClientApplication authentication." );
64+ throw new ResponseStatusException (HttpStatus .UNAUTHORIZED , "Client application not authenticated" );
65+ }
66+
67+ String rpId = clientApp .getRelyingPartyHostname ();
68+ String rpName = clientApp .getRelyingPartyName ();
69+
70+ if (rpId == null || rpId .isBlank ()) {
71+ log .error ("Authenticated ClientApplication (ID: {}) is missing a valid relyingPartyHostname." , clientApp .getId ());
72+ throw new ResponseStatusException (HttpStatus .INTERNAL_SERVER_ERROR , "Client application configuration error: Missing RP hostname" );
73+ }
74+
5775 return ResponseEntity .ok (
58- userSignupService .startRegistration (request .getName ())
76+ userSignupService .startRegistration (UserSignupStartRequest .builder ()
77+ .name (request .getName ())
78+ .rpId (rpId )
79+ .rpName (rpName )
80+ .build ()
81+ )
5982 );
6083 }
6184
@@ -88,4 +111,4 @@ public ResponseEntity<SignInFinishResponse> finishSignInCredential(@RequestBody
88111 throw new ResponseStatusException (HttpStatus .BAD_REQUEST , "Sign in failed" );
89112 }
90113 }
91- }
114+ }
0 commit comments