A heckin ton. Mostly hackish
This commit is contained in:
parent
052052d76b
commit
b229ff9a15
70 changed files with 2226 additions and 881 deletions
|
@ -1,20 +1,23 @@
|
|||
package achievements;
|
||||
|
||||
import achievements.misc.DbConnectionService;
|
||||
import achievements.misc.DbConnection;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@SpringBootApplication
|
||||
@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
|
||||
@EnableScheduling
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
var context = SpringApplication.run(Application.class, args);
|
||||
|
||||
// Verify the database connection succeeded
|
||||
var db = context.getBean(DbConnectionService.class);
|
||||
var db = context.getBean(DbConnection.class);
|
||||
if (db.getConnection() == null) {
|
||||
SpringApplication.exit(context, () -> 0);
|
||||
}
|
||||
|
@ -25,9 +28,9 @@ public class Application {
|
|||
return new WebMvcConfigurer() {
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry
|
||||
.addMapping("/*")
|
||||
.allowedOrigins("*");
|
||||
registry
|
||||
.addMapping("/**")
|
||||
.allowedOrigins("*");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,11 +0,0 @@
|
|||
package achievements.controllers;
|
||||
|
||||
import achievements.data.Profile;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
public class DataController {
|
||||
|
||||
public void getProfile(@RequestBody Profile.Query query) {
|
||||
|
||||
}
|
||||
}
|
|
@ -7,13 +7,13 @@ import achievements.services.AuthenticationService;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import static org.springframework.web.bind.annotation.RequestMethod.POST;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/auth")
|
||||
public class LoginController {
|
||||
|
||||
@Autowired
|
||||
|
@ -26,17 +26,11 @@ public class LoginController {
|
|||
*
|
||||
* -1 => Unknown error
|
||||
*/
|
||||
@RequestMapping(value = "/create_user", method = POST, consumes = "application/json", produces = "application/json")
|
||||
@PostMapping(value = "/create_user", consumes = "application/json", produces = "application/json")
|
||||
public ResponseEntity createUser(@RequestBody User user) {
|
||||
var response = authService.createUser(user);
|
||||
if (response.status == 0) {
|
||||
return ResponseEntity.ok(
|
||||
new Session(
|
||||
authService.session().generate(response.id),
|
||||
response.id,
|
||||
response.hue
|
||||
)
|
||||
);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(response.session);
|
||||
} else if (response.status > 0) {
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new APError(response.status));
|
||||
} else {
|
||||
|
@ -56,17 +50,11 @@ public class LoginController {
|
|||
*
|
||||
* -1 => Unknown error
|
||||
*/
|
||||
@RequestMapping(value = "/login", method = POST, consumes = "application/json", produces = "application/json")
|
||||
@PostMapping(value = "/login", consumes = "application/json", produces = "application/json")
|
||||
public ResponseEntity login(@RequestBody User user) {
|
||||
var response = authService.login(user);
|
||||
if (response.status == 0) {
|
||||
return ResponseEntity.ok(
|
||||
new Session(
|
||||
authService.session().generate(response.id),
|
||||
response.id,
|
||||
response.hue
|
||||
)
|
||||
);
|
||||
return ResponseEntity.ok(response.session);
|
||||
} else if (response.status > 0) {
|
||||
// Hardcoded 1 response code
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new APError(1));
|
||||
|
@ -75,7 +63,16 @@ public class LoginController {
|
|||
}
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/logout", method = POST, consumes = "application/json", produces = "application/json")
|
||||
@PostMapping(value = "/refresh", consumes = "application/json", produces = "application/json")
|
||||
public ResponseEntity refresh(@RequestBody Session key) {
|
||||
if (authService.refresh(key)) {
|
||||
return ResponseEntity.ok("{}");
|
||||
} else {
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("{}");
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping(value = "/logout", consumes = "application/json")
|
||||
public ResponseEntity logout(@RequestBody Session session) {
|
||||
authService.logout(session);
|
||||
return ResponseEntity.ok("{}");
|
||||
|
|
|
@ -0,0 +1,42 @@
|
|||
package achievements.controllers;
|
||||
|
||||
import achievements.services.PlatformService;
|
||||
import org.apache.tomcat.util.http.fileupload.IOUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
@RestController
|
||||
public class PlatformController {
|
||||
|
||||
@Autowired
|
||||
private PlatformService platforms;
|
||||
|
||||
@GetMapping(value = "/platform/image/{id}", produces = "application/json")
|
||||
public void getPlatformImage(@PathVariable("id") int id, HttpServletResponse response) {
|
||||
try {
|
||||
var file = new File("images/platform/" + id + ".png");
|
||||
if (file.exists()) {
|
||||
var stream = new FileInputStream(file);
|
||||
IOUtils.copy(stream, response.getOutputStream());
|
||||
|
||||
response.setContentType("image/png");
|
||||
response.setStatus(200);
|
||||
response.flushBuffer();
|
||||
stream.close();
|
||||
} else {
|
||||
response.setStatus(HttpStatus.BAD_REQUEST.value());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,108 @@
|
|||
package achievements.controllers;
|
||||
|
||||
import achievements.data.APError;
|
||||
import achievements.data.APPostRequest;
|
||||
import achievements.data.query.AddPlatformRequest;
|
||||
import achievements.data.query.RemovePlatformRequest;
|
||||
import achievements.data.query.SetUsername;
|
||||
import achievements.services.UserService;
|
||||
import org.apache.tomcat.util.http.fileupload.IOUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@GetMapping(value = "/{user}", produces = "application/json")
|
||||
public ResponseEntity getProfile(@PathVariable("user") int user) {
|
||||
var profile = userService.getProfile(user);
|
||||
if (profile == null) {
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new APError(1, "Failed to get user profile"));
|
||||
} else {
|
||||
return ResponseEntity.ok(profile);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping(value = "/{user}/username", consumes = "application/json", produces = "application/json")
|
||||
public ResponseEntity setUsername(@PathVariable("user") int userId, @RequestBody SetUsername username) {
|
||||
var name = userService.setUsername(userId, username);
|
||||
if (name == 0) {
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body("{}");
|
||||
}
|
||||
return ResponseEntity.badRequest().body("{}");
|
||||
}
|
||||
|
||||
@GetMapping(value = "/{user}/image")
|
||||
public void getProfilePicture(@PathVariable("user") int user, HttpServletResponse response) {
|
||||
var pfp = userService.getProfileImageType(user);
|
||||
if (pfp == null) {
|
||||
|
||||
} else {
|
||||
var file = new File("images/user/" + pfp[0] + "." + pfp[1]);
|
||||
response.setContentType("image/" + pfp[2]);
|
||||
try {
|
||||
var stream = new FileInputStream(file);
|
||||
IOUtils.copy(stream, response.getOutputStream());
|
||||
|
||||
response.flushBuffer();
|
||||
stream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping(value = "/{user}/image", consumes = "multipart/form-data", produces = "application/json")
|
||||
public ResponseEntity setProfilePicture(@PathVariable("user") int user, @RequestPart APPostRequest session, @RequestPart MultipartFile file) {
|
||||
try {
|
||||
var type = userService.setProfileImageType(user, session.getKey(), file.getContentType());
|
||||
if ("not_an_image".equals(type)) {
|
||||
return ResponseEntity.badRequest().body("{ \"code\": 1, \"message\": \"Not an image type\" }");
|
||||
} else if ("unsupported_type".equals(type)) {
|
||||
return ResponseEntity.badRequest().body("{ \"code\": 1, \"message\": \"Unsupported file type\" }");
|
||||
} else if ("forbidden".equals(type)) {
|
||||
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("{ \"code\": 2, \"message\": \"Invalid credentials\" }");
|
||||
} else if (!"unknown".equals(type)) {
|
||||
var pfp = new FileOutputStream("images/user/" + user + "." + type);
|
||||
FileCopyUtils.copy(file.getInputStream(), pfp);
|
||||
pfp.close();
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body("{ \"code\": 0, \"message\": \"Success\" }");
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{ \"code\": -1, \"message\": \"Unknown error\" }");
|
||||
}
|
||||
|
||||
@PostMapping(value = "/{user}/platforms/add", consumes = "application/json", produces = "application/json")
|
||||
public ResponseEntity addPlatformForUser(@PathVariable("user") int userId, @RequestBody AddPlatformRequest request) {
|
||||
var result = userService.addPlatform(userId, request);
|
||||
if (result == 0) {
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body("{}");
|
||||
} else {
|
||||
return ResponseEntity.badRequest().body("{}");
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping(value = "/{user}/platforms/remove", consumes = "application/json", produces = "application/json")
|
||||
public ResponseEntity removePlatformForUser(@PathVariable("user") int userId, @RequestBody RemovePlatformRequest request) {
|
||||
var result = userService.removePlatform(userId, request);
|
||||
if (result == 0) {
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body("{}");
|
||||
} else {
|
||||
return ResponseEntity.badRequest().body("{}");
|
||||
}
|
||||
}
|
||||
}
|
17
backend/src/main/java/achievements/data/APPostRequest.java
Normal file
17
backend/src/main/java/achievements/data/APPostRequest.java
Normal file
|
@ -0,0 +1,17 @@
|
|||
package achievements.data;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class APPostRequest {
|
||||
|
||||
@JsonProperty("key")
|
||||
private String key;
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
}
|
|
@ -7,37 +7,53 @@ import java.util.List;
|
|||
|
||||
public class Profile {
|
||||
|
||||
public static class Query {
|
||||
@JsonProperty("username")
|
||||
private StringFilter string;
|
||||
public static class Platform {
|
||||
@JsonProperty
|
||||
private int id;
|
||||
@JsonProperty("name")
|
||||
private String name;
|
||||
@JsonProperty("connected")
|
||||
private boolean connected;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public boolean getConnected() {
|
||||
return connected;
|
||||
}
|
||||
|
||||
public void setConnected(boolean connected) {
|
||||
this.connected = connected;
|
||||
}
|
||||
}
|
||||
|
||||
@JsonProperty("id")
|
||||
private int id;
|
||||
@JsonProperty("username")
|
||||
private String username;
|
||||
@JsonProperty("plaforms")
|
||||
private List<String> platforms;
|
||||
@JsonProperty("games")
|
||||
private List<Game> games;
|
||||
@JsonProperty("achievements")
|
||||
private List<Achievement> achievements;
|
||||
|
||||
public Profile(int id, String username, List<String> platforms, List<Game> games, List<Achievement> achievements) {
|
||||
this.id = id;
|
||||
this.username = username;
|
||||
this.platforms = platforms;
|
||||
this.games = games;
|
||||
this.achievements = achievements;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
@JsonProperty("completed")
|
||||
private int completed;
|
||||
@JsonProperty("average")
|
||||
private Integer average;
|
||||
@JsonProperty("perfect")
|
||||
private int perfect;
|
||||
@JsonProperty("noteworthy")
|
||||
private List<Achievement> noteworthy;
|
||||
@JsonProperty("platforms")
|
||||
private List<Platform> platforms;
|
||||
/*@JsonProperty("ratings")
|
||||
private List<Rating> ratings;*/
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
|
@ -47,27 +63,43 @@ public class Profile {
|
|||
this.username = username;
|
||||
}
|
||||
|
||||
public List<String> getPlatforms() {
|
||||
public int getCompleted() {
|
||||
return completed;
|
||||
}
|
||||
|
||||
public void setCompleted(int completed) {
|
||||
this.completed = completed;
|
||||
}
|
||||
|
||||
public Integer getAverage() {
|
||||
return average;
|
||||
}
|
||||
|
||||
public void setAverage(Integer average) {
|
||||
this.average = average;
|
||||
}
|
||||
|
||||
public int getPerfect() {
|
||||
return perfect;
|
||||
}
|
||||
|
||||
public void setPerfect(int perfect) {
|
||||
this.perfect = perfect;
|
||||
}
|
||||
|
||||
public List<Achievement> getNoteworthy() {
|
||||
return noteworthy;
|
||||
}
|
||||
|
||||
public void setNoteworthy(List<Achievement> noteworthy) {
|
||||
this.noteworthy = noteworthy;
|
||||
}
|
||||
|
||||
public List<Platform> getPlatforms() {
|
||||
return platforms;
|
||||
}
|
||||
|
||||
public void setPlatforms(List<String> platforms) {
|
||||
public void setPlatforms(List<Platform> platforms) {
|
||||
this.platforms = platforms;
|
||||
}
|
||||
|
||||
public List<Game> getGames() {
|
||||
return games;
|
||||
}
|
||||
|
||||
public void setGames(List<Game> games) {
|
||||
this.games = games;
|
||||
}
|
||||
|
||||
public List<Achievement> getAchievements() {
|
||||
return achievements;
|
||||
}
|
||||
|
||||
public void setAchievements(List<Achievement> achievements) {
|
||||
this.achievements = achievements;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package achievements.data;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class Session {
|
||||
|
@ -10,11 +11,14 @@ public class Session {
|
|||
private int id;
|
||||
@JsonProperty("hue")
|
||||
private int hue;
|
||||
@JsonIgnore
|
||||
private boolean used;
|
||||
|
||||
public Session(String key, int id, int hue) {
|
||||
this.key = key;
|
||||
this.id = id;
|
||||
this.hue = hue;
|
||||
this.key = key;
|
||||
this.id = id;
|
||||
this.hue = hue;
|
||||
this.used = false;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
|
@ -40,4 +44,12 @@ public class Session {
|
|||
public void setHue(int hue) {
|
||||
this.hue = hue;
|
||||
}
|
||||
|
||||
public boolean getUsed() {
|
||||
return used;
|
||||
}
|
||||
|
||||
public void setUsed(boolean used) {
|
||||
this.used = used;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
package achievements.data.query;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class AddPlatformRequest {
|
||||
@JsonProperty("sessionKey")
|
||||
private String sessionKey;
|
||||
@JsonProperty("platformId")
|
||||
private int platformId;
|
||||
@JsonProperty("platformUserId")
|
||||
private String platformUserId;
|
||||
|
||||
public String getSessionKey() {
|
||||
return sessionKey;
|
||||
}
|
||||
|
||||
public void setSessionKey(String sessionKey) {
|
||||
this.sessionKey = sessionKey;
|
||||
}
|
||||
|
||||
public int getPlatformId() {
|
||||
return platformId;
|
||||
}
|
||||
|
||||
public void setPlatformId(int platformId) {
|
||||
this.platformId = platformId;
|
||||
}
|
||||
|
||||
public String getPlatformUserId() {
|
||||
return platformUserId;
|
||||
}
|
||||
|
||||
public void setPlatformUserId(String platformUserId) {
|
||||
this.platformUserId = platformUserId;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package achievements.data.query;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class RemovePlatformRequest {
|
||||
@JsonProperty("sessionKey")
|
||||
private String sessionKey;
|
||||
@JsonProperty("platformId")
|
||||
private int platformId;
|
||||
|
||||
public String getSessionKey() {
|
||||
return sessionKey;
|
||||
}
|
||||
|
||||
public void setSessionKey(String sessionKey) {
|
||||
this.sessionKey = sessionKey;
|
||||
}
|
||||
|
||||
public int getPlatformId() {
|
||||
return platformId;
|
||||
}
|
||||
|
||||
public void setPlatformId(int platformId) {
|
||||
this.platformId = platformId;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package achievements.data.query;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
public class SetUsername {
|
||||
@JsonProperty("sessionKey")
|
||||
private String sessionKey;
|
||||
@JsonProperty("userId")
|
||||
private int userId;
|
||||
@JsonProperty("username")
|
||||
private String username;
|
||||
|
||||
public String getSessionKey() {
|
||||
return sessionKey;
|
||||
}
|
||||
|
||||
public void setSessionKey(String sessionKey) {
|
||||
this.sessionKey = sessionKey;
|
||||
}
|
||||
|
||||
public int getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserId(int userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
}
|
|
@ -10,7 +10,7 @@ import java.sql.SQLException;
|
|||
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
|
||||
|
||||
@Component
|
||||
public class DbConnectionService {
|
||||
public class DbConnection {
|
||||
|
||||
private Connection connection;
|
||||
|
||||
|
@ -23,7 +23,7 @@ public class DbConnectionService {
|
|||
@Value("${database.user.password}")
|
||||
private String password;
|
||||
|
||||
public DbConnectionService() {}
|
||||
public DbConnection() {}
|
||||
|
||||
@PostConstruct
|
||||
public void connect() {
|
|
@ -1,26 +1,64 @@
|
|||
package achievements.misc;
|
||||
|
||||
import achievements.data.Session;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
@Component
|
||||
public class SessionManager {
|
||||
|
||||
private HashMap<String, Integer> session;
|
||||
private HashMap<String, Session> sessions;
|
||||
|
||||
public SessionManager() {
|
||||
session = new HashMap();
|
||||
sessions = new HashMap();
|
||||
}
|
||||
|
||||
public String generate(Integer user) {
|
||||
public Session generate(int user, int hue) {
|
||||
var key = HashManager.encode(HashManager.generateBytes(16));
|
||||
session.put(key, user);
|
||||
return key;
|
||||
var session = new Session(key, user, hue);
|
||||
sessions.put(key, session);
|
||||
return session;
|
||||
}
|
||||
|
||||
public int getUser(String key) {
|
||||
return session.get(key);
|
||||
return sessions.get(key).getId();
|
||||
}
|
||||
|
||||
public void remove(String key) {
|
||||
session.remove(key);
|
||||
sessions.remove(key);
|
||||
}
|
||||
|
||||
public boolean validate(int user, String key) {
|
||||
var foreign = sessions.get(key);
|
||||
return foreign != null && user == foreign.getId();
|
||||
}
|
||||
|
||||
public boolean refresh(String key) {
|
||||
var foreign = sessions.get(key);
|
||||
if (foreign != null) {
|
||||
foreign.setUsed(true);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up inactive sessions
|
||||
@Scheduled(cron = "0 */30 * * * *")
|
||||
public void clean() {
|
||||
var remove = new ArrayList<String>();
|
||||
sessions.forEach((key, session) -> {
|
||||
if (!session.getUsed()) {
|
||||
remove.add(session.getKey());
|
||||
} else {
|
||||
session.setUsed(false);
|
||||
}
|
||||
});
|
||||
for (var session : remove) {
|
||||
sessions.remove(session);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package achievements.services;
|
|||
|
||||
import achievements.data.Session;
|
||||
import achievements.data.User;
|
||||
import achievements.misc.DbConnectionService;
|
||||
import achievements.misc.DbConnection;
|
||||
import achievements.misc.Password;
|
||||
import achievements.misc.SessionManager;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -16,37 +16,33 @@ public class AuthenticationService {
|
|||
|
||||
public static class LoginResponse {
|
||||
public int status;
|
||||
public Integer id;
|
||||
public int hue;
|
||||
public Session session;
|
||||
|
||||
public LoginResponse() {
|
||||
this.status = 0;
|
||||
this.id = null;
|
||||
this.hue = 0;
|
||||
}
|
||||
|
||||
public LoginResponse(int status) {
|
||||
this.status = status;
|
||||
this.id = null;
|
||||
this.status = status;
|
||||
this.session = null;
|
||||
}
|
||||
|
||||
public LoginResponse(int status, int id, int hue) {
|
||||
this.status = status;
|
||||
this.id = id;
|
||||
this.hue = hue;
|
||||
public LoginResponse(int status, Session session) {
|
||||
this.status = status;
|
||||
this.session = session;
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private DbConnectionService dbs;
|
||||
private DbConnection dbs;
|
||||
private Connection db;
|
||||
|
||||
@Autowired
|
||||
private SessionManager session;
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
db = dbs.getConnection();
|
||||
session = new SessionManager();
|
||||
}
|
||||
|
||||
public LoginResponse createUser(User user) {
|
||||
|
@ -70,8 +66,10 @@ public class AuthenticationService {
|
|||
statement.execute();
|
||||
var response = new LoginResponse(
|
||||
statement.getInt(1),
|
||||
statement.getInt(6),
|
||||
statement.getInt(7)
|
||||
session.generate(
|
||||
statement.getInt(6),
|
||||
statement.getInt(7)
|
||||
)
|
||||
);
|
||||
statement.close();
|
||||
|
||||
|
@ -93,7 +91,13 @@ public class AuthenticationService {
|
|||
var salt = result.getString("Salt");
|
||||
var hash = result.getString("Password");
|
||||
if (Password.validate(salt, user.getPassword(), hash)) {
|
||||
response = new LoginResponse(0, result.getInt("ID"), result.getInt("Hue"));
|
||||
response = new LoginResponse(
|
||||
0,
|
||||
session.generate(
|
||||
result.getInt("ID"),
|
||||
result.getInt("Hue")
|
||||
)
|
||||
);
|
||||
} else {
|
||||
response = new LoginResponse(2);
|
||||
}
|
||||
|
@ -107,6 +111,8 @@ public class AuthenticationService {
|
|||
return response;
|
||||
}
|
||||
|
||||
public boolean refresh(Session key) { return session.refresh(key.getKey()); }
|
||||
|
||||
public void logout(Session key) {
|
||||
session.remove(key.getKey());
|
||||
}
|
||||
|
|
|
@ -1,40 +0,0 @@
|
|||
package achievements.services;
|
||||
|
||||
import achievements.data.Achievement;
|
||||
import achievements.data.Game;
|
||||
import achievements.data.Profile;
|
||||
import achievements.misc.DbConnectionService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.sql.Connection;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class DataService {
|
||||
|
||||
@Autowired
|
||||
private DbConnectionService dbs;
|
||||
private Connection db;
|
||||
|
||||
@Autowired
|
||||
private AuthenticationService auth;
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
db = dbs.getConnection();
|
||||
}
|
||||
|
||||
/*public List<Achievement> getUsers() {
|
||||
|
||||
}
|
||||
|
||||
public List<Game> getGames() {
|
||||
|
||||
}
|
||||
|
||||
public List<Profile> getProfiles() {
|
||||
|
||||
}*/
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package achievements.services;
|
||||
|
||||
import achievements.misc.DbConnection;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.sql.Connection;
|
||||
|
||||
@Service
|
||||
public class PlatformService {
|
||||
|
||||
@Autowired
|
||||
private DbConnection dbs;
|
||||
private Connection db;
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
db = dbs.getConnection();
|
||||
}
|
||||
}
|
193
backend/src/main/java/achievements/services/UserService.java
Normal file
193
backend/src/main/java/achievements/services/UserService.java
Normal file
|
@ -0,0 +1,193 @@
|
|||
package achievements.services;
|
||||
|
||||
import achievements.data.Profile;
|
||||
import achievements.data.query.AddPlatformRequest;
|
||||
import achievements.data.query.RemovePlatformRequest;
|
||||
import achievements.data.query.SetUsername;
|
||||
import achievements.misc.DbConnection;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
|
||||
@Autowired
|
||||
private DbConnection dbs;
|
||||
private Connection db;
|
||||
|
||||
@Autowired
|
||||
private AuthenticationService auth;
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
db = dbs.getConnection();
|
||||
}
|
||||
|
||||
public Profile getProfile(int userId) {
|
||||
try {
|
||||
var profile = (Profile) null;
|
||||
{
|
||||
var stmt = db.prepareCall("{? = call GetUserNameAndStats(?, ?, ?, ?, ?)}");
|
||||
stmt.registerOutParameter(1, Types.INTEGER);
|
||||
stmt.setInt(2, userId);
|
||||
stmt.registerOutParameter(3, Types.VARCHAR);
|
||||
stmt.registerOutParameter(4, Types.INTEGER);
|
||||
stmt.registerOutParameter(5, Types.INTEGER);
|
||||
stmt.registerOutParameter(6, Types.INTEGER);
|
||||
|
||||
stmt.execute();
|
||||
if (stmt.getInt(1) == 0) {
|
||||
profile = new Profile();
|
||||
profile.setUsername(stmt.getString(3));
|
||||
profile.setCompleted(stmt.getInt(4));
|
||||
var average = stmt.getString(5);
|
||||
profile.setPerfect(stmt.getInt(6));
|
||||
|
||||
if (average != null) {
|
||||
profile.setAverage(Integer.parseInt(average));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
var stmt = db.prepareCall("{call GetUserPlatforms(?)}");
|
||||
stmt.setInt(1, userId);
|
||||
|
||||
var results = stmt.executeQuery();
|
||||
var platforms = new ArrayList<Profile.Platform>();
|
||||
while (results.next()) {
|
||||
var platform = new Profile.Platform();
|
||||
platform.setId (results.getInt ("ID" ));
|
||||
platform.setName (results.getString ("PlatformName"));
|
||||
platform.setConnected(results.getBoolean("Connected" ));
|
||||
platforms.add(platform);
|
||||
}
|
||||
profile.setPlatforms(platforms);
|
||||
}
|
||||
|
||||
return profile;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NumberFormatException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int setUsername(int userId, SetUsername username) {
|
||||
try {
|
||||
if (auth.session().validate(userId, username.getSessionKey()) && username.getUsername().length() > 0 && username.getUsername().length() <= 32) {
|
||||
var stmt = db.prepareCall("{call SetUsername(?, ?)}");
|
||||
stmt.setInt(1, userId);
|
||||
stmt.setString(2, username.getUsername());
|
||||
|
||||
stmt.execute();
|
||||
return 0;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private static final HashMap<String, String> VALID_IMAGE_TYPES = new HashMap<>();
|
||||
static {
|
||||
VALID_IMAGE_TYPES.put("apng", "apng");
|
||||
VALID_IMAGE_TYPES.put("avif", "avif");
|
||||
VALID_IMAGE_TYPES.put("gif", "gif" );
|
||||
VALID_IMAGE_TYPES.put("jpeg", "jpg" );
|
||||
VALID_IMAGE_TYPES.put("png", "png" );
|
||||
VALID_IMAGE_TYPES.put("svg+xml", "svg" );
|
||||
VALID_IMAGE_TYPES.put("webp", "webp");
|
||||
}
|
||||
public String[] getProfileImageType(int userId) {
|
||||
try {
|
||||
var stmt = db.prepareCall("{call GetUserImage(?)}");
|
||||
stmt.setInt(1, userId);
|
||||
|
||||
var result = stmt.executeQuery();
|
||||
if (result.next()) {
|
||||
var type = result.getString("PFP");
|
||||
if (type == null) {
|
||||
return new String[] { "default", "png", "png" };
|
||||
} else {
|
||||
return new String[] { Integer.toString(userId), VALID_IMAGE_TYPES.get(type), type };
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NumberFormatException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String setProfileImageType(int userId, String sessionKey, String type) {
|
||||
try {
|
||||
if (type.matches("image/.*")) {
|
||||
type = type.substring(6);
|
||||
var extension = VALID_IMAGE_TYPES.get(type);
|
||||
if (!auth.session().validate(userId, sessionKey)) {
|
||||
return "forbidden";
|
||||
} else if (extension == null) {
|
||||
return "unsupported_type";
|
||||
} else {
|
||||
var stmt = db.prepareCall("{call SetUserImage(?, ?)}");
|
||||
stmt.setInt(1, userId);
|
||||
stmt.setString(2, type);
|
||||
|
||||
stmt.execute();
|
||||
|
||||
return extension;
|
||||
}
|
||||
} else {
|
||||
return "not_an_image";
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
public int addPlatform(int userId, AddPlatformRequest request) {
|
||||
try {
|
||||
if (auth.session().validate(userId, request.getSessionKey())) {
|
||||
var stmt = db.prepareCall("{call AddPlatform(?, ?, ?)}");
|
||||
stmt.setInt(1, userId);
|
||||
stmt.setInt(2, request.getPlatformId());
|
||||
stmt.setString(3, request.getPlatformUserId());
|
||||
|
||||
stmt.execute();
|
||||
|
||||
return 0;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public int removePlatform(int userId, RemovePlatformRequest request) {
|
||||
try {
|
||||
if (auth.session().validate(userId, request.getSessionKey())) {
|
||||
var stmt = db.prepareCall("{call RemovePlatform(?, ?)}");
|
||||
stmt.setInt(1, userId);
|
||||
stmt.setInt(2, request.getPlatformId());
|
||||
|
||||
stmt.execute();
|
||||
|
||||
return 0;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
|
@ -1,2 +1,6 @@
|
|||
server.port = 4730
|
||||
spring.application.name = Achievements Project
|
||||
spring.jackson.default-property-inclusion=always
|
||||
|
||||
server.session.cookie.secure = false
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue