Skip to content
Snippets Groups Projects
Commit 44fabfd1 authored by Felix Sidokhine's avatar Felix Sidokhine
Browse files

realized the nameserver

parent 485ba4e1
No related branches found
No related tags found
No related merge requests found
Showing
with 150 additions and 18 deletions
package net.jami.jams.authmodule;
import lombok.extern.slf4j.Slf4j;
import net.jami.jams.common.jami.NameRegistrationRequest;
import net.jami.jams.common.jami.NameServer;
import net.jami.jams.common.objects.roots.X509Fields;
import net.jami.jams.common.objects.user.User;
......@@ -29,7 +30,10 @@ public class RegisterUserFlow {
//Didn't exactly plan on this happening here, but this is the only place we actually need it.
//Given an interface of NameServer, we need to enroll the user or decline the enrollement before
//storing him
if(nameServer != null && nameServer.registerName(user.getUsername() ) != 200){
NameRegistrationRequest nameRegistrationRequest = new NameRegistrationRequest();
nameRegistrationRequest.setOwner(ethKeyPair[0]);
nameRegistrationRequest.setAddr(user.getJamiId());
if(nameServer != null && nameServer.registerName(user.getUsername(), nameRegistrationRequest ) != 200){
return false;
}
datastore.getUserDao().storeObject(user);
......
......@@ -22,6 +22,7 @@ module jams.common {
opens net.jami.jams.common.cryptoengineapi;
opens net.jami.jams.common.utils;
opens net.jami.jams.common.serialization;
opens net.jami.jams.common.jami;
requires lombok;
requires org.slf4j;
requires org.bouncycastle.pkix;
......
package net.jami.jams.common.jami;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class AddrLookupResponse {
private String name;
}
package net.jami.jams.common.jami;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class NameLookupResponse {
private String name;
private String addr;
private String publickey;
private String signature;
}
package net.jami.jams.common.jami;
public interface NameServer {
Integer registerName(String username);
String getAddressFromName(String username);
Integer registerName(String username, NameRegistrationRequest nameRegistrationRequest);
NameLookupResponse getAddressFromName(String username);
String getNameFromAddress(String address);
}
......@@ -6,6 +6,7 @@ import net.jami.datastore.main.DataStore;
import net.jami.jams.common.authentication.AuthenticationSourceType;
import net.jami.jams.common.authmodule.AuthenticationModule;
import net.jami.jams.common.cryptoengineapi.CertificateAuthority;
import net.jami.jams.common.jami.NameServer;
import net.jami.jams.common.serialization.JsoniterRegistry;
import net.jami.jams.common.server.ServerSettings;
import net.jami.jams.common.utils.LibraryLoader;
......@@ -31,6 +32,7 @@ public class Server {
//This one gets loaded via JAR, to make it more flexible.
public static CertificateAuthority certificateAuthority;
public static AuthenticationModule userAuthenticationModule;
public static NameServer nameServer;
public static void main(String[] args) {
//Start tomcat.
......@@ -61,6 +63,7 @@ public class Server {
userAuthenticationModule.attachAuthSource(AuthenticationSourceType.AD,
serverSettings.getActiveDirectoryConfiguration());
}
//init the nameserver here.
}
catch (Exception e){
log.error("Could not load configuration file or initialize some components - this is critical");
......
......@@ -3,6 +3,8 @@ package net.jami.jams.server.core.jaminamserver;
import lombok.extern.slf4j.Slf4j;
import net.jami.jams.common.dao.StatementElement;
import net.jami.jams.common.dao.StatementList;
import net.jami.jams.common.jami.NameLookupResponse;
import net.jami.jams.common.jami.NameRegistrationRequest;
import net.jami.jams.common.jami.NameServer;
import net.jami.jams.common.objects.user.User;
......@@ -14,19 +16,24 @@ import static net.jami.jams.server.Server.dataStore;
public class LocalNameServer implements NameServer {
//This always returns 200, for obvious reasons.
@Override
public Integer registerName(String username) {
public Integer registerName(String username, NameRegistrationRequest nameRegistrationRequest) {
return 200;
}
@Override
public String getAddressFromName(String username) {
public NameLookupResponse getAddressFromName(String username) {
StatementList statementList = new StatementList();
StatementElement statementElement = new StatementElement("username","=",username,"");
statementList.addStatement(statementElement);
List<User> results = dataStore.getUserDao().getObjects(statementList);
if(results.size() == 0) return null;
return results.get(0).getAddress();
NameLookupResponse nameLookupResponse = new NameLookupResponse();
nameLookupResponse.setName(results.get(0).getUsername());
nameLookupResponse.setAddr(results.get(0).getJamiId());
return nameLookupResponse;
}
@Override
......
package net.jami.jams.server.core.jaminamserver;
import com.jsoniter.JsonIterator;
import com.jsoniter.output.JsonStream;
import lombok.extern.slf4j.Slf4j;
import net.jami.jams.common.jami.NameLookupResponse;
import net.jami.jams.common.jami.NameRegistrationRequest;
import net.jami.jams.common.jami.NameServer;
import java.net.HttpURLConnection;
......@@ -16,14 +20,13 @@ public class PublicNameServer implements NameServer {
}
@Override
public Integer registerName(String username) {
public Integer registerName(String username, NameRegistrationRequest nameRegistrationRequest) {
try {
URL url = new URL(nameserverURI+"/");
URL url = new URL(nameserverURI+"/name/" + username);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.connect();
//Send the name registration request.
con.getOutputStream().write("".getBytes());
con.setDoOutput(true);
con.getOutputStream().write(JsonStream.serialize(nameRegistrationRequest).getBytes());
return con.getResponseCode();
}
catch (Exception e) {
......@@ -32,28 +35,52 @@ public class PublicNameServer implements NameServer {
}
@Override
public String getAddressFromName(String usernmae) {
public NameLookupResponse getAddressFromName(String username) {
try {
URL url = new URL(nameserverURI+"/");
URL url = new URL(nameserverURI+"/name/" + username);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
if(con.getResponseCode() == 200) {
StringBuilder responseData = new StringBuilder();
int respSize = Integer.parseInt(con.getHeaderField("Content-Length"));
int currentSize = 0;
while (currentSize < respSize) {
responseData.append((char) con.getInputStream().read());
currentSize++;
}
log.info("Reponse received from public nameserver {} ", responseData.toString());
return JsonIterator.deserialize(responseData.toString(),NameLookupResponse.class);
}
return null;
}
catch (Exception e){
log.info("An error occurred while querying the public nameserver {} ", e.toString());
return null;
}
return null;
}
@Override
public String getNameFromAddress(String address) {
try {
URL url = new URL(nameserverURI+"/");
URL url = new URL(nameserverURI+"/addr/" + address);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
if(con.getResponseCode() == 200) {
StringBuilder responseData = new StringBuilder();
int respSize = Integer.parseInt(con.getHeaderField("Content-Length"));
int currentSize = 0;
while (currentSize < respSize) {
responseData.append((char) con.getInputStream().read());
currentSize++;
}
log.info("Response received from public nameserver {} ",responseData.toString());
return responseData.toString();
}
return null;
}
catch (Exception e){
log.info("An error occurred while querying the public nameserver {} ",e.toString());
return null;
}
return null;
}
}
package net.jami.jams.server.servlets.api.jaminameserver;
import com.jsoniter.output.JsonStream;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.jami.jams.common.jami.AddrLookupResponse;
import net.jami.jams.common.jami.NameLookupResponse;
import java.io.IOException;
import static net.jami.jams.server.Server.nameServer;
@WebServlet("/api/nameservice/addr/*")
public class AddressServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
String[] path = req.getServletPath().split("/");
String username = nameServer.getNameFromAddress(path[path.length - 1]);
if(username == null) resp.sendError(404);
else resp.getOutputStream().write(JsonStream.serialize(new AddrLookupResponse(username)).getBytes());
}
}
package net.jami.jams.server.servlets.api.jaminameserver;
import com.jsoniter.output.JsonStream;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.jami.jams.common.jami.NameLookupResponse;
import java.io.IOException;
import static net.jami.jams.server.Server.nameServer;
@WebServlet("/api/nameservice/name/*")
public class NameServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
String[] path = req.getServletPath().split("/");
NameLookupResponse nameLookupResponse = nameServer.getAddressFromName(path[path.length - 1]);
if(nameLookupResponse == null) resp.sendError(404);
else resp.getOutputStream().write(JsonStream.serialize(nameLookupResponse).getBytes());
}
}
package net.jami.jams.server.core.jaminamserver;
import net.jami.jams.common.jami.NameLookupResponse;
import net.jami.jams.common.jami.NameRegistrationRequest;
import net.jami.jams.common.jami.NameServer;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.*;
class PublicNameServerTest {
private static NameServer nameServer;
@BeforeAll
public static void init(){
nameServer = new PublicNameServer("http://ns.jami.net");
}
@Test
public void testNameRegistration(){
NameRegistrationRequest nameRegistrationRequest = new NameRegistrationRequest();
nameRegistrationRequest.setAddr("0e76529938772ac7626552ab71");
nameRegistrationRequest.setOwner("0x74ef2cD532a0f3cE31089b4fd296DDdFB5d6566A");
nameRegistrationRequest.setSignature(null);
nameRegistrationRequest.setPublickey(null);
String name = UUID.randomUUID().toString().replace("-","");
Integer res = nameServer.registerName(name,nameRegistrationRequest);
Assertions.assertEquals(200,res,"The response should have been 200!");
}
@Test
public void testNameLookUp(){
NameLookupResponse resp = nameServer.getAddressFromName("sidokhine6");
Assertions.assertNotNull(resp,"The response should exist!");
}
@Test
public void testAddrLookUp(){
String addr = nameServer.getNameFromAddress("0d1f0002ce728d6aa8b98b5227c75fc773735f9e");
Assertions.assertNotNull(addr,"The address should exist!");
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment