Login is now possible
This commit is contained in:
parent
d5e3bfc566
commit
211b7279b5
@ -34,6 +34,9 @@ dependencies {
|
||||
|
||||
//JSON Parser
|
||||
implementation 'com.google.code.gson:gson:2.8.5'
|
||||
|
||||
compile group: 'org.springframework.security', name: 'spring-security-core', version: '5.1.4.RELEASE'
|
||||
|
||||
}
|
||||
|
||||
node {
|
||||
|
||||
@ -5,9 +5,14 @@ import hhn.labsw.bugageocaching.entities.*;
|
||||
import hhn.labsw.bugageocaching.exceptions.IllegalParameterException;
|
||||
import hhn.labsw.bugageocaching.repositories.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.crypto.bcrypt.BCrypt;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.time.Clock;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
@RestController
|
||||
public class Controller {
|
||||
@ -33,12 +38,35 @@ public class Controller {
|
||||
@Autowired
|
||||
UserRepository userRepository;
|
||||
|
||||
private AtomicLong counter = new AtomicLong();
|
||||
|
||||
@RequestMapping("/api/allCaches")
|
||||
@ResponseBody
|
||||
public String getAllCaches() {
|
||||
return new Gson().toJson(cacheRepository.findAll());
|
||||
}
|
||||
|
||||
@RequestMapping("/api/login")
|
||||
@ResponseBody
|
||||
public ResponseEntity<Object> login(@RequestParam(value = "username", defaultValue = "") String username,
|
||||
@RequestParam(value = "password", defaultValue = "") String password){
|
||||
if(username == "" || password == ""){
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
|
||||
}
|
||||
if(userRepository.findByUsername(username) == null){
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
|
||||
}
|
||||
|
||||
if(BCrypt.checkpw(password, userRepository.findByUsername(username).getPassword())){
|
||||
String token = BCrypt.hashpw(String.valueOf(System.currentTimeMillis() + counter.incrementAndGet()), BCrypt.gensalt());
|
||||
String hashedToken = BCrypt.hashpw(token, BCrypt.gensalt());
|
||||
userRepository.findByUsername(username).setToken(hashedToken);
|
||||
userRepository.save(userRepository.findByUsername(username));
|
||||
return ResponseEntity.ok(new Gson().toJson(token));
|
||||
}
|
||||
return ResponseEntity.status(HttpStatus.BAD_GATEWAY).body(null);
|
||||
}
|
||||
|
||||
@RequestMapping("/api/startCache")
|
||||
public @ResponseBody
|
||||
String startCache(@RequestParam(value = "userID", defaultValue = "-1") String userID,
|
||||
@ -61,7 +89,7 @@ public class Controller {
|
||||
Cache cache = cacheOptional.get();
|
||||
bearbeitet.setCache(cache);
|
||||
} else {
|
||||
throw new IllegalParameterException( "There is no cache with the ID " + cacheID);
|
||||
throw new IllegalParameterException("There is no cache with the ID " + cacheID);
|
||||
}
|
||||
|
||||
Optional<CacheAccesDefinition> cacheAccesDefinitionOptional =
|
||||
@ -83,7 +111,7 @@ public class Controller {
|
||||
Cache cache = cacheOptional.get();
|
||||
return new Gson().toJson(cache);
|
||||
} else {
|
||||
throw new IllegalParameterException( "There is no cache with the ID " + cacheID);
|
||||
throw new IllegalParameterException("There is no cache with the ID " + cacheID);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
34
src/main/java/hhn/labsw/bugageocaching/entities/Role.java
Normal file
34
src/main/java/hhn/labsw/bugageocaching/entities/Role.java
Normal file
@ -0,0 +1,34 @@
|
||||
package hhn.labsw.bugageocaching.entities;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Role {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public Role() {
|
||||
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
package hhn.labsw.bugageocaching.entities;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table
|
||||
@ -14,10 +15,13 @@ public class User {
|
||||
private String lastname;
|
||||
private String username;
|
||||
private int rankingPointsSum;
|
||||
private String discriminator; //should be Admin or Cacher
|
||||
private String email;
|
||||
private String password;
|
||||
private String salt;
|
||||
|
||||
@ManyToMany
|
||||
private List<Role> roles;
|
||||
|
||||
private String token;
|
||||
|
||||
@ManyToOne
|
||||
private Team team;
|
||||
@ -62,14 +66,6 @@ public class User {
|
||||
this.rankingPointsSum = rankingPointsSum;
|
||||
}
|
||||
|
||||
public String getDiscriminator() {
|
||||
return discriminator;
|
||||
}
|
||||
|
||||
public void setDiscriminator(String discriminator) {
|
||||
this.discriminator = discriminator;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
@ -86,14 +82,6 @@ public class User {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getSalt() {
|
||||
return salt;
|
||||
}
|
||||
|
||||
public void setSalt(String salt) {
|
||||
this.salt = salt;
|
||||
}
|
||||
|
||||
public Team getTeam() {
|
||||
return team;
|
||||
}
|
||||
@ -101,4 +89,20 @@ public class User {
|
||||
public void setTeam(Team team) {
|
||||
this.team = team;
|
||||
}
|
||||
|
||||
public List<Role> getRoles() {
|
||||
return roles;
|
||||
}
|
||||
|
||||
public void setRoles(List<Role> roles) {
|
||||
this.roles = roles;
|
||||
}
|
||||
|
||||
public String getToken() {
|
||||
return token;
|
||||
}
|
||||
|
||||
public void setToken(String token) {
|
||||
this.token = token;
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,4 +4,5 @@ import hhn.labsw.bugageocaching.entities.User;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface UserRepository extends CrudRepository<User, Integer> {
|
||||
User findByUsername(String username);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user