]> git.basschouten.com Git - openhab-addons.git/blob
0482e3e1ee60914923ebc84d9dd29354333ca392
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.boschindego.internal;
14
15 import static org.openhab.binding.boschindego.internal.BoschIndegoBindingConstants.*;
16
17 import java.io.IOException;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.boschindego.internal.exceptions.IndegoAuthenticationException;
21 import org.openhab.core.auth.client.oauth2.AccessTokenResponse;
22 import org.openhab.core.auth.client.oauth2.OAuthClientService;
23 import org.openhab.core.auth.client.oauth2.OAuthException;
24 import org.openhab.core.auth.client.oauth2.OAuthResponseException;
25
26 /**
27  * The {@link AuthorizationController} acts as a bridge between
28  * {@link OAuthClientService} and {@link IndegoController}.
29  * 
30  * @author Jacob Laursen - Initial contribution
31  */
32 @NonNullByDefault
33 public class AuthorizationController implements AuthorizationProvider {
34
35     private static final String BEARER = "Bearer ";
36
37     private final AuthorizationListener listener;
38
39     private OAuthClientService oAuthClientService;
40
41     public AuthorizationController(OAuthClientService oAuthClientService, AuthorizationListener listener) {
42         this.oAuthClientService = oAuthClientService;
43         this.listener = listener;
44     }
45
46     public void setOAuthClientService(OAuthClientService oAuthClientService) {
47         this.oAuthClientService = oAuthClientService;
48     }
49
50     public String getAuthorizationHeader() throws IndegoAuthenticationException {
51         final AccessTokenResponse accessTokenResponse;
52         try {
53             accessTokenResponse = getAccessToken();
54         } catch (OAuthException | OAuthResponseException e) {
55             var throwable = new IndegoAuthenticationException(
56                     "Error fetching access token. Invalid authcode? Please generate a new one -> "
57                             + getAuthorizationUrl(),
58                     e);
59             listener.onFailedAuthorization(throwable);
60             throw throwable;
61         } catch (IOException e) {
62             var throwable = new IndegoAuthenticationException("An unexpected IOException occurred: " + e.getMessage(),
63                     e);
64             listener.onFailedAuthorization(throwable);
65             throw throwable;
66         }
67
68         String accessToken = accessTokenResponse.getAccessToken();
69         if (accessToken == null || accessToken.isEmpty()) {
70             var throwable = new IndegoAuthenticationException(
71                     "No access token. Is this thing authorized? -> " + getAuthorizationUrl());
72             listener.onFailedAuthorization(throwable);
73             throw throwable;
74         }
75         if (accessTokenResponse.getRefreshToken() == null || accessTokenResponse.getRefreshToken().isEmpty()) {
76             var throwable = new IndegoAuthenticationException(
77                     "No refresh token. Please reauthorize -> " + getAuthorizationUrl());
78             listener.onFailedAuthorization(throwable);
79             throw throwable;
80         }
81
82         listener.onSuccessfulAuthorization();
83
84         return BEARER + accessToken;
85     }
86
87     public AccessTokenResponse getAccessToken() throws OAuthException, OAuthResponseException, IOException {
88         AccessTokenResponse accessTokenResponse = oAuthClientService.getAccessTokenResponse();
89         if (accessTokenResponse == null) {
90             throw new OAuthException("No access token response");
91         }
92
93         return accessTokenResponse;
94     }
95
96     private String getAuthorizationUrl() {
97         try {
98             return oAuthClientService.getAuthorizationUrl(BSK_REDIRECT_URI, BSK_SCOPE, null);
99         } catch (OAuthException e) {
100             return "";
101         }
102     }
103 }