2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.boschindego.internal;
15 import static org.openhab.binding.boschindego.internal.BoschIndegoBindingConstants.*;
17 import java.io.IOException;
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;
27 * The {@link AuthorizationController} acts as a bridge between
28 * {@link OAuthClientService} and {@link IndegoController}.
30 * @author Jacob Laursen - Initial contribution
33 public class AuthorizationController implements AuthorizationProvider {
35 private static final String BEARER = "Bearer ";
37 private final AuthorizationListener listener;
39 private OAuthClientService oAuthClientService;
41 public AuthorizationController(OAuthClientService oAuthClientService, AuthorizationListener listener) {
42 this.oAuthClientService = oAuthClientService;
43 this.listener = listener;
46 public void setOAuthClientService(OAuthClientService oAuthClientService) {
47 this.oAuthClientService = oAuthClientService;
51 public String getAuthorizationHeader() throws IndegoAuthenticationException {
52 final AccessTokenResponse accessTokenResponse;
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(),
60 listener.onFailedAuthorization(throwable);
62 } catch (IOException e) {
63 var throwable = new IndegoAuthenticationException("An unexpected IOException occurred: " + e.getMessage(),
65 listener.onFailedAuthorization(throwable);
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);
76 if (accessTokenResponse.getRefreshToken() == null || accessTokenResponse.getRefreshToken().isEmpty()) {
77 var throwable = new IndegoAuthenticationException(
78 "No refresh token. Please reauthorize -> " + getAuthorizationUrl());
79 listener.onFailedAuthorization(throwable);
83 listener.onSuccessfulAuthorization();
85 return BEARER + accessToken;
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");
94 return accessTokenResponse;
97 private String getAuthorizationUrl() {
99 return oAuthClientService.getAuthorizationUrl(BSK_REDIRECT_URI, BSK_SCOPE, null);
100 } catch (OAuthException e) {