]> git.basschouten.com Git - openhab-addons.git/blob
be121492acfa774fcf4bd2ffb4aae01865f30b25
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.tesla.internal.handler;
14
15 import static org.openhab.binding.tesla.internal.TeslaBindingConstants.*;
16
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.TimeUnit;
19 import java.util.concurrent.TimeoutException;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.eclipse.jetty.client.HttpClient;
24 import org.eclipse.jetty.client.api.ContentResponse;
25 import org.eclipse.jetty.client.util.StringContentProvider;
26 import org.eclipse.jetty.http.HttpHeader;
27 import org.eclipse.jetty.http.HttpMethod;
28 import org.openhab.binding.tesla.internal.protocol.sso.RefreshTokenRequest;
29 import org.openhab.binding.tesla.internal.protocol.sso.TokenResponse;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import com.google.gson.Gson;
34
35 /**
36  * The {@link TeslaSSOHandler} is responsible for authenticating with the Tesla SSO service.
37  *
38  * @author Christian Güdel - Initial contribution
39  */
40 @NonNullByDefault
41 public class TeslaSSOHandler {
42
43     private final HttpClient httpClient;
44     private final Gson gson = new Gson();
45     private final Logger logger = LoggerFactory.getLogger(TeslaSSOHandler.class);
46
47     public TeslaSSOHandler(HttpClient httpClient) {
48         this.httpClient = httpClient;
49     }
50
51     @Nullable
52     public TokenResponse getAccessToken(String refreshToken) {
53         logger.debug("Exchanging SSO refresh token for API access token");
54
55         // get a new access token for the owner API token endpoint
56         RefreshTokenRequest refreshRequest = new RefreshTokenRequest(refreshToken);
57         String refreshTokenPayload = gson.toJson(refreshRequest);
58
59         final org.eclipse.jetty.client.api.Request request = httpClient.newRequest(URI_SSO + "/" + PATH_TOKEN);
60         request.content(new StringContentProvider(refreshTokenPayload));
61         request.header(HttpHeader.CONTENT_TYPE, "application/json");
62         request.method(HttpMethod.POST);
63
64         ContentResponse refreshResponse = executeHttpRequest(request);
65
66         if (refreshResponse != null && refreshResponse.getStatus() == 200) {
67             String refreshTokenResponse = refreshResponse.getContentAsString();
68             TokenResponse tokenResponse = gson.fromJson(refreshTokenResponse.trim(), TokenResponse.class);
69
70             if (tokenResponse != null && tokenResponse.access_token != null && !tokenResponse.access_token.isEmpty()) {
71                 return tokenResponse;
72             } else {
73                 logger.debug("An error occurred while exchanging SSO auth token for API access token.");
74             }
75         } else {
76             logger.debug("An error occurred during refresh of SSO token: {}",
77                     (refreshResponse != null ? refreshResponse.getStatus() : "no response"));
78         }
79
80         return null;
81     }
82
83     @Nullable
84     private ContentResponse executeHttpRequest(org.eclipse.jetty.client.api.Request request) {
85         request.timeout(10, TimeUnit.SECONDS);
86
87         ContentResponse response;
88         try {
89             response = request.send();
90             return response;
91         } catch (InterruptedException | TimeoutException | ExecutionException e) {
92             logger.debug("An exception occurred while invoking a HTTP request: '{}'", e.getMessage());
93             return null;
94         }
95     }
96 }