]> git.basschouten.com Git - openhab-addons.git/blob
bcacc8b4e91f71bebf62558c45e9787591f0f7a9
[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                   """
43                 <?xml version="1.0" encoding="UTF-8"?>
44                 <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">
45                  <soapenv:Body>
46                   <authenticate1 xmlns="utcs">
47                    <password>%s</password>
48                    <username>%s</username>
49                    <application>%s</application>
50                   </authenticate1>
51                  </soapenv:Body>
52                 </soapenv:Envelope>\
53                 """;
54         // @formatter:on
55
56         String query = String.format(soapQuery, password, username, application);
57         String response = sendSoapQuery(null, query);
58         return new WSLoginResult().parseXMLData(response);
59     }
60 }