Moved OAuth to the external http daemon and added OAuth2 store
This commit is contained in:
parent
20228d9e79
commit
28ff89d07f
8 changed files with 114 additions and 53 deletions
|
|
@ -39,14 +39,11 @@ 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/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 CONFIG_CLIENT_ID = "hal_assistant.google.client_id";
|
||||
private static final String ENDPOINT_SMARTHOME = "api/assistant/google/smarthome";
|
||||
protected static final String CONFIG_CLIENT_ID = "hal_assistant.google.client_id";
|
||||
|
||||
private SmartHomeImpl smartHome;
|
||||
private OAuth2Registry oAuth2Registry;
|
||||
|
||||
|
||||
@Override
|
||||
public void initiate(ScheduledExecutorService executor) {
|
||||
|
|
@ -58,13 +55,9 @@ public class SmartHomeDaemon implements HalDaemon {
|
|||
|
||||
smartHome = new SmartHomeImpl();
|
||||
|
||||
oAuth2Registry = new OAuth2Registry();
|
||||
oAuth2Registry.addWhitelist(HalContext.getStringProperty(CONFIG_CLIENT_ID));
|
||||
oAuth2Registry.setTokenListener(smartHome);
|
||||
|
||||
HalServer.registerExternalPage(ENDPOINT_AUTH, new OAuth2AuthorizationPage(oAuth2Registry));
|
||||
HalServer.registerExternalPage(ENDPOINT_TOKEN, new OAuth2TokenPage(oAuth2Registry));
|
||||
HalServer.registerExternalPage(ENDPOINT_SMARTHOME, new SmartHomePage(smartHome));
|
||||
HalServer.getExternalWebDaemon().getOAuth2Registry().addWhitelist(HalContext.getStringProperty(CONFIG_CLIENT_ID));
|
||||
HalServer.getExternalWebDaemon().getOAuth2Registry().addTokenListener(smartHome);
|
||||
HalServer.getExternalWebDaemon().registerPage(ENDPOINT_SMARTHOME, new SmartHomePage(smartHome));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ import se.hal.struct.Event;
|
|||
import se.hal.struct.Sensor;
|
||||
import zutil.db.DBConnection;
|
||||
import zutil.log.LogUtil;
|
||||
import zutil.net.http.page.oauth.OAuth2Registry.TokenRegistrationListener;
|
||||
import zutil.net.http.page.oauth.OAuth2Registry.OAuth2TokenRegistrationListener;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
|
|
@ -42,14 +42,14 @@ import java.util.logging.Level;
|
|||
import java.util.logging.Logger;
|
||||
|
||||
|
||||
public class SmartHomeImpl extends SmartHomeApp implements TokenRegistrationListener {
|
||||
public class SmartHomeImpl extends SmartHomeApp implements OAuth2TokenRegistrationListener {
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
|
||||
public static final String CONFIG_USER_AGENT = "hal_assistant_google.user_agent";
|
||||
public static final String CONFIG_TOKEN = "hal_assistant_google.token";
|
||||
public static final String CONFIG_TOKEN_TIMEOUT = "hal_assistant_google.token_timeout";
|
||||
|
||||
|
||||
private final String userAgent;
|
||||
private final String clientId;
|
||||
|
||||
|
||||
public SmartHomeImpl() {
|
||||
|
|
@ -57,28 +57,22 @@ public class SmartHomeImpl extends SmartHomeApp implements TokenRegistrationList
|
|||
HalContext.setProperty(CONFIG_USER_AGENT, "Hal-" + (int) (Math.random() * 10000));
|
||||
userAgent = HalContext.getStringProperty(CONFIG_USER_AGENT);
|
||||
|
||||
if (HalContext.containsProperty(CONFIG_TOKEN)) {
|
||||
// Restore previous token
|
||||
onTokenRegistration(
|
||||
null,
|
||||
HalContext.getStringProperty(CONFIG_TOKEN),
|
||||
HalContext.getLongProperty(CONFIG_TOKEN_TIMEOUT));
|
||||
}
|
||||
clientId = HalContext.getStringProperty(SmartHomeDaemon.CONFIG_CLIENT_ID);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onTokenRegistration(String clientId, String token, long timeoutMillis) {
|
||||
if (this.clientId == null || !this.clientId.equals(clientId))
|
||||
return; // Only assign the token for the Google client ID
|
||||
|
||||
try {
|
||||
GoogleCredentials credentials = GoogleCredentials.create(new AccessToken(
|
||||
token,
|
||||
new Date(System.currentTimeMillis() + timeoutMillis)
|
||||
new Date(timeoutMillis)
|
||||
));
|
||||
this.setCredentials(credentials);
|
||||
logger.fine("New OAuth2 token registered.");
|
||||
|
||||
HalContext.setProperty(CONFIG_TOKEN, token);
|
||||
HalContext.setProperty(CONFIG_TOKEN_TIMEOUT, String.valueOf(timeoutMillis));
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, "Could not load google credentials", e);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue