Skip to content

Commit 1a41496

Browse files
authored
Merge pull request #3750 from ControlSystemStudio/fix_saveandrestore_login
Fix backwards compatibility in save&restore login
2 parents 4ead464 + 67222e6 commit 1a41496

2 files changed

Lines changed: 70 additions & 4 deletions

File tree

  • app/save-and-restore/model/src/main/java/org/phoebus/applications/saveandrestore/model
  • services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/web/controllers
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (C) 2020 European Spallation Source ERIC.
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation; either version 2
7+
* of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17+
*/
18+
19+
package org.phoebus.applications.saveandrestore.model;
20+
21+
import java.util.List;
22+
23+
/**
24+
* Simple pojo used to convey username and list of roles to a client upon
25+
* login or explicit request.
26+
*/
27+
public class UserData {
28+
29+
private String userName;
30+
private List<String> roles;
31+
32+
public UserData(){
33+
34+
}
35+
36+
public UserData(String userName, List<String> roles){
37+
this.userName = userName;
38+
this.roles = roles;
39+
}
40+
41+
public String getUserName() {
42+
return userName;
43+
}
44+
45+
public void setUserName(String userName) {
46+
this.userName = userName;
47+
}
48+
49+
public List<String> getRoles() {
50+
return roles;
51+
}
52+
53+
public void setRoles(List<String> roles) {
54+
this.roles = roles;
55+
}
56+
}

services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/web/controllers/AuthenticationController.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,23 @@
2020
package org.phoebus.service.saveandrestore.web.controllers;
2121

2222
import org.phoebus.applications.saveandrestore.model.LoginCredentials;
23+
import org.phoebus.applications.saveandrestore.model.UserData;
2324
import org.springframework.beans.factory.annotation.Autowired;
2425
import org.springframework.http.HttpStatus;
2526
import org.springframework.http.ResponseEntity;
2627
import org.springframework.security.authentication.AuthenticationManager;
2728
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
2829
import org.springframework.security.core.Authentication;
2930
import org.springframework.security.core.AuthenticationException;
31+
import org.springframework.security.core.GrantedAuthority;
3032
import org.springframework.web.bind.annotation.PostMapping;
3133
import org.springframework.web.bind.annotation.RequestBody;
3234
import org.springframework.web.bind.annotation.RestController;
3335

36+
import java.util.List;
3437
import java.util.logging.Level;
3538
import 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

Comments
 (0)