use of org.openhab.binding.tesla.internal.protocol.sso.TokenResponse in project openhab-addons by openhab.
the class TeslaAccountHandler method authenticate.
private ThingStatusInfo authenticate() {
TokenResponse token = logonToken;
boolean hasExpired = true;
if (token != null) {
Instant tokenCreationInstant = Instant.ofEpochMilli(token.created_at * 1000);
logger.debug("Found a request token created at {}", dateFormatter.format(tokenCreationInstant));
Instant tokenExpiresInstant = Instant.ofEpochMilli(token.created_at * 1000 + 60 * token.expires_in);
if (tokenExpiresInstant.isBefore(Instant.now())) {
logger.debug("The token has expired at {}", dateFormatter.format(tokenExpiresInstant));
hasExpired = true;
} else {
hasExpired = false;
}
}
if (hasExpired) {
String username = (String) getConfig().get(CONFIG_USERNAME);
String password = (String) getConfig().get(CONFIG_PASSWORD);
String refreshToken = (String) getConfig().get(CONFIG_REFRESHTOKEN);
if (refreshToken == null || refreshToken.isEmpty()) {
if (username != null && !username.isEmpty() && password != null && !password.isEmpty()) {
try {
refreshToken = ssoHandler.authenticate(username, password);
} catch (Exception e) {
logger.error("An exception occurred while obtaining refresh token with username/password: '{}'", e.getMessage());
}
if (refreshToken != null) {
// store refresh token from SSO endpoint in config, clear the password
Configuration cfg = editConfiguration();
cfg.put(TeslaBindingConstants.CONFIG_REFRESHTOKEN, refreshToken);
cfg.remove(TeslaBindingConstants.CONFIG_PASSWORD);
updateConfiguration(cfg);
} else {
return new ThingStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Failed to obtain refresh token with username/password.");
}
} else {
return new ThingStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Neither a refresh token nor credentials are provided.");
}
}
this.logonToken = ssoHandler.getAccessToken(refreshToken);
if (this.logonToken == null) {
return new ThingStatusInfo(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Failed to obtain access token for API.");
}
}
return new ThingStatusInfo(ThingStatus.ONLINE, ThingStatusDetail.NONE, null);
}
use of org.openhab.binding.tesla.internal.protocol.sso.TokenResponse in project openhab-addons by openhab.
the class TeslaSSOHandler method getAccessToken.
@Nullable
public TokenResponse getAccessToken(String refreshToken) {
logger.debug("Exchanging SSO refresh token for API access token");
// get a new access token for the owner API token endpoint
RefreshTokenRequest refreshRequest = new RefreshTokenRequest(refreshToken);
String refreshTokenPayload = gson.toJson(refreshRequest);
final org.eclipse.jetty.client.api.Request request = httpClient.newRequest(URI_SSO + "/" + PATH_TOKEN);
request.content(new StringContentProvider(refreshTokenPayload));
request.header(HttpHeader.CONTENT_TYPE, "application/json");
request.method(HttpMethod.POST);
ContentResponse refreshResponse = executeHttpRequest(request);
if (refreshResponse != null && refreshResponse.getStatus() == 200) {
String refreshTokenResponse = refreshResponse.getContentAsString();
TokenResponse tokenResponse = gson.fromJson(refreshTokenResponse.trim(), TokenResponse.class);
if (tokenResponse != null && tokenResponse.access_token != null && !tokenResponse.access_token.isEmpty()) {
TokenExchangeRequest token = new TokenExchangeRequest();
String tokenPayload = gson.toJson(token);
final org.eclipse.jetty.client.api.Request logonRequest = httpClient.newRequest(URI_OWNERS + "/" + PATH_ACCESS_TOKEN);
logonRequest.content(new StringContentProvider(tokenPayload));
logonRequest.header(HttpHeader.CONTENT_TYPE, "application/json");
logonRequest.header(HttpHeader.AUTHORIZATION, "Bearer " + tokenResponse.access_token);
logonRequest.method(HttpMethod.POST);
ContentResponse logonTokenResponse = executeHttpRequest(logonRequest);
if (logonTokenResponse != null && logonTokenResponse.getStatus() == 200) {
String tokenResponsePayload = logonTokenResponse.getContentAsString();
TokenResponse tr = gson.fromJson(tokenResponsePayload.trim(), TokenResponse.class);
if (tr != null && tr.token_type != null && !tr.access_token.isEmpty()) {
return tr;
}
} else {
logger.debug("An error occurred while exchanging SSO access token for API access token: {}", (logonTokenResponse != null ? logonTokenResponse.getStatus() : "no response"));
}
}
} else {
logger.debug("An error occurred during refresh of SSO token: {}", (refreshResponse != null ? refreshResponse.getStatus() : "no response"));
}
return null;
}
Aggregations