Merge branch 'cose' into 'dev'

Added /auth/profile

See merge request sa4-2020/the-sanmarinoes/backend!64
This commit is contained in:
Claudio Maggioni 2020-03-25 22:45:27 +01:00
commit 1d18d9ae85
3 changed files with 11 additions and 3 deletions

View file

@ -1,6 +1,5 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.config; package ch.usi.inf.sa4.sanmarinoes.smarthut.config;
import java.util.List; import java.util.List;
import java.util.function.Predicate; import java.util.function.Predicate;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -75,7 +74,8 @@ public class SpringFoxConfig {
.or(PathSelectors.regex("/sensor.*")::apply) .or(PathSelectors.regex("/sensor.*")::apply)
.or(PathSelectors.regex("/smartPlug.*")::apply) .or(PathSelectors.regex("/smartPlug.*")::apply)
.or(PathSelectors.regex("/switch.*")::apply) .or(PathSelectors.regex("/switch.*")::apply)
.or(PathSelectors.regex("/motionSensor.*")::apply); .or(PathSelectors.regex("/motionSensor.*")::apply)
.or(PathSelectors.regex("/auth/profile.*")::apply);
} }
/** /**

View file

@ -6,6 +6,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.JWTResponse;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.UnauthorizedException; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.UnauthorizedException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.UserNotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.UserNotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.security.Principal;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.BadCredentialsException;
@ -69,6 +70,11 @@ public class AuthenticationController {
return new JWTResponse(token); return new JWTResponse(token);
} }
@GetMapping("/profile")
public User profile(final Principal principal) {
return userRepository.findByUsername(principal.getName());
}
private void authenticate(String username, String password) throws UnauthorizedException { private void authenticate(String username, String password) throws UnauthorizedException {
try { try {
authenticationManager.authenticate( authenticationManager.authenticate(

View file

@ -1,5 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import java.util.Objects; import java.util.Objects;
import javax.persistence.*; import javax.persistence.*;
@ -24,6 +25,7 @@ public class User {
/** A properly salted way to store the password */ /** A properly salted way to store the password */
@Column(nullable = false) @Column(nullable = false)
@GsonExclude
private String password; private String password;
/** /**
@ -34,7 +36,7 @@ public class User {
private String email; private String email;
@Column(nullable = false) @Column(nullable = false)
@ApiModelProperty(hidden = true) @GsonExclude
private Boolean isEnabled = false; private Boolean isEnabled = false;
public Long getId() { public Long getId() {