Skip to content

Commit

Permalink
19 auth (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
kappsegla committed Feb 17, 2023
2 parents 92c8f3b + 8c0d1af commit b262fb5
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions src/main/java/org/fungover/haze/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand All @@ -19,9 +20,12 @@ public class Main {
public static void main(String[] args) {
Initialize initialize = new Initialize();
initialize.importCliOptions(args);

HazeDatabase hazeDatabase = new HazeDatabase();
HazeList hazeList = new HazeList();
HazeDatabase hazeDatabase = new HazeDatabase();
Auth auth = new Auth();
initializeServer(args, initialize, auth);
final boolean isPasswordSet = auth.isPasswordSet();


Thread printingHook = new Thread(() -> shutdown(hazeDatabase));
Runtime.getRuntime().addShutdownHook(printingHook);
Expand All @@ -36,18 +40,22 @@ public static void main(String[] args) {
Runnable newThread = () -> {
try {
BufferedReader input = new BufferedReader(new InputStreamReader(client.getInputStream()));

boolean clientAuthenticated = false;
while (true) {
List<String> inputList = new ArrayList<>();

String firstReading = input.readLine();
readInputStream(input, inputList, firstReading);

client.getOutputStream().write(executeCommand(hazeDatabase, inputList,hazeList ).getBytes());
clientAuthenticated = authenticateClient(auth, isPasswordSet, client, inputList, clientAuthenticated);

client.getOutputStream().write(executeCommand(hazeDatabase, inputList, hazeList).getBytes());

inputList.forEach(System.out::println); // For checking incoming message

printThreadDebug();

inputList.clear();
}

} catch (IOException e) {
Expand All @@ -73,7 +81,7 @@ private static void printThreadDebug() {
}

public static String executeCommand(HazeDatabase hazeDatabase, List<String> inputList, HazeList hazeList) {
logger.debug("executeCommand: {} {} ", ()-> hazeDatabase, ()-> inputList);
logger.debug("executeCommand: {} {} ", () -> hazeDatabase, () -> inputList);
String command = inputList.get(0).toUpperCase();

return switch (command) {
Expand All @@ -91,13 +99,14 @@ public static String executeCommand(HazeDatabase hazeDatabase, List<String> inpu
case "LLEN" -> hazeList.lLen(inputList);
case "LMOVE" -> hazeList.lMove(inputList);
case "LTRIM" -> hazeList.callLtrim(inputList);
case "AUTH" -> "+OK\r\n";
default -> "-ERR unknown command\r\n";
};
}

private static void readInputStream(BufferedReader input, List<String> inputList, String firstReading) throws
IOException {
logger.debug("readInputStream: {} {} {}", ()-> input, () -> inputList, () -> firstReading);
logger.debug("readInputStream: {} {} {}", () -> input, () -> inputList, () -> firstReading);
int size;
if (firstReading.startsWith("*")) {
size = Integer.parseInt(firstReading.substring(1)) * 2;
Expand All @@ -112,4 +121,27 @@ private static void readInputStream(BufferedReader input, List<String> inputList
}
}

private static void initializeServer(String[] args, Initialize initialize, Auth auth) {
initialize.importCliOptions(args);
auth.setPassword(initialize.getPassword());
}

private static boolean authenticateClient(Auth auth, boolean isPasswordSet, Socket client, List<String> inputList, boolean clientAuthenticated) throws IOException {
if (authCommandReceived(isPasswordSet, inputList, clientAuthenticated))
return auth.authenticate(inputList.get(1), client);

shutdownClientIfNotAuthenticated(client, clientAuthenticated, isPasswordSet);
return clientAuthenticated;
}

private static void shutdownClientIfNotAuthenticated(Socket client, boolean clientAuthenticated, boolean isPasswordSet) throws IOException {
if (!clientAuthenticated && isPasswordSet) {
client.getOutputStream().write(Auth.printAuthError());
client.shutdownOutput();
}
}

private static boolean authCommandReceived(boolean isPasswordSet, List<String> inputList, boolean clientAuthenticated) {
return isPasswordSet && !clientAuthenticated && inputList.size() == 2 && inputList.get(0).equals("AUTH");
}
}

0 comments on commit b262fb5

Please sign in to comment.