Now using Zutil Oauth code properly

This commit is contained in:
Ziver Koc 2020-11-27 02:18:59 +01:00
parent 03ae4329bc
commit ccc99033cd
4 changed files with 23 additions and 14 deletions

View file

@ -12,4 +12,4 @@ se.hal.level = ALL
zutil.level = ALL
zutil.db.bean.level = INFO
zutil.net.http.page.level = INFO
zutil.net.http.page.HttpFilePage.level = INFO

View file

@ -33,7 +33,6 @@ import zutil.net.http.page.oauth.OAuth2AuthorizationPage;
import zutil.net.http.page.oauth.OAuth2Registry;
import zutil.net.http.page.oauth.OAuth2TokenPage;
import java.util.Date;
import java.util.concurrent.ScheduledExecutorService;
import java.util.logging.Logger;
@ -41,8 +40,9 @@ import java.util.logging.Logger;
public class SmartHomeDaemon implements HalDaemon {
private static final Logger logger = LogUtil.getLogger();
public static final String ENDPOINT_AUTH = "api/assistant/google/auth/token";
public static final String ENDPOINT_TOKEN = "api/assistant/google/auth/authorize";
public static final String ENDPOINT_AUTH = "api/assistant/google/auth/authorize";
public static final String ENDPOINT_TOKEN = "api/assistant/google/auth/token";
public static final String ENDPOINT_SMARTHOME = "api/assistant/google/smarthome";
private static final String PARAM_PORT = "assistant.google.port";
private static final String PARAM_CLIENT_ID = "assistant.google.client_id";
@ -60,15 +60,16 @@ public class SmartHomeDaemon implements HalDaemon {
return;
}
smartHome = new SmartHomeImpl("token", new Date(System.currentTimeMillis() + 24*60*60*1000));
smartHome = new SmartHomeImpl();
oAuth2Registry = new OAuth2Registry();
oAuth2Registry.addWhitelist(HalContext.getStringProperty(PARAM_CLIENT_ID));
oAuth2Registry.setTokenListener(smartHome);
httpServer = new HttpServer(HalContext.getIntegerProperty(PARAM_PORT));
httpServer.setPage(ENDPOINT_AUTH, new OAuth2AuthorizationPage(oAuth2Registry));
httpServer.setPage(ENDPOINT_TOKEN, new OAuth2TokenPage(oAuth2Registry));
httpServer.setPage(SmartHomePage.ENDPOINT_URL, new SmartHomePage(smartHome));
httpServer.setPage(ENDPOINT_SMARTHOME, new SmartHomePage(smartHome));
httpServer.start();
}
}

View file

@ -17,26 +17,35 @@
package se.hal.plugin.assistant.google;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.google.actions.api.smarthome.*;
import com.google.auth.oauth2.AccessToken;
import com.google.auth.oauth2.GoogleCredentials;
import zutil.log.LogUtil;
import zutil.net.http.page.oauth.OAuth2Registry.TokenRegistrationListener;
public class SmartHomeImpl extends SmartHomeApp {
public class SmartHomeImpl extends SmartHomeApp implements TokenRegistrationListener {
private static final Logger logger = LogUtil.getLogger();
public SmartHomeImpl(String accessToken, Date accessExpirationTime) {
/* try {
GoogleCredentials credentials = GoogleCredentials.create(new AccessToken(accessToken, accessExpirationTime));
public SmartHomeImpl() { }
@Override
public void onTokenRegistration(String clientId, String token, long timeoutMillis) {
try {
GoogleCredentials credentials = GoogleCredentials.create(new AccessToken(
token,
new Date(System.currentTimeMillis() + timeoutMillis)
));
this.setCredentials(credentials);
logger.fine("New OAuth2 token registered.");
} catch (Exception e) {
logger.severe("Could not load google credentials");
throw new RuntimeException(e);
}*/
logger.log(Level.SEVERE, "Could not load google credentials", e);
}
}

View file

@ -61,7 +61,6 @@ import zutil.net.http.HttpPrintStream;
*/
public class SmartHomePage implements HttpPage {
private static final Logger logger = LogUtil.getLogger();
public static final String ENDPOINT_URL = "api/assistant/google/smarthome";
private SmartHomeImpl smartHome;