login
~1h work
This commit is contained in:
parent
a73ce3037f
commit
88f996f5e0
52
src/at/nanopenguin/mtcg/application/SessionHandler.java
Normal file
52
src/at/nanopenguin/mtcg/application/SessionHandler.java
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
package at.nanopenguin.mtcg.application;
|
||||||
|
|
||||||
|
import at.nanopenguin.mtcg.application.service.schemas.UserCredentials;
|
||||||
|
import at.nanopenguin.mtcg.db.DbQuery;
|
||||||
|
import at.nanopenguin.mtcg.db.SqlCommand;
|
||||||
|
import at.nanopenguin.mtcg.db.Table;
|
||||||
|
import lombok.val;
|
||||||
|
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
public final class SessionHandler {
|
||||||
|
private static SessionHandler INSTANCE;
|
||||||
|
private final Map<UUID, Integer> Sessions = new HashMap<>();
|
||||||
|
|
||||||
|
private SessionHandler() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SessionHandler getInstance() {
|
||||||
|
if (INSTANCE == null) {
|
||||||
|
INSTANCE = new SessionHandler();
|
||||||
|
}
|
||||||
|
|
||||||
|
return INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UUID login(UserCredentials userCredentials) throws SQLException {
|
||||||
|
val result = DbQuery.builder()
|
||||||
|
.command(SqlCommand.SELECT)
|
||||||
|
.table(Table.USERS)
|
||||||
|
.column("id")
|
||||||
|
.column("password")
|
||||||
|
.condition("username", userCredentials.username())
|
||||||
|
.executeQuery();
|
||||||
|
if (result.isEmpty()) {
|
||||||
|
// user not found
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (!result.get(0).get("password").equals(userCredentials.password())) {
|
||||||
|
// wrong password
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
UUID uuid = UUID.randomUUID();
|
||||||
|
this.Sessions.put(uuid, (Integer) result.get(0).get("id"));
|
||||||
|
return uuid;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,5 +1,6 @@
|
|||||||
package at.nanopenguin.mtcg.application.service;
|
package at.nanopenguin.mtcg.application.service;
|
||||||
|
|
||||||
|
import at.nanopenguin.mtcg.application.SessionHandler;
|
||||||
import at.nanopenguin.mtcg.application.User;
|
import at.nanopenguin.mtcg.application.User;
|
||||||
import at.nanopenguin.mtcg.application.service.schemas.UserCredentials;
|
import at.nanopenguin.mtcg.application.service.schemas.UserCredentials;
|
||||||
import at.nanopenguin.mtcg.http.HttpMethod;
|
import at.nanopenguin.mtcg.http.HttpMethod;
|
||||||
@ -10,6 +11,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
|||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
public class UserService implements Service {
|
public class UserService implements Service {
|
||||||
|
|
||||||
@ -17,14 +19,17 @@ public class UserService implements Service {
|
|||||||
public Response handleRequest(HttpRequest request) throws JsonProcessingException {
|
public Response handleRequest(HttpRequest request) throws JsonProcessingException {
|
||||||
try {
|
try {
|
||||||
if (request.getPath().split("/")[1].equals("sessions") && request.getMethod() == HttpMethod.POST) {
|
if (request.getPath().split("/")[1].equals("sessions") && request.getMethod() == HttpMethod.POST) {
|
||||||
// response = login()
|
// login
|
||||||
return new Response(HttpStatus.NOT_IMPLEMENTED); // new ObjectMapper().readValue(request.getBody(), UserCredentials.class);
|
UUID uuid = SessionHandler.getInstance().login(new ObjectMapper().readValue(request.getBody(), UserCredentials.class));
|
||||||
|
return uuid != null ?
|
||||||
|
new Response(HttpStatus.OK, "application/json", uuid.toString()) :
|
||||||
|
new Response(HttpStatus.UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.getPath().split("/")[1].equals("users")) {
|
if (request.getPath().split("/")[1].equals("users")) {
|
||||||
return switch (request.getMethod()) {
|
return switch (request.getMethod()) {
|
||||||
case GET -> new Response(HttpStatus.NOT_IMPLEMENTED);
|
case GET -> new Response(HttpStatus.NOT_IMPLEMENTED);
|
||||||
case POST -> {
|
case POST -> { // register new user
|
||||||
int success = User.create(new ObjectMapper().readValue(request.getBody(), UserCredentials.class));
|
int success = User.create(new ObjectMapper().readValue(request.getBody(), UserCredentials.class));
|
||||||
yield new Response(success > 0 ? HttpStatus.CREATED : HttpStatus.CONFLICT);
|
yield new Response(success > 0 ? HttpStatus.CREATED : HttpStatus.CONFLICT);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,7 +25,7 @@ public final class DbQuery {
|
|||||||
private SortedMap<String, Object> conditions;
|
private SortedMap<String, Object> conditions;
|
||||||
|
|
||||||
public static class DbQueryBuilder {
|
public static class DbQueryBuilder {
|
||||||
public ResultSet executeQuery() throws SQLException {
|
public List<Map<String, Object>> executeQuery() throws SQLException {
|
||||||
DbQuery dbQuery = this.build();
|
DbQuery dbQuery = this.build();
|
||||||
if (dbQuery.command != SqlCommand.SELECT) throw new SQLException();
|
if (dbQuery.command != SqlCommand.SELECT) throw new SQLException();
|
||||||
return dbQuery.read();
|
return dbQuery.read();
|
||||||
@ -75,7 +75,7 @@ public final class DbQuery {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ResultSet read() throws SQLException {
|
private List<Map<String, Object>> read() throws SQLException {
|
||||||
try (Connection connection = connect()) {
|
try (Connection connection = connect()) {
|
||||||
StringJoiner columnJoiner = new StringJoiner(", ");
|
StringJoiner columnJoiner = new StringJoiner(", ");
|
||||||
if (this.columns.isEmpty()) {
|
if (this.columns.isEmpty()) {
|
||||||
@ -92,7 +92,18 @@ public final class DbQuery {
|
|||||||
preparedStatement.setObject(i++, entry.getValue());
|
preparedStatement.setObject(i++, entry.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
return preparedStatement.executeQuery();
|
ResultSet resultSet = preparedStatement.executeQuery();
|
||||||
|
|
||||||
|
List<Map<String, Object>> result = new ArrayList<>();
|
||||||
|
while (resultSet.next()) {
|
||||||
|
Map<String, Object> row = new HashMap<>();
|
||||||
|
for (i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
|
||||||
|
row.put(resultSet.getMetaData().getColumnName(i), resultSet.getObject(i));
|
||||||
|
}
|
||||||
|
result.add(row);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user