Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add auth, auth tests #4

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>com.bigboxer23</groupId>
<artifactId>utils</artifactId>
<version>2.0.35</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.17.0</version>
</dependency>

</dependencies>
<repositories>
<repository>
Expand Down
71 changes: 71 additions & 0 deletions src/main/java/com/bigboxer23/eco_net/EcoNetAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.bigboxer23.eco_net;

import com.bigboxer23.eco_net.data.EcoNetLoginData;
import com.bigboxer23.utils.http.OkHttpUtil;
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** */
public class EcoNetAPI {
private static final Logger logger = LoggerFactory.getLogger(EcoNetAPI.class);
protected static final String baseUrl = "https://rheem.clearblade.com/api/v/1/";

private static EcoNetAPI instance;

private final String userToken;
private final String accountId;

private static final String CLEAR_BLADE_SYSTEM_KEY = "e2e699cb0bb0bbb88fc8858cb5a401";
private static final String CLEAR_BLADE_SYSTEM_SECRET = "E2E699CB0BE6C6FADDB1B0BC9A20";

private EcoNetAPI(String accountId, String userToken) {
this.accountId = accountId;
this.userToken = userToken;
}

public static EcoNetAPI getInstance(String email, String password) {
if (StringUtils.isBlank(email) || StringUtils.isBlank(password)) {
logger.error("need to define email and password values.");
throw new RuntimeException("need to define email and password values.");
}

return Optional.ofNullable(instance).orElseGet(() -> {
instance = getAccountIDAndToken(email, password)
.map(data -> new EcoNetAPI(data.getOptions().getAccountId(), data.getUserToken()))
.orElse(null);
return instance;
});
}

private static Optional<EcoNetLoginData> getAccountIDAndToken(String email, String password) {
try (Response response = OkHttpUtil.postSynchronous(
baseUrl + "user/auth",
RequestBody.create(URLDecoder.decode(
"{\"email\": \"" + email + "\", \"password\": \"" + password + "\"}",
StandardCharsets.UTF_8.displayName())
.getBytes(StandardCharsets.UTF_8)),
(builder) -> {
builder.addHeader("ClearBlade-SystemKey", CLEAR_BLADE_SYSTEM_KEY)
.addHeader("ClearBlade-SystemSecret", CLEAR_BLADE_SYSTEM_SECRET)
.addHeader("Content-Type", "application/json; charset=UTF-8");
return builder;
})) {
Optional<EcoNetLoginData> body = OkHttpUtil.getBody(response, EcoNetLoginData.class);
if (body.isPresent() && !body.get().getOptions().isSuccess()) {
logger.error("getAccountIDAndToken: " + body.get().getOptions().getMessage());
return Optional.empty();
}
return body;
} catch (IOException e) {
logger.error("getAccountIDAndToken", e);
return Optional.empty();
}
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/bigboxer23/eco_net/data/EcoNetLoginData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.bigboxer23.eco_net.data;

import com.squareup.moshi.Json;
import lombok.Data;

/** */
@Data
public class EcoNetLoginData {
@Json(name = "user_token")
private String userToken;

private EcoNetLoginOptionsData options;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.bigboxer23.eco_net.data;

import com.squareup.moshi.Json;
import lombok.Data;

/** */
@Data
public class EcoNetLoginOptionsData {
@Json(name = "account_id")
private String accountId;

private boolean success;

private String message;
}
4 changes: 0 additions & 4 deletions src/main/java/com/bigboxer23/switch_bot/EcoNetAPI.java

This file was deleted.

37 changes: 37 additions & 0 deletions src/test/java/com/bigboxer23/eco_net/EcoNetApiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.bigboxer23.eco_net;

import static org.junit.jupiter.api.Assertions.*;

import com.bigboxer23.utils.properties.PropertyUtils;
import java.io.IOException;
import org.junit.jupiter.api.Test;

/** Need to define environment variables for econet_email/econet_password to run tests */
public class EcoNetApiTest {
private static final String email = PropertyUtils.getProperty("econet_email");

private static final String password = PropertyUtils.getProperty("econet_password");

private static final EcoNetAPI instance = EcoNetAPI.getInstance(email, password);

@Test
public void testAuth() {
testInvalidLogins(null, null);
testInvalidLogins("", null);
testInvalidLogins("", "");
testInvalidLogins(null, "");
testInvalidLogins(email, "blah");
testInvalidLogins("invalid", password);
}

private void testInvalidLogins(String email, String password) {
try {
EcoNetAPI api = EcoNetAPI.getInstance(email, password);
if (api != null) {
fail();
}
} catch (RuntimeException e) {

}
}
}