]> git.basschouten.com Git - openhab-addons.git/blob
06d70191833914005fa9675a4a7d4ee852ef43df
[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     @Override
51     public String getAuthorizationHeader() throws IndegoAuthenticationException {
52         final AccessTokenResponse accessTokenResponse;
53         try {
54             accessTokenResponse = getAccessToken();
55         } catch (OAuthException | OAuthResponseException e) {
56             var throwable = new IndegoAuthenticationException(
57                     "Error fetching access token. Invalid authcode? Please generate a new one -> "
58                             + getAuthorizationUrl(),
59                     e);
60             listener.onFailedAuthorization(throwable);
61             throw throwable;
62         } catch (IOException e) {
63             var throwable = new IndegoAuthenticationException("An unexpected IOException occurred: " + e.getMessage(),
64                     e);
65             listener.onFailedAuthorization(throwable);
66             throw throwable;
67         }
68
69         String accessToken = accessTokenResponse.getAccessToken();
70         if (accessToken == null || accessToken.isEmpty()) {
71             var throwable = new IndegoAuthenticationException(
72                     "No access token. Is this thing authorized? -> " + getAuthorizationUrl());
73             listener.onFailedAuthorization(throwable);
74             throw throwable;
75         }
76         if (accessTokenResponse.getRefreshToken() == null || accessTokenResponse.getRefreshToken().isEmpty()) {
77             var throwable = new IndegoAuthenticationException(
78                     "No refresh token. Please reauthorize -> " + getAuthorizationUrl());
79             listener.onFailedAuthorization(throwable);
80             throw throwable;
81         }
82
83         listener.onSuccessfulAuthorization();
84
85         return BEARER + accessToken;
86     }
87
88     public AccessTokenResponse getAccessToken() throws OAuthException, OAuthResponseException, IOException {
89         AccessTokenResponse accessTokenResponse = oAuthClientService.getAccessTokenResponse();
90         if (accessTokenResponse == null) {
91             throw new OAuthException("No access token response");
92         }
93
94         return accessTokenResponse;
95     }
96
97     private String getAuthorizationUrl() {
98         try {
99             return oAuthClientService.getAuthorizationUrl(BSK_REDIRECT_URI, BSK_SCOPE, null);
100         } catch (OAuthException e) {
101             return "";
102         }
103     }
104 }