Added gitignore; Common module created; TTResponse created; TTLogger created; Auth RMI created;

This commit is contained in:
Lorenzo Iovino 2017-06-15 23:38:01 +02:00
parent ac694a1776
commit f18d7c4bb4
90 changed files with 844 additions and 2105 deletions

View file

@ -7,5 +7,7 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="com.github.cliftonlabs:json-simple:2.1.2" level="project" />
<orderEntry type="module" module-name="Commons" />
</component>
</module>

View file

@ -1,8 +1,18 @@
package com.texttwist.server;
import com.texttwist.server.components.TTServer;
import utilities.TTLogger;
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
public static void main(String[] args) throws IOException {
System.out.println("Server start");
TTLogger logger = new TTLogger(new File("./log"));
TTServer ttServer = new TTServer();
ttServer.start();
}
}

View file

@ -0,0 +1,34 @@
package com.texttwist.server.components;
import com.texttwist.server.components.auth.TTAuth;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
/**
* Created by loke on 15/06/2017.
*/
public class TTServer {
private static final int PORT = 9999;
public void start() {
try {
TTAuth auth = new TTAuth();
Registry registry = LocateRegistry.createRegistry(PORT);
registry.bind("auth", auth);
System.out.println("Auth Service running at "+PORT+" port...");
} catch (RemoteException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (AlreadyBoundException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,52 @@
package com.texttwist.server.components.auth;
import interfaces.ITTAuth;
import models.TTResponse;
import org.json.simple.JsonObject;
import utilities.TTLogger;
import java.math.BigInteger;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.security.SecureRandom;
/**
* Created by loke on 15/06/2017.
*/
public class TTAuth extends UnicastRemoteObject implements ITTAuth {
private SecureRandom random = new SecureRandom();
public String nextSessionId() {
return new BigInteger(130, random).toString(32);
}
public TTAuth() throws RemoteException{
}
@Override
public TTResponse login(String userName, String password) throws RemoteException {
TTLogger.write("LOGGER: Invoked login with username=" + userName + " AND " + " password=" + password);
if ((userName != null && !userName.isEmpty())
&& (password != null && !password.equals(""))) {
if((userName.equalsIgnoreCase("admin"))
&& (password.equals("admin"))) {
JsonObject data = new JsonObject();
data.put("token", nextSessionId());
System.out.println("LOGGER: Login successfull");
return new TTResponse("Login successfull", 200, data);
}
}
System.out.println("LOGGER: Login unsuccessfull");
return new TTResponse("Login unsuccessfull", 400, null);
}
@Override
public TTResponse logout(String userName, String token) throws RemoteException {
System.out.println("LOGGER: Invoked logout with username=" + userName + " AND " + " token=" + token);
if ((userName != null && !userName.isEmpty())
&& (token != null && !token.isEmpty())) {
return new TTResponse("Logout successfull", 200, null);
}
return new TTResponse("Logout successfull (but something go wrong)", 200, null);
}
}