]> git.basschouten.com Git - openhab-addons.git/blob
a782c13dc78e6936edb9fbcfbffa50406a344595
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.mielecloud.internal.config.servlet;
14
15 import javax.servlet.http.HttpServletRequest;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.mielecloud.internal.auth.OAuthException;
19 import org.openhab.binding.mielecloud.internal.config.OAuthAuthorizationHandler;
20 import org.openhab.binding.mielecloud.internal.config.exception.NoOngoingAuthorizationException;
21 import org.openhab.core.thing.ThingUID;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Servlet processing the response by the Miele service after a login. This servlet is called as a result of a
27  * completed login to the Miele service and assumes that the OAuth 2 parameters are passed. Depending on the parameters
28  * and whether the token response can be fetched either the browser is redirected to the success or the failure page.
29  *
30  * @author Björn Lange - Initial Contribution
31  */
32 @NonNullByDefault
33 public final class ResultServlet extends AbstractRedirectionServlet {
34     private static final long serialVersionUID = 2157912755568949550L;
35
36     public static final String CODE_PARAMETER_NAME = "code";
37     public static final String STATE_PARAMETER_NAME = "state";
38     public static final String ERROR_PARAMETER_NAME = "error";
39
40     private final Logger logger = LoggerFactory.getLogger(ResultServlet.class);
41
42     private final OAuthAuthorizationHandler authorizationHandler;
43
44     /**
45      * Creates a new {@link ResultServlet}.
46      *
47      * @param authorizationHandler Handler implementing the OAuth authorization.
48      */
49     public ResultServlet(OAuthAuthorizationHandler authorizationHandler) {
50         this.authorizationHandler = authorizationHandler;
51     }
52
53     @Override
54     protected String getRedirectionDestination(HttpServletRequest request) {
55         String error = request.getParameter(ERROR_PARAMETER_NAME);
56         if (error != null) {
57             logger.warn("Received error response: {}", error);
58             return "/mielecloud/failure?" + FailureServlet.OAUTH2_ERROR_PARAMETER_NAME + "=" + error;
59         }
60
61         String code = request.getParameter(CODE_PARAMETER_NAME);
62         if (code == null) {
63             logger.warn("Code is null");
64             return "/mielecloud/failure?" + FailureServlet.ILLEGAL_RESPONSE_PARAMETER_NAME + "=true";
65         }
66         String state = request.getParameter(STATE_PARAMETER_NAME);
67         if (state == null) {
68             logger.warn("State is null");
69             return "/mielecloud/failure?" + FailureServlet.ILLEGAL_RESPONSE_PARAMETER_NAME + "=true";
70         }
71
72         try {
73             ThingUID bridgeId = authorizationHandler.getBridgeUid();
74             String email = authorizationHandler.getEmail();
75
76             StringBuffer requestUrl = request.getRequestURL();
77             if (requestUrl == null) {
78                 return "/mielecloud/failure?" + FailureServlet.MISSING_REQUEST_URL_PARAMETER_NAME + "=true";
79             }
80
81             try {
82                 authorizationHandler.completeAuthorization(requestUrl.toString() + "?" + request.getQueryString());
83             } catch (OAuthException e) {
84                 logger.warn("Failed to complete authorization.", e);
85                 return "/mielecloud/failure?" + FailureServlet.FAILED_TO_COMPLETE_AUTHORIZATION_PARAMETER_NAME
86                         + "=true";
87             }
88
89             return "/mielecloud/success?" + SuccessServlet.BRIDGE_UID_PARAMETER_NAME + "=" + bridgeId.getAsString()
90                     + "&" + SuccessServlet.EMAIL_PARAMETER_NAME + "=" + email;
91         } catch (NoOngoingAuthorizationException e) {
92             logger.warn("Failed to complete authorization: There is no ongoing authorization or it timed out");
93             return "/mielecloud/failure?" + FailureServlet.NO_ONGOING_AUTHORIZATION_PARAMETER_NAME + "=true";
94         }
95     }
96 }