]> git.basschouten.com Git - openhab-addons.git/blob
638c8c9882449834c25a9d3184e2cc0d312506f9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.netatmo.internal.api;
14
15 import static org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.*;
16 import static org.openhab.core.auth.oauth2client.internal.Keyword.*;
17
18 import java.net.URI;
19 import java.util.List;
20 import java.util.Optional;
21 import java.util.Set;
22 import java.util.stream.Stream;
23
24 import javax.ws.rs.core.UriBuilder;
25
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.FeatureArea;
29 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.Scope;
30 import org.openhab.binding.netatmo.internal.handler.ApiBridgeHandler;
31
32 /**
33  * The {@link AuthenticationApi} handles oAuth2 authentication and token refreshing
34  *
35  * @author GaĆ«l L'hopital - Initial contribution
36  * @author Jacob Laursen - Refactored to use standard OAuth2 implementation
37  */
38 @NonNullByDefault
39 public class AuthenticationApi extends RestManager {
40     public static final URI TOKEN_URI = getApiBaseBuilder(PATH_OAUTH, SUB_PATH_TOKEN).build();
41     public static final URI AUTH_URI = getApiBaseBuilder(PATH_OAUTH, SUB_PATH_AUTHORIZE).build();
42
43     private List<Scope> grantedScope = List.of();
44     private @Nullable String authorization;
45
46     public AuthenticationApi(ApiBridgeHandler bridge) {
47         super(bridge, FeatureArea.NONE);
48     }
49
50     public void setAccessToken(@Nullable String accessToken) {
51         if (accessToken != null) {
52             authorization = "Bearer " + accessToken;
53         } else {
54             authorization = null;
55         }
56     }
57
58     public void setScope(String scope) {
59         grantedScope = Stream.of(scope.split(" ")).map(s -> Scope.valueOf(s.toUpperCase())).toList();
60     }
61
62     public void dispose() {
63         authorization = null;
64         grantedScope = List.of();
65     }
66
67     public Optional<String> getAuthorization() {
68         return Optional.ofNullable(authorization);
69     }
70
71     public boolean matchesScopes(Set<Scope> requiredScopes) {
72         return requiredScopes.isEmpty() || grantedScope.containsAll(requiredScopes);
73     }
74
75     public boolean isConnected() {
76         return authorization != null;
77     }
78
79     public static UriBuilder getAuthorizationBuilder(String clientId) {
80         return getApiBaseBuilder(PATH_OAUTH, SUB_PATH_AUTHORIZE).queryParam(CLIENT_ID, clientId)
81                 .queryParam(SCOPE, FeatureArea.ALL_SCOPES).queryParam(STATE, clientId);
82     }
83 }