]> git.basschouten.com Git - openhab-addons.git/blob
56ad773b5fda60d70d8f191c1a193f3e8e41ea02
[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 java.io.IOException;
16
17 import javax.servlet.http.HttpServletRequest;
18 import javax.servlet.http.HttpServletResponse;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21
22 /**
23  * Servlet showing a failure page.
24  *
25  * @author Björn Lange - Initial Contribution
26  */
27 @NonNullByDefault
28 public class FailureServlet extends AbstractShowPageServlet {
29     private static final long serialVersionUID = -5195984256535664942L;
30
31     public static final String OAUTH2_ERROR_PARAMETER_NAME = "oauth2Error";
32     public static final String ILLEGAL_RESPONSE_PARAMETER_NAME = "illegalResponse";
33     public static final String NO_ONGOING_AUTHORIZATION_PARAMETER_NAME = "noOngoingAuthorization";
34     public static final String FAILED_TO_COMPLETE_AUTHORIZATION_PARAMETER_NAME = "failedToCompleteAuthorization";
35     public static final String MISSING_BRIDGE_UID_PARAMETER_NAME = "missingBridgeUid";
36     public static final String MISSING_EMAIL_PARAMETER_NAME = "missingEmail";
37     public static final String MALFORMED_BRIDGE_UID_PARAMETER_NAME = "malformedBridgeUid";
38     public static final String MISSING_REQUEST_URL_PARAMETER_NAME = "missingRequestUrl";
39
40     public static final String OAUTH2_ERROR_ACCESS_DENIED = "access_denied";
41     public static final String OAUTH2_ERROR_INVALID_REQUEST = "invalid_request";
42     public static final String OAUTH2_ERROR_UNAUTHORIZED_CLIENT = "unauthorized_client";
43     public static final String OAUTH2_ERROR_UNSUPPORTED_RESPONSE_TYPE = "unsupported_response_type";
44     public static final String OAUTH2_ERROR_INVALID_SCOPE = "invalid_scope";
45     public static final String OAUTH2_ERROR_SERVER_ERROR = "server_error";
46     public static final String OAUTH2_ERROR_TEMPORARY_UNAVAILABLE = "temporarily_unavailable";
47
48     private static final String ERROR_MESSAGE_TEXT_PLACEHOLDER = "<!-- ERROR MESSAGE TEXT -->";
49
50     /**
51      * Creates a new {@link FailureServlet}.
52      *
53      * @param resourceLoader Loader to use for resources.
54      */
55     public FailureServlet(ResourceLoader resourceLoader) {
56         super(resourceLoader);
57     }
58
59     @Override
60     protected String handleGetRequest(HttpServletRequest request, HttpServletResponse response)
61             throws MieleHttpException, IOException {
62         return getResourceLoader().loadResourceAsString("failure.html").replace(ERROR_MESSAGE_TEXT_PLACEHOLDER,
63                 getErrorMessage(request));
64     }
65
66     private String getErrorMessage(HttpServletRequest request) {
67         String oauth2Error = request.getParameter(OAUTH2_ERROR_PARAMETER_NAME);
68         if (oauth2Error != null) {
69             return getOAuth2ErrorMessage(oauth2Error);
70         } else if (ServletUtil.isParameterEnabled(request, ILLEGAL_RESPONSE_PARAMETER_NAME)) {
71             return "Miele cloud service returned an illegal response.";
72         } else if (ServletUtil.isParameterEnabled(request, NO_ONGOING_AUTHORIZATION_PARAMETER_NAME)) {
73             return "There is no ongoing authorization. Please start an authorization first.";
74         } else if (ServletUtil.isParameterEnabled(request, FAILED_TO_COMPLETE_AUTHORIZATION_PARAMETER_NAME)) {
75             return "Completing the final authorization request failed. Please try the config flow again.";
76         } else if (ServletUtil.isParameterEnabled(request, MISSING_BRIDGE_UID_PARAMETER_NAME)) {
77             return "Missing bridge UID.";
78         } else if (ServletUtil.isParameterEnabled(request, MISSING_EMAIL_PARAMETER_NAME)) {
79             return "Missing e-mail address.";
80         } else if (ServletUtil.isParameterEnabled(request, MALFORMED_BRIDGE_UID_PARAMETER_NAME)) {
81             return "Malformed bridge UID.";
82         } else if (ServletUtil.isParameterEnabled(request, MISSING_REQUEST_URL_PARAMETER_NAME)) {
83             return "Missing request URL. Please try the config flow again.";
84         } else {
85             return "Unknown error.";
86         }
87     }
88
89     private String getOAuth2ErrorMessage(String oauth2Error) {
90         return "OAuth2 authentication with Miele cloud service failed: " + getOAuth2ErrorDetailMessage(oauth2Error);
91     }
92
93     private String getOAuth2ErrorDetailMessage(String oauth2Error) {
94         switch (oauth2Error) {
95             case OAUTH2_ERROR_ACCESS_DENIED:
96                 return "Access denied.";
97             case OAUTH2_ERROR_INVALID_REQUEST:
98                 return "Malformed request.";
99             case OAUTH2_ERROR_UNAUTHORIZED_CLIENT:
100                 return "Account not authorized to request authorization code.";
101             case OAUTH2_ERROR_UNSUPPORTED_RESPONSE_TYPE:
102                 return "Obtaining an authorization code is not supported.";
103             case OAUTH2_ERROR_INVALID_SCOPE:
104                 return "Invalid scope.";
105             case OAUTH2_ERROR_SERVER_ERROR:
106                 return "Unexpected server error.";
107             case OAUTH2_ERROR_TEMPORARY_UNAVAILABLE:
108                 return "Authorization server temporarily unavailable.";
109             default:
110                 return "Unknown error code \"" + oauth2Error + "\".";
111         }
112     }
113 }