Skip to content

Commit

Permalink
Introduce UserName and Password classes (Value Objects) instead of op…
Browse files Browse the repository at this point in the history
…erating on Strings.
  • Loading branch information
TDF-PL-038 committed Jan 28, 2024
1 parent e9b9b63 commit 7164361
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 86 deletions.
57 changes: 31 additions & 26 deletions connection/src/integration-test/java/ConnectorFactoryTests.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import org.junit.jupiter.api.Test;
import org.wot.tak.common.Port;
import org.wot.tak.common.Url;
import org.wot.tak.connection.UID;
import org.wot.tak.connection.configuration.AuthenticationConfig;
import org.wot.tak.connection.configuration.ConnectorFactory;
Expand All @@ -12,6 +10,13 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.wot.tak.common.Password.noPassword;
import static org.wot.tak.common.Password.password;
import static org.wot.tak.common.Path.path;
import static org.wot.tak.common.Path.noPath;
import static org.wot.tak.common.Port.port;
import static org.wot.tak.common.Url.url;
import static org.wot.tak.common.UserName.noUserName;
import static org.wot.tak.connection.messages.ClientProtobufMessage.announcement;

class ConnectorFactoryTests {
Expand All @@ -20,15 +25,15 @@ class ConnectorFactoryTests {
void Can_create_SSL_connector_using_connector_factory() throws Exception {

var config = new ConnectorFactoryConfig(
Url.of("tak-dev.1gs20.net"),
Port.of(8089),
url("tak-dev.1gs20.net"),
port(8089),
new AuthenticationConfig(
"",
"",
"cert-test/truststore-int-ca.p12",
"atakatak",
"cert-test/charlie.p12",
"atakatak",
noUserName(),
noPassword(),
path("cert-test/truststore-int-ca.p12"),
password("atakatak"),
path("cert-test/charlie.p12"),
password("atakatak"),
true));

var connectorFactory = new ConnectorFactory(config);
Expand Down Expand Up @@ -62,15 +67,15 @@ void Can_create_SSL_connector_using_connector_factory() throws Exception {
void Can_create_TCP_connector_using_connector_factory() throws Exception {

var config = new ConnectorFactoryConfig(
Url.of("127.0.0.1"),
Port.of(8999),
url("127.0.0.1"),
port(8999),
new AuthenticationConfig(
"",
"",
"",
"",
"",
"",
noUserName(),
noPassword(),
noPath(),
noPassword(),
noPath(),
noPassword(),
true));

var connectorFactory = new ConnectorFactory(config);
Expand Down Expand Up @@ -104,15 +109,15 @@ void Can_create_TCP_connector_using_connector_factory() throws Exception {
void Can_create_UDP_connector_using_connector_factory() throws Exception {

var config = new ConnectorFactoryConfig(
Url.of("127.0.0.1"),
Port.of(8999),
url("127.0.0.1"),
port(8999),
new AuthenticationConfig(
"",
"",
"",
"",
"",
"",
noUserName(),
noPassword(),
noPath(),
noPassword(),
noPath(),
noPassword(),
true));

var connectorFactory = new ConnectorFactory(config);
Expand Down
25 changes: 25 additions & 0 deletions connection/src/main/java/org/wot/tak/common/Password.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.wot.tak.common;

public record Password(String password) {

public static Password password(String password) {
return new Password(password);
}

public static Password of(String password) {
return new Password(password);
}

public static Password noPassword() {
return new Password("");
}

public char[] asCharArray() {
return password.toCharArray();
}

@Override
public String toString() {
return password;
}
}
15 changes: 15 additions & 0 deletions connection/src/main/java/org/wot/tak/common/Path.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.wot.tak.common;

public final class Path {

private Path() {
}

public static java.nio.file.Path path(String path) {
return java.nio.file.Path.of(path);
}

public static java.nio.file.Path noPath() {
return java.nio.file.Path.of("");
}
}
24 changes: 6 additions & 18 deletions connection/src/main/java/org/wot/tak/common/Port.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
package org.wot.tak.common;

import lombok.Value;
public record Port(int number) {

@SuppressWarnings("RedundantModifiersValueLombok")
@Value
public class Port {

private final int number;

/**
* Creates TCP/UDP Port number.
* @param number port number
*/
public Port(int number) {
public Port {
if (number < 0 || number > 65535) {
throw new IllegalArgumentException("Port number must be between 0 and 65535");
}
this.number = number;
}

/**
* Creates TCP/UDP Port number.
* @param number port number
* @return Port object
*/
public static Port port(int number) {
return new Port(number);
}

public static Port of(int number) {
return new Port(number);
}
Expand Down
9 changes: 4 additions & 5 deletions connection/src/main/java/org/wot/tak/common/Url.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package org.wot.tak.common;

import lombok.Value;
public record Url(String url) {

@SuppressWarnings("RedundantModifiersValueLombok")
@Value
public class Url {
private final String url;
public static Url url(String url) {
return new Url(url);
}

public static Url of(String url) {
return new Url(url);
Expand Down
21 changes: 21 additions & 0 deletions connection/src/main/java/org/wot/tak/common/UserName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.wot.tak.common;

public record UserName(String userName) {

public static UserName userName(String userName) {
return new UserName(userName);
}

public static UserName of(String userName) {
return new UserName(userName);
}

public static UserName noUserName() {
return new UserName("");
}

@Override
public String toString() {
return userName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.wot.tak.common.Password;
import org.wot.tak.common.UserName;

import java.nio.file.Path;

@Getter(AccessLevel.PACKAGE)
@RequiredArgsConstructor
public class AuthenticationConfig {
private final String userName;
private final String userPassword;
private final String trustStorePath;
private final String trustStorePassword;
private final String keyStorePath;
private final String keyStorePassword;
private final UserName userName;
private final Password userPassword;
private final Path trustStorePath;
private final Password trustStorePassword;
private final Path keyStorePath;
private final Password keyStorePassword;
private final Boolean serverCertVerification;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.wot.tak.common.Password;
import org.wot.tak.common.Port;
import org.wot.tak.common.Url;
import org.wot.tak.common.UserName;

@Getter(AccessLevel.PUBLIC)
@RequiredArgsConstructor
Expand All @@ -13,11 +15,11 @@ public final class ConnectorFactoryConfig {
private final Port takServerPort;
private final AuthenticationConfig authenticationConfig;

public String getUserName() {
public UserName getUserName() {
return authenticationConfig.getUserName();
}

public String getUserPassword() {
public Password getUserPassword() {
return authenticationConfig.getUserPassword();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package org.wot.tak.connection.configuration;

import lombok.Value;
import org.wot.tak.common.Password;

import java.nio.file.Path;

import static org.wot.tak.common.Password.noPassword;
import static org.wot.tak.common.Path.noPath;


@SuppressWarnings("RedundantModifiersValueLombok")
Expand All @@ -9,12 +15,12 @@ public class SocketFactoryConfig {

public static SocketFactoryConfig empty() {
return new SocketFactoryConfig(
"", "", "", "", false);
noPath(), noPassword(), noPath(), noPassword(), false);
}

private String trustStorePath;
private String trustStorePassword;
private String keyStorePath;
private String keyStorePassword;
private Path trustStorePath;
private Password trustStorePassword;
private Path keyStorePath;
private Password keyStorePassword;
private Boolean serverCertVerification;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.wot.tak.connection.connectors;

import org.wot.tak.common.Password;
import org.wot.tak.common.Port;
import org.wot.tak.common.Url;
import org.wot.tak.common.UserName;
import org.wot.tak.connection.configuration.MessageReceiver;
import org.wot.tak.connection.infra.SocketFactory;
import org.wot.tak.connection.infra.TCPConnectorBase;
Expand All @@ -14,10 +16,10 @@

public final class CredentialsConnector extends TCPConnectorBase {

private final String username;
private final String password;
private final UserName username;
private final Password password;

public CredentialsConnector(Url url, Port port, SocketFactory sFactory, String username, String password) {
public CredentialsConnector(Url url, Port port, SocketFactory sFactory, UserName username, Password password) {
super(url, port, sFactory);
this.username = username;
this.password = password;
Expand All @@ -37,8 +39,8 @@ private void authenticate() throws Exception {
var auth = doc.createElement("auth");
doc.appendChild(auth);
var cot = doc.createElement("cot");
cot.setAttribute("username", username);
cot.setAttribute("password", password);
cot.setAttribute("username", username.userName());
cot.setAttribute("password", password.password());
//cot.setAttribute("uid", uid);
auth.appendChild(cot);
var transformerFactory = TransformerFactory.newInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void connect(MessageReceiver handler) throws Exception {
throw new SSLException("Cannot verify SSL socket without session");
}
var hostnameVerifier = new JsseDefaultHostnameAuthorizer(null);
if (!hostnameVerifier.verified(getUrl().getUrl(), session)) {
if (!hostnameVerifier.verified(getUrl().url(), session)) {
throw new SSLPeerUnverifiedException("Cannot verify hostname: " + getUrl());
}
super.connect(handler);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.wot.tak.connection.connectors;

import org.wot.tak.common.Password;
import org.wot.tak.common.Port;
import org.wot.tak.common.Url;
import org.wot.tak.common.UserName;
import org.wot.tak.connection.configuration.MessageReceiver;
import org.wot.tak.connection.infra.SocketFactory;

Expand All @@ -13,10 +15,10 @@

public final class SSLCredentialsConnector extends SSLConnector {

private final String username;
private final String password;
private final UserName username;
private final Password password;

public SSLCredentialsConnector(Url url, Port port, SocketFactory sFactory, String username, String password) {
public SSLCredentialsConnector(Url url, Port port, SocketFactory sFactory, UserName username, Password password) {
super(url, port, sFactory);
this.username = username;
this.password = password;
Expand All @@ -35,8 +37,8 @@ private void authenticate() throws Exception {
var auth = doc.createElement("auth");
doc.appendChild(auth);
var cot = doc.createElement("cot");
cot.setAttribute("username", username);
cot.setAttribute("password", password);
cot.setAttribute("username", username.userName());
cot.setAttribute("password", password.password());
//cot.setAttribute("uid", uid);
auth.appendChild(cot);
var transformerFactory = TransformerFactory.newInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public ProtocolVersion getProtocolVersion() {
@Override
public void connect(MessageReceiver handler) throws Exception {
this.socket = new DatagramSocket();
this.address = InetAddress.getByName(url.getUrl());
this.address = InetAddress.getByName(url.url());
this.handler = handler;
this.listener = new ResponseListener();
this.listener.start();
Expand All @@ -51,13 +51,13 @@ public void connect(MessageReceiver handler) throws Exception {
@Override
public void send(Event event) throws IOException, JAXBException {
var bytes = EventMarshalling.toBytes(event);
socket.send(new DatagramPacket(bytes, bytes.length, address, port.getNumber()));
socket.send(new DatagramPacket(bytes, bytes.length, address, port.number()));
}

@Override
public void send(Takmessage.TakMessage message) throws IOException {
var bytes = asBytesWithHeader(message.toByteArray());
socket.send(new DatagramPacket(bytes, bytes.length, address, port.getNumber()));
socket.send(new DatagramPacket(bytes, bytes.length, address, port.number()));
}

@Override
Expand Down
Loading

0 comments on commit 7164361

Please sign in to comment.