[homeconnect] Fix login for simulator environment (#13653)

* Add redirect URI to oAuth request

Signed-off-by: Jonas Brüstel <jonas@bruestel.net>
This commit is contained in:
bruestel 2022-11-04 18:28:40 +01:00 committed by GitHub
parent c114714a7b
commit 5654b9f97a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 6 deletions

View File

@ -95,6 +95,7 @@ public class HomeConnectServlet extends HttpServlet {
private static final String PARAM_BRIDGE_ID = "bridgeId";
private static final String PARAM_THING_ID = "thingId";
private static final String PARAM_PATH = "path";
private static final String PARAM_REDIRECT_URI = "redirectUri";
private static final String ACTION_AUTHORIZE = "authorize";
private static final String ACTION_CLEAR_CREDENTIALS = "clearCredentials";
private static final String ACTION_SHOW_DETAILS = "show-details";
@ -392,8 +393,11 @@ public class HomeConnectServlet extends HttpServlet {
HomeConnectBridgeHandler bridgeHandler = bridgeHandlerOptional.get();
if (ACTION_AUTHORIZE.equals(action)) {
try {
String authorizationUrl = bridgeHandler.getOAuthClientService().getAuthorizationUrl(null, null,
bridgeHandler.getThing().getUID().getAsString());
String redirectUri = bridgeHandler.getConfiguration().isSimulator()
? request.getParameter(PARAM_REDIRECT_URI)
: null;
String authorizationUrl = bridgeHandler.getOAuthClientService().getAuthorizationUrl(redirectUri,
null, bridgeHandler.getThing().getUID().getAsString());
logger.debug("Generated authorization url: {}", authorizationUrl);
response.sendRedirect(authorizationUrl);
@ -491,8 +495,11 @@ public class HomeConnectServlet extends HttpServlet {
Optional<HomeConnectBridgeHandler> bridgeHandler = getBridgeHandler(state);
if (bridgeHandler.isPresent()) {
try {
String redirectUri = bridgeHandler.get().getConfiguration().isSimulator()
? request.getRequestURL().toString()
: null;
AccessTokenResponse accessTokenResponse = bridgeHandler.get().getOAuthClientService()
.getAccessTokenResponseByAuthorizationCode(code, null);
.getAccessTokenResponseByAuthorizationCode(code, redirectUri);
logger.debug("access token response: {}", accessTokenResponse);

View File

@ -6,6 +6,7 @@
feather.replace();
$(".redirectUri").text(window.location.href.substring(0, window.location.href.lastIndexOf('/homeconnect') + 12));
$(".redirectUriInput").val(window.location.href.substring(0, window.location.href.lastIndexOf('/homeconnect') + 12));
$('#apiDetailModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);

View File

@ -71,12 +71,13 @@
<dd class="col-sm-8">
<form method="post" style="display: inline-block">
<input type="hidden" name="bridgeId" th:value="${uid}"/>
<input type="hidden" name="action" value="clearCredentials">
<input type="hidden" name="action" value="clearCredentials" />
<button type="submit" class="btn btn-secondary btn-sm">Clear stored credentials</button>
</form>
<form method="post" style="display: inline-block">
<input type="hidden" name="bridgeId" th:value="${uid}"/>
<input type="hidden" name="action" value="authorize">
<input type="hidden" name="bridgeId" th:value="${uid}" />
<input type="hidden" name="action" value="authorize" />
<input type="hidden" name="redirectUri" value="" class="redirectUriInput" />
<button type="submit" class="btn btn-primary btn-sm">Authorize bridge</button>
</form>
</dd>