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.zoneminder.internal.handler;
15 import java.net.URLEncoder;
16 import java.nio.charset.StandardCharsets;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.zoneminder.internal.dto.AuthResponseDTO;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 import com.google.gson.Gson;
27 * The {@link ZmAuth} manages the authentication process when Zoneminder
28 * authentication is enabled. This class requests access and refresh tokens based
29 * on the expiration times provided by the Zoneminder server.
31 * @author Mark Hilbush - Initial contribution
36 private final Logger logger = LoggerFactory.getLogger(ZmAuth.class);
38 private final ZmBridgeHandler bridgeHandler;
39 private final String authContent;
40 private final boolean usingAuthorization;
41 private boolean isAuthorized;
43 private @Nullable String refreshToken;
44 private long refreshTokenExpiresAt;
45 private @Nullable String accessToken;
46 private long accessTokenExpiresAt;
48 public ZmAuth(ZmBridgeHandler handler) {
49 this(handler, null, null);
52 public ZmAuth(ZmBridgeHandler handler, @Nullable String user, @Nullable String pass) {
53 this.bridgeHandler = handler;
54 if (user == null || pass == null) {
55 logger.debug("ZmAuth: Authorization is disabled");
56 usingAuthorization = false;
60 logger.debug("ZmAuth: Authorization is enabled");
61 usingAuthorization = true;
63 String encodedUser = URLEncoder.encode(user, StandardCharsets.UTF_8);
64 String encodedPass = URLEncoder.encode(pass, StandardCharsets.UTF_8);
65 authContent = encodedUser == null ? ""
66 : String.format("user=%s&pass=%s&stateful=1", encodedUser, encodedPass);
70 public String getAccessToken() {
71 String localAccessToken = accessToken;
72 return localAccessToken != null ? localAccessToken : "";
75 public boolean usingAuthorization() {
76 return usingAuthorization;
79 public boolean isAuthorized() {
80 if (usingAuthorization()) {
86 private void checkTokens() {
87 if (isExpired(refreshTokenExpiresAt)) {
89 } else if (isExpired(accessTokenExpiresAt)) {
94 @SuppressWarnings("null")
95 private synchronized void getNewRefreshToken() {
96 // First check to see if another thread has updated it
97 if (!isExpired(refreshTokenExpiresAt)) {
100 String url = bridgeHandler.buildLoginUrl();
101 logger.debug("ZmAuth: Update expired REFRESH token using url '{}'", url);
102 String response = bridgeHandler.executePost(url, authContent, "application/x-www-form-urlencoded");
103 if (response != null) {
104 Gson gson = bridgeHandler.getGson();
105 AuthResponseDTO auth = gson.fromJson(response, AuthResponseDTO.class);
106 if (auth != null && auth.exception == null && auth.refreshToken != null && auth.accessToken != null) {
107 updateRefreshToken(auth);
108 updateAccessToken(auth);
113 isAuthorized = false;
116 @SuppressWarnings("null")
117 private synchronized void getNewAccessToken() {
118 // First check to see if another thread has updated it
119 if (!isExpired(accessTokenExpiresAt)) {
122 String url = bridgeHandler.buildLoginUrl(String.format("?token=%s", refreshToken));
123 logger.debug("ZmAuth: Update expired ACCESS token using url '{}'", url);
124 String response = bridgeHandler.executeGet(url);
125 if (response != null) {
126 Gson gson = bridgeHandler.getGson();
127 AuthResponseDTO auth = gson.fromJson(response, AuthResponseDTO.class);
128 if (auth != null && auth.exception == null && auth.accessToken != null) {
129 updateAccessToken(auth);
134 isAuthorized = false;
137 private void updateAccessToken(AuthResponseDTO auth) {
138 accessToken = auth.accessToken;
139 accessTokenExpiresAt = getExpiresAt(auth.accessTokenExpires);
140 logger.trace("ZmAuth: New access token: {}", accessToken);
141 logger.trace("ZmAuth: New access token expires in {} sec", getExpiresIn(accessTokenExpiresAt));
144 private void updateRefreshToken(AuthResponseDTO auth) {
145 refreshToken = auth.refreshToken;
146 refreshTokenExpiresAt = getExpiresAt(auth.refreshTokenExpires);
147 logger.trace("ZmAuth: New refresh token: {}", refreshToken);
148 logger.trace("ZmAuth: New refresh token expires in {} sec", getExpiresIn(refreshTokenExpiresAt));
151 private boolean isExpired(long expiresAt) {
152 return (System.currentTimeMillis() / 1000) > expiresAt;
155 private long getExpiresAt(String expiresInSeconds) {
157 return (System.currentTimeMillis() / 1000) + (Integer.parseInt(expiresInSeconds) - 300);
158 } catch (NumberFormatException e) {
163 private long getExpiresIn(long expiresAtSeconds) {
164 return expiresAtSeconds - (System.currentTimeMillis() / 1000);