Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2020 European Spallation Source ERIC.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

package org.phoebus.applications.saveandrestore.model;

import java.util.List;

/**
* Simple pojo used to convey username and list of roles to a client upon
* login or explicit request.
*/
public class UserData {

private String userName;
private List<String> roles;

public UserData(){

}

public UserData(String userName, List<String> roles){
this.userName = userName;
this.roles = roles;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public List<String> getRoles() {
return roles;
}

public void setRoles(List<String> roles) {
this.roles = roles;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,23 @@
package org.phoebus.service.saveandrestore.web.controllers;

import org.phoebus.applications.saveandrestore.model.LoginCredentials;
import org.phoebus.applications.saveandrestore.model.UserData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
* Controller class for user authentication endpoints.
Expand All @@ -51,15 +55,21 @@
* @return A {@link ResponseEntity} indicating the outcome, e.g. OK (200) or UNAUTHORIZED (401)
*/
@PostMapping(value = "login")
public ResponseEntity<Void> login(@RequestBody LoginCredentials loginCredentials) {
public ResponseEntity<UserData> login(@RequestBody LoginCredentials loginCredentials) {
Authentication authentication =
new UsernamePasswordAuthenticationToken(loginCredentials.username(), loginCredentials.password());
try {
authenticationManager.authenticate(authentication);
authentication = authenticationManager.authenticate(authentication);
} catch (AuthenticationException e) {
Logger.getLogger(AuthenticationController.class.getName()).log(Level.WARNING, "Unable to authenticate user " + loginCredentials.username(), e);
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
return new ResponseEntity<>(
null,
HttpStatus.UNAUTHORIZED);
}
return new ResponseEntity<>(HttpStatus.OK);
List<String> roles = authentication.getAuthorities().stream()
.map(GrantedAuthority::getAuthority).collect(Collectors.toList());

Check warning on line 70 in services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/web/controllers/AuthenticationController.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this usage of 'Stream.collect(Collectors.toList())' with 'Stream.toList()' and ensure that the list is unmodified.

See more on https://sonarcloud.io/project/issues?id=ControlSystemStudio_phoebus&issues=AZ0kaORib9IEKqTkIvqP&open=AZ0kaORib9IEKqTkIvqP&pullRequest=3750
return new ResponseEntity<>(
new UserData(loginCredentials.username(), roles),
HttpStatus.OK);
}
}
Loading