2020package org .phoebus .service .saveandrestore .web .controllers ;
2121
2222import org .phoebus .applications .saveandrestore .model .LoginCredentials ;
23+ import org .phoebus .applications .saveandrestore .model .UserData ;
2324import org .springframework .beans .factory .annotation .Autowired ;
2425import org .springframework .http .HttpStatus ;
2526import org .springframework .http .ResponseEntity ;
2627import org .springframework .security .authentication .AuthenticationManager ;
2728import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
2829import org .springframework .security .core .Authentication ;
2930import org .springframework .security .core .AuthenticationException ;
31+ import org .springframework .security .core .GrantedAuthority ;
3032import org .springframework .web .bind .annotation .PostMapping ;
3133import org .springframework .web .bind .annotation .RequestBody ;
3234import org .springframework .web .bind .annotation .RestController ;
3335
36+ import java .util .List ;
3437import java .util .logging .Level ;
3538import java .util .logging .Logger ;
39+ import java .util .stream .Collectors ;
3640
3741/**
3842 * Controller class for user authentication endpoints.
@@ -51,15 +55,21 @@ public class AuthenticationController extends BaseController {
5155 * @return A {@link ResponseEntity} indicating the outcome, e.g. OK (200) or UNAUTHORIZED (401)
5256 */
5357 @ PostMapping (value = "login" )
54- public ResponseEntity <Void > login (@ RequestBody LoginCredentials loginCredentials ) {
58+ public ResponseEntity <UserData > login (@ RequestBody LoginCredentials loginCredentials ) {
5559 Authentication authentication =
5660 new UsernamePasswordAuthenticationToken (loginCredentials .username (), loginCredentials .password ());
5761 try {
58- authenticationManager .authenticate (authentication );
62+ authentication = authenticationManager .authenticate (authentication );
5963 } catch (AuthenticationException e ) {
6064 Logger .getLogger (AuthenticationController .class .getName ()).log (Level .WARNING , "Unable to authenticate user " + loginCredentials .username (), e );
61- return new ResponseEntity <>(HttpStatus .UNAUTHORIZED );
65+ return new ResponseEntity <>(
66+ null ,
67+ HttpStatus .UNAUTHORIZED );
6268 }
63- return new ResponseEntity <>(HttpStatus .OK );
69+ List <String > roles = authentication .getAuthorities ().stream ()
70+ .map (GrantedAuthority ::getAuthority ).collect (Collectors .toList ());
71+ return new ResponseEntity <>(
72+ new UserData (loginCredentials .username (), roles ),
73+ HttpStatus .OK );
6474 }
6575}
0 commit comments