]> git.basschouten.com Git - openhab-addons.git/blob
24b7b8557350b7d3e02d15f0a5bcd2c202b2d41c
[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.ihc.internal.ws.services;
14
15 import org.openhab.binding.ihc.internal.ws.datatypes.WSLoginResult;
16 import org.openhab.binding.ihc.internal.ws.exeptions.IhcExecption;
17 import org.openhab.binding.ihc.internal.ws.http.IhcConnectionPool;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Class to handle IHC / ELKO LS Controller's authentication service.
23  *
24  * Communication to controller need to be authenticated. On successful
25  * authentication Controller returns session id which need to be used further
26  * communication.
27  *
28  * @author Pauli Anttila - Initial contribution
29  */
30 public class IhcAuthenticationService extends IhcBaseService {
31     private final Logger logger = LoggerFactory.getLogger(IhcAuthenticationService.class);
32
33     public IhcAuthenticationService(String host, int timeout, IhcConnectionPool ihcConnectionPool) {
34         super(ihcConnectionPool, timeout, host, "AuthenticationService");
35     }
36
37     public WSLoginResult authenticate(String username, String password, String application) throws IhcExecption {
38         logger.debug("Authenticate");
39
40         // @formatter:off
41         final String soapQuery =
42                   "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
43                 + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"
44                 + " <soapenv:Body>\n"
45                 + "  <authenticate1 xmlns=\"utcs\">\n"
46                 + "   <password>%s</password>\n"
47                 + "   <username>%s</username>\n"
48                 + "   <application>%s</application>\n"
49                 + "  </authenticate1>\n"
50                 + " </soapenv:Body>\n"
51                 + "</soapenv:Envelope>";
52         // @formatter:on
53
54         String query = String.format(soapQuery, password, username, application);
55         String response = sendSoapQuery(null, query);
56         return new WSLoginResult().parseXMLData(response);
57     }
58 }