Skip to content

Commit

Permalink
Added debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
andan67 committed Apr 3, 2022
1 parent a506995 commit dc256e1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.openhab.binding.sony.internal.scalarweb.ScalarWebConstants;
import org.openhab.binding.sony.internal.transports.SonyTransport;
import org.openhab.binding.sony.internal.transports.TransportOptionHeader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This class contains the logic to determine if an authorization call is needed (via {@link SonyAuth})
Expand All @@ -28,6 +30,10 @@
*/
@NonNullByDefault
public class SonyAuthChecker {

/** The logger */
private final Logger logger = LoggerFactory.getLogger(SonyAuthChecker.class);

/** The transport to use for check authorization */
private final SonyTransport transport;

Expand Down Expand Up @@ -69,8 +75,10 @@ public CheckResult checkResult(final CheckResultCallback callback) {
final TransportOptionHeader authHeader = new TransportOptionHeader(
NetUtil.createAccessCodeHeader(localAccessCode));
try {
logger.debug("localAccessCode: '{}'", localAccessCode);
transport.setOption(authHeader);
if (AccessResult.OK.equals(callback.checkResult())) {
logger.debug("checkResult: '{}'", CheckResult.OK_HEADER.getCode());
return CheckResult.OK_HEADER;
}
} finally {
Expand All @@ -81,16 +89,20 @@ public CheckResult checkResult(final CheckResultCallback callback) {
// If we made it here - we are likely not header based but cookie based (or we are not even authenticated)
// Attempt the check result without the auth header and return OK_COOKIE is good
final AccessResult res = callback.checkResult();
logger.debug("res: '{}'", res.getCode());
if (res == null) {
logger.debug("checkResult: '{}'", CheckResult.OTHER);
return new CheckResult(CheckResult.OTHER, "Check result returned null");
}

if (AccessResult.OK.equals(res)) {
logger.debug("checkResult: '{}'", CheckResult.OK_COOKIE.getCode());
return CheckResult.OK_COOKIE;
}

// We aren't either cookie or header based - return the results (likely needs pairing or the screen is off or
// not on the main screen)
logger.debug("checkResult: '{}'", new CheckResult(res).getCode());
return new CheckResult(res);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,11 @@ T getCallback() {
*/
@Nullable
LoginUnsuccessfulResponse login() throws IOException {
logger.debug("Starting ircc login...");
transport.setOption(TransportOptionAutoAuth.FALSE);

final String accessCode = config.getAccessCode();

logger.debug("accessCode = '{}'", SonyUtil.defaultIfEmpty(accessCode, ""));
final SonyAuthChecker authChecker = new SonyAuthChecker(transport, accessCode);
final CheckResult checkResult = authChecker.checkResult(() -> {
// To check our authorization, we execute a non-existent command.
Expand All @@ -176,8 +177,10 @@ LoginUnsuccessfulResponse login() throws IOException {
// If we have 200 (good) or 500 (command not found), we return OK
// If we have 403 (unauthorized) or 503 (service not available), we need pairing
HttpResponse status = irccClient.executeSoap(transport, "nonexistentcommand");
logger.debug("Response status of calling non existing command: '{}'", status.getHttpCode());
if (status.getHttpCode() == HttpStatus.OK_200) {
status = getStatus();
logger.debug("Response status of calling ircc status: '{}'", status.getHttpCode());
}

if (status.getHttpCode() == HttpStatus.OK_200
Expand All @@ -190,6 +193,7 @@ LoginUnsuccessfulResponse login() throws IOException {
}
return new AccessResult(status);
});
logger.debug("checkResult: '{}'", checkResult.getCode());

if (CheckResult.OK_HEADER.equals(checkResult)) {
if (accessCode == null || accessCode.isEmpty()) {
Expand All @@ -200,9 +204,11 @@ LoginUnsuccessfulResponse login() throws IOException {
return new LoginUnsuccessfulResponse(ThingStatusDetail.CONFIGURATION_ERROR,
"Access code cannot be blank");
} else {
logger.debug("setup header auth");
SonyAuth.setupHeader(accessCode, transport);
}
} else if (CheckResult.OK_COOKIE.equals(checkResult)) {
logger.debug("setup cookie auth");
SonyAuth.setupCookie(transport);
} else if (AccessResult.NEEDSPAIRING.equals(checkResult)) {
if (accessCode == null || accessCode.isEmpty()) {
Expand All @@ -219,6 +225,7 @@ LoginUnsuccessfulResponse login() throws IOException {
}
} else {
final AccessResult resp = sonyAuth.registerRenewal(transport);
logger.debug("register renewal");
if (AccessResult.OK.equals(resp)) {
SonyAuth.setupCookie(transport);
} else {
Expand Down

0 comments on commit dc256e1

Please sign in to comment.