mirror of
https://github.com/zyphlar/LanternPowerMonitor.git
synced 2024-03-08 14:07:47 +00:00
Turns out we don't actually need 30MB of bloated jars to make a single HTTP post to get a Google SSO auth token. Don't need them for Firebase either. And not for Apple SSO. Shoot while we're at it, might as well get rid of pi4j too since making a JNI wrapper for PiGPio is easy enough.
This commit is contained in:
@@ -43,7 +43,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.2</version>
|
||||
<version>3.10.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
@@ -63,7 +63,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.2.1</version>
|
||||
<version>3.3.0</version>
|
||||
<configuration>
|
||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||
<filters>
|
||||
|
||||
@@ -23,14 +23,9 @@
|
||||
<version>1.2.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.pi4j</groupId>
|
||||
<artifactId>pi4j-core</artifactId>
|
||||
<version>2.2.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.pi4j</groupId>
|
||||
<artifactId>pi4j-plugin-pigpio</artifactId>
|
||||
<version>2.2.0-SNAPSHOT</version>
|
||||
<groupId>com.lanternsoftware.pigpio</groupId>
|
||||
<artifactId>lantern-pigpio</artifactId>
|
||||
<version>${pigpio.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.hypfvieh</groupId>
|
||||
@@ -63,7 +58,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.2</version>
|
||||
<version>3.10.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
@@ -83,7 +78,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.2.1</version>
|
||||
<version>3.3.0</version>
|
||||
<configuration>
|
||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||
<filters>
|
||||
|
||||
@@ -6,12 +6,9 @@ import com.lanternsoftware.datamodel.currentmonitor.Breaker;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.BreakerHub;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.BreakerPolarity;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.BreakerPower;
|
||||
import com.lanternsoftware.pigpio.PiGpioFactory;
|
||||
import com.lanternsoftware.util.CollectionUtils;
|
||||
import com.lanternsoftware.util.concurrency.ConcurrencyUtils;
|
||||
import com.pi4j.Pi4J;
|
||||
import com.pi4j.context.Context;
|
||||
import com.pi4j.io.spi.Spi;
|
||||
import com.pi4j.io.spi.SpiMode;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -28,26 +25,17 @@ public class CurrentMonitor {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(CurrentMonitor.class);
|
||||
private static final int BATCH_CNT = 4;
|
||||
private final ExecutorService executor = Executors.newCachedThreadPool();
|
||||
private Context pi4j;
|
||||
private final Map<Integer, MCP3008> chips = new HashMap<>();
|
||||
private Sampler sampler;
|
||||
private PowerListener listener;
|
||||
private boolean debug = false;
|
||||
|
||||
public void start() {
|
||||
pi4j = Pi4J.newAutoContext();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
stopMonitoring();
|
||||
ConcurrencyUtils.sleep(1000);
|
||||
executor.shutdown();
|
||||
ConcurrencyUtils.sleep(1000);
|
||||
for (MCP3008 mcp : chips.values()) {
|
||||
mcp.shutdown(pi4j);
|
||||
}
|
||||
ConcurrencyUtils.sleep(1000);
|
||||
pi4j.shutdown();
|
||||
PiGpioFactory.shutdown();
|
||||
chips.clear();
|
||||
LOG.info("Power Monitor Service Stopped");
|
||||
}
|
||||
@@ -139,9 +127,7 @@ public class CurrentMonitor {
|
||||
if (chip == null) {
|
||||
String id = "SPI" + _chip;
|
||||
LOG.info("Creating chip {}", id);
|
||||
Spi spi = pi4j.create(Spi.newConfigBuilder(pi4j).mode(SpiMode.MODE_0).id(id).name("MCP3008_" + _chip).address(_chip).baud(810000).build());
|
||||
LOG.info("is open {}", spi.isOpen());
|
||||
chip = new MCP3008(spi);
|
||||
chip = new MCP3008(PiGpioFactory.getSpiChannel(_chip, 810000, false));
|
||||
chips.put(_chip, chip);
|
||||
}
|
||||
return chip;
|
||||
|
||||
@@ -214,7 +214,6 @@ public class MonitorApp {
|
||||
if (NullUtils.isNotEmpty(config.getHost()))
|
||||
host = NullUtils.terminateWith(config.getHost(), "/");
|
||||
monitor.setDebug(config.isDebug());
|
||||
monitor.start();
|
||||
LEDFlasher.setLEDOn(false);
|
||||
if (NullUtils.isNotEmpty(config.getAuthCode()))
|
||||
authCode = config.getAuthCode();
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
package com.lanternsoftware.currentmonitor.adc;
|
||||
|
||||
import com.pi4j.context.Context;
|
||||
import com.pi4j.io.spi.Spi;
|
||||
import com.lanternsoftware.pigpio.Spi;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class MCP3008 {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(MCP3008.class);
|
||||
protected static final Logger LOG = LoggerFactory.getLogger(MCP3008.class);
|
||||
private static final byte[][] pins = new byte[8][];
|
||||
|
||||
private final Spi spi;
|
||||
@@ -22,15 +21,9 @@ public class MCP3008 {
|
||||
spi = _spi;
|
||||
}
|
||||
|
||||
public void shutdown(Context _pi4j) {
|
||||
spi.close();
|
||||
spi.shutdown(_pi4j);
|
||||
}
|
||||
|
||||
public int readPin(int _pin) {
|
||||
if (spi.transfer(pins[_pin], resp) > 2) {
|
||||
if (spi != null && spi.transfer(pins[_pin], resp) > 2)
|
||||
return ((resp[1] & 0x03) << 8) + (resp[2] & 0xFF);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,6 @@
|
||||
<configuration>
|
||||
<property name="log.pattern" value="%date %-5level %logger{0} - %message%n"/>
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>${log.pattern}</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>/opt/currentmonitor/log/log.txt</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
|
||||
|
||||
@@ -43,7 +43,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.2</version>
|
||||
<version>3.10.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
@@ -63,7 +63,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>2.4</version>
|
||||
<version>3.2.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
@@ -73,16 +73,6 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.5</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<index>true</index>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.2</version>
|
||||
<version>3.10.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
@@ -48,7 +48,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>2.4</version>
|
||||
<version>3.2.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
@@ -58,16 +58,6 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.5</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<index>true</index>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
|
||||
@@ -11,44 +11,32 @@
|
||||
<version>1.1.0</version>
|
||||
</parent>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.api-client</groupId>
|
||||
<artifactId>google-api-client-bom</artifactId>
|
||||
<version>1.33.4</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.lanternsoftware.currentmonitor</groupId>
|
||||
<artifactId>lantern-dataaccess-currentmonitor</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<version>${cm.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.lanternsoftware.util</groupId>
|
||||
<artifactId>lantern-util-servlet</artifactId>
|
||||
<version>${util.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.lanternsoftware.util</groupId>
|
||||
<artifactId>lantern-util-cloudservices</artifactId>
|
||||
<version>${util.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.lanternsoftware.util</groupId>
|
||||
<artifactId>lantern-util-http</artifactId>
|
||||
<version>${util.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.lanternsoftware.rules</groupId>
|
||||
<artifactId>lantern-service-rules</artifactId>
|
||||
<version>${rules.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.api-client</groupId>
|
||||
<artifactId>google-api-client</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax</groupId>
|
||||
<artifactId>javaee-api</artifactId>
|
||||
@@ -65,6 +53,11 @@
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>1.2.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.auth0</groupId>
|
||||
<artifactId>java-jwt</artifactId>
|
||||
<version>3.19.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.mailjet</groupId>
|
||||
<artifactId>mailjet-client</artifactId>
|
||||
@@ -81,7 +74,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.2</version>
|
||||
<version>3.10.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
@@ -100,7 +93,7 @@
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>2.5</version>
|
||||
<version>3.3.2</version>
|
||||
<configuration>
|
||||
<webResources>
|
||||
<resource>
|
||||
|
||||
@@ -6,8 +6,9 @@ import com.lanternsoftware.datamodel.currentmonitor.HubCommand;
|
||||
import com.lanternsoftware.datamodel.currentmonitor.HubCommands;
|
||||
import com.lanternsoftware.rules.RulesEngine;
|
||||
import com.lanternsoftware.util.DateUtils;
|
||||
import com.lanternsoftware.util.external.LanternFiles;
|
||||
import com.lanternsoftware.util.dao.mongo.MongoConfig;
|
||||
import com.lanternsoftware.util.external.LanternFiles;
|
||||
import com.lanternsoftware.util.http.HttpFactory;
|
||||
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
@@ -32,6 +33,7 @@ public class Globals implements ServletContextListener {
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent sce) {
|
||||
dao.shutdown();
|
||||
HttpFactory.shutdown();
|
||||
RulesEngine.shutdown();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
package com.lanternsoftware.currentmonitor.servlet;
|
||||
|
||||
import com.lanternsoftware.currentmonitor.context.Globals;
|
||||
import com.lanternsoftware.currentmonitor.util.GoogleAuthHelper;
|
||||
import com.lanternsoftware.util.DateUtils;
|
||||
import com.lanternsoftware.util.NullUtils;
|
||||
import com.lanternsoftware.util.cloudservices.apple.AppleSSO;
|
||||
import com.lanternsoftware.util.cloudservices.google.GoogleSSO;
|
||||
import com.lanternsoftware.util.dao.DaoEntity;
|
||||
import com.lanternsoftware.util.dao.DaoSerializer;
|
||||
import com.lanternsoftware.util.external.LanternFiles;
|
||||
import com.lanternsoftware.util.servlet.BasicAuth;
|
||||
import com.lanternsoftware.util.servlet.LanternServlet;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@@ -16,19 +17,27 @@ import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@WebServlet("/auth/*")
|
||||
public class AuthServlet extends LanternServlet {
|
||||
private static final Logger logger = LoggerFactory.getLogger(AuthServlet.class);
|
||||
private static final GoogleSSO googleSSO = new GoogleSSO(LanternFiles.CONFIG_PATH + "google_sso.txt");
|
||||
private static final AppleSSO appleSSO = new AppleSSO(LanternFiles.CONFIG_PATH + "apple_sso.txt");
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest _req, HttpServletResponse _rep) {
|
||||
String authCode = _req.getHeader("auth_code");
|
||||
if (NullUtils.isEmpty(authCode)) {
|
||||
String idToken = _req.getHeader("id_token");
|
||||
String email = null;
|
||||
if (NullUtils.isNotEmpty(idToken))
|
||||
email = appleSSO.getEmailFromIdToken(idToken);
|
||||
else if (NullUtils.isNotEmpty(authCode))
|
||||
authCode = Globals.dao.exchangeAuthCode(authCode, DaoSerializer.toInteger(_req.getHeader("override_account")));
|
||||
else {
|
||||
BasicAuth auth = new BasicAuth(_req);
|
||||
if (NullUtils.isEqual(auth.getUsername(), "googlesso")) {
|
||||
logger.info("Attempting google SSO");
|
||||
authCode = GoogleAuthHelper.signin(auth.getPassword(), DateUtils.fromTimeZoneId(_req.getHeader("timezone")));
|
||||
} else
|
||||
if (NullUtils.isEqual(auth.getUsername(), "googlesso"))
|
||||
email = googleSSO.signin(auth.getPassword());
|
||||
else
|
||||
authCode = Globals.dao.authenticateAccount(auth.getUsername(), auth.getPassword());
|
||||
}
|
||||
if (NullUtils.isNotEmpty(email))
|
||||
authCode = Globals.dao.getAuthCodeForEmail(email, DateUtils.fromTimeZoneId(_req.getHeader("timezone")));
|
||||
DaoEntity rep = new DaoEntity("auth_code", authCode).and("timezone", Globals.dao.getTimeZoneForAccount(authCode));
|
||||
if (isPath(_req, 0, "bin"))
|
||||
zipBsonResponse(_rep, rep);
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
package com.lanternsoftware.currentmonitor.servlet.console;
|
||||
|
||||
import com.lanternsoftware.currentmonitor.util.GoogleAuthHelper;
|
||||
import com.lanternsoftware.currentmonitor.context.Globals;
|
||||
import com.lanternsoftware.util.DateUtils;
|
||||
import com.lanternsoftware.util.NullUtils;
|
||||
import com.lanternsoftware.util.cloudservices.google.GoogleSSO;
|
||||
import com.lanternsoftware.util.external.LanternFiles;
|
||||
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.Cookie;
|
||||
@@ -10,6 +13,8 @@ import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@WebServlet("/gso")
|
||||
public class GsoServlet extends SecureConsoleServlet {
|
||||
private static final GoogleSSO googleSSO = new GoogleSSO(LanternFiles.CONFIG_PATH + "google_sso.txt");
|
||||
|
||||
@Override
|
||||
protected void get(HttpServletRequest _req, HttpServletResponse _rep) {
|
||||
render(_rep, "login.ftl", model(_req));
|
||||
@@ -19,13 +24,16 @@ public class GsoServlet extends SecureConsoleServlet {
|
||||
protected void post(HttpServletRequest _req, HttpServletResponse _rep) {
|
||||
String code = getRequestPayloadAsString(_req);
|
||||
if (NullUtils.isNotEmpty(code)) {
|
||||
String authCode = GoogleAuthHelper.signin(code, null);
|
||||
if (NullUtils.isNotEmpty(authCode)) {
|
||||
Cookie authCookie = new Cookie("auth_code", authCode);
|
||||
authCookie.setMaxAge(157680000);
|
||||
authCookie.setSecure(true);
|
||||
_rep.addCookie(authCookie);
|
||||
_req.getSession().setAttribute("auth_code", authCode);
|
||||
String email = googleSSO.signin(code);
|
||||
if (NullUtils.isNotEmpty(email)) {
|
||||
String authCode = Globals.dao.getAuthCodeForEmail(email, DateUtils.fromTimeZoneId(_req.getHeader("timezone")));
|
||||
if (NullUtils.isNotEmpty(authCode)) {
|
||||
Cookie authCookie = new Cookie("auth_code", authCode);
|
||||
authCookie.setMaxAge(157680000);
|
||||
authCookie.setSecure(true);
|
||||
_rep.addCookie(authCookie);
|
||||
_req.getSession().setAttribute("auth_code", authCode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
package com.lanternsoftware.currentmonitor.util;
|
||||
|
||||
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest;
|
||||
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
|
||||
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
|
||||
import com.google.api.client.http.javanet.NetHttpTransport;
|
||||
import com.google.api.client.json.gson.GsonFactory;
|
||||
import com.lanternsoftware.currentmonitor.context.Globals;
|
||||
import com.lanternsoftware.util.ResourceLoader;
|
||||
import com.lanternsoftware.util.dao.DaoEntity;
|
||||
import com.lanternsoftware.util.dao.DaoSerializer;
|
||||
import com.lanternsoftware.util.external.LanternFiles;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class GoogleAuthHelper {
|
||||
private static final Logger logger = LoggerFactory.getLogger(GoogleAuthHelper.class);
|
||||
private static final NetHttpTransport transport = new NetHttpTransport();
|
||||
private static final String googleClientId;
|
||||
private static final String googleClientSecret;
|
||||
static {
|
||||
DaoEntity google = DaoSerializer.parse(ResourceLoader.loadFileAsString(LanternFiles.CONFIG_PATH + "google_sso.txt"));
|
||||
googleClientId = DaoSerializer.getString(google, "id");
|
||||
googleClientSecret = DaoSerializer.getString(google, "secret");
|
||||
}
|
||||
|
||||
public static String signin(String _code, TimeZone _tz) {
|
||||
try {
|
||||
GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(transport, new GsonFactory(), "https://oauth2.googleapis.com/token", googleClientId, googleClientSecret, _code, "https://lanternsoftware.com/console").execute();
|
||||
if (tokenResponse != null) {
|
||||
GoogleIdToken idToken = tokenResponse.parseIdToken();
|
||||
if (idToken != null)
|
||||
return Globals.dao.getAuthCodeForEmail(idToken.getPayload().getEmail(), _tz);
|
||||
}
|
||||
} catch (Exception _e) {
|
||||
logger.error("Failed to validate google auth code", _e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user