Skip to content

Commit

Permalink
Fix person reauth (openhab#13839)
Browse files Browse the repository at this point in the history
* add null checks
* fix/improve account.html

Signed-off-by: Tom Deckers <tom@ducbase.com>
Signed-off-by: Andras Uhrin <andras.uhrin@gmail.com>
  • Loading branch information
tdeckers authored and andrasU committed Dec 24, 2022
1 parent 4ed3e55 commit ae6a063
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private void handleRedirect(Map<String, String> replaceMap, String servletBaseUR

replaceMap.put(KEY_PAGE_REFRESH,
params.isEmpty() ? "" : String.format(HTML_META_REFRESH_CONTENT, servletBaseURL));
if (!reqError.isBlank()) {
if (reqError != null && !reqError.isBlank()) {
logger.debug("Webex redirected with an error: {}", reqError);
replaceMap.put(KEY_ERROR, String.format(HTML_ERROR, reqError));
} else if (!reqState.isBlank()) {
Expand Down Expand Up @@ -172,12 +172,12 @@ private String formatAccount(String accountTemplate, WebexTeamsHandler handler,
map.put(ACCOUNT_SHWOMSG, "u-show");
map.put(ACCOUNT_MSG, "Configure account.");
} else if (handler.isAuthorized()) {
map.put(ACCOUNT_USER_ID, String.format(" (Authorized user: %s)", webexUser));
map.put(ACCOUNT_USER_ID, String.format("Authorized user: %s", webexUser));
map.put(ACCOUNT_SHOWBTN, "u-hide");
map.put(ACCOUNT_SHWOMSG, "u-show");
map.put(ACCOUNT_MSG, "Authorized.");
} else if (!webexUser.isBlank()) {
map.put(ACCOUNT_USER_ID, String.format(" (Unauthorized user: %s)", webexUser));
} else if (webexUser.isBlank()) {
map.put(ACCOUNT_USER_ID, "Unauthorized user");
map.put(ACCOUNT_SHOWBTN, "u-show");
map.put(ACCOUNT_SHWOMSG, "u-hide");
map.put(ACCOUNT_MSG, "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class WebexTeamsHandler extends BaseThingHandler implements AccessTokenRe

private @Nullable OAuthClientService authService;

private boolean configured = false; // is the handler instance properly configured?
private boolean configured; // is the handler instance properly configured?
private volatile boolean active; // is the handler instance active?
String accountType = ""; // bot or person?

Expand All @@ -97,6 +97,7 @@ public Collection<Class<? extends ThingHandlerService>> getServices() {
public void initialize() {
logger.debug("Initializing thing {}", this.getThing().getUID());
active = true;
this.configured = false;
config = getConfigAs(WebexTeamsConfiguration.class);

final String token = config.token;
Expand Down Expand Up @@ -210,6 +211,9 @@ protected String authorize(String redirectUri, String reqCode) throws WebexTeams

public boolean isAuthorized() {
final AccessTokenResponse accessTokenResponse = getAccessTokenResponse();
if (accessTokenResponse == null) {
return false;
}

if ("person".equals(this.accountType)) {
return accessTokenResponse != null && accessTokenResponse.getAccessToken() != null
Expand Down Expand Up @@ -287,7 +291,7 @@ private boolean refresh() {
updateStatus(ThingStatus.ONLINE);
return true;
} catch (WebexTeamsException e) {
logger.warn("Failed to refresh: {}", e.getMessage());
logger.warn("Failed to refresh: {}. Did you authorize?", e.getMessage());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
}
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="row" id="${account.id}">
<div class="one column">${account.type}:</div>
<div class="nine columns"><i>${account.name}${account.user}</i></div>
<div class="three columns">${account.name}:</div>
<div class="seven columns"><i>${account.user} (${account.type})</i></div>
<div class="two columns ${account.showbtn}">
<div class="button-primary"><a href=${account.authorize}>Authorize Account</a></div>
</div>
Expand Down

0 comments on commit ae6a063

Please sign in to comment.