HTTPMOD, Probleme mit Session Authentifizierung

Begonnen von Lanhydrock, 27 Januar 2015, 19:36:48

Vorheriges Thema - Nächstes Thema

Lanhydrock

Wir bekommen die zweistufige Authentifizierung auf Session-Id Basis mit der neuen Version von HTTPMOD nicht zum Laufen.
Es geht um den Siemens Webserver ozw672 (für den wir per Skript problemlos zugreifen können).

https://192.168.178.8/api/auth/login.json?user=username&pwd=password


liefert das folgende JSON:

{

"SessionId": "dd41b079-bbfb-4246-ad4c-5f5e504c55b6",

    "Result": {
"Success": "true"
    }
}


danach gehts dann mit

https://192.168.178.8/api/menutree/read_datapoint.json?SessionId=dd41b079-bbfb-4246-ad4c-5f5e504c55b6&Id=1898

entsprechend weiter, um den/die Parameter abzufragen.

{
"Data":
{

"Type": "Enumeration",
"Value": "Freigegeben für HK",
"Unit": "",
"EnumValue": "175"

},
    "Result":
{
"Success": "true"
    }
}


Wir haben folgendes definiert:

define PM HTTPMOD https://192.168.178.8/api/menutree/read_datapoint.json?SessionId=$sid&Id=300 60
attr PM userattr reAuthRegex sid1IDRegex sidHeader1 sidURL verbose
attr PM reAuthRegex .*session not valid.*
attr PM sid1IDRegex [^"]*-.*-.*-[0-9a-z]*
attr PM sidHeader1 Content-Type: application/json
attr PM sidURL https://192.168.178.8/api/auth/login.json?user=testuser&pwd=testpw
attr verbose 5


Trotzdem belegt das Modul den $hash->{sid} mit keinem Wert.

2015.01.27 18:42:00 4: PM: GetUpdate called
2015.01.27 18:42:00 5: PM: AddToQueue called, initial send queue length : 0
2015.01.27 18:42:00 5: PM: AddToQueue adds type Update to URL https://192.168.178.8/api/menutree/read_datapoint.json?SessionId=$sid&Id=300, data , header
2015.01.27 18:42:00 5: PM: handle send queue called, qlen = 1
2015.01.27 18:42:00 4: PM: HandleSendQueue sends request type Update to URL https://192.168.178.8/api/menutree/read_datapoint.json?SessionId=$sid&Id=300, data , header , timeout 2
2015.01.27 18:42:00 4: HttpUtils url=https://192.168.178.8/api/menutree/read_datapoint.json?SessionId=$sid&Id=300
2015.01.27 18:42:01 4: https://192.168.178.8/api/menutree/read_datapoint.json?SessionId=$sid&Id=300: HTTP response code 200
2015.01.27 18:42:01 4: HttpUtils https://192.168.178.8/api/menutree/read_datapoint.json?SessionId=$sid&Id=300: Got data, length: 103
2015.01.27 18:42:01 5: PM: Read Callback: Request type was Update
header: HTTP/1.0 200 OK
Content-Type: application/json;charset=utf-8
Expires: 0
Cache-Control: must-revalidate, max-age=0, proxy-revalidate, s-maxage=0, post-check=0, pre-check=0
Content-Length: 103
Connection: close
Date: Tue, 27 Jan 2015 17:40:58 GMT
Server: Siemens Switzerland Ltd.,
buffer: {
"Result": {
"Success": "false",
"Error": {
"Txt": "session not valid",
"Nr": "2"
}
}
}
2015.01.27 18:42:01 5: PM: checking response with ReAuthRegex .*session not valid.*
2015.01.27 18:42:01 4: PM: New authentication required
2015.01.27 18:42:01 4: PM: start Auth with Steps: 1
2015.01.27 18:42:01 5: PM: AddToQueue called, initial send queue length : 0
2015.01.27 18:42:01 5: PM: AddToQueue adds type Auth1 to URL https://192.168.178.8/api/auth/login.json?user=testuser&pwd=testpw, data , header Content-Type: application/json
2015.01.27 18:42:01 5: PM: handle send queue called, qlen = 1
2015.01.27 18:42:01 4: PM: HandleSendQueue sends request type Auth1 to URL https://192.168.178.8/api/auth/login.json?user=testuser&pwd=testpw, data , header Content-Type: application/json, timeout 2
2015.01.27 18:42:01 4: HttpUtils url=https://192.168.178.8/api/auth/login.json?user=testuser&pwd=testpw
2015.01.27 18:42:01 4: PM: ReQueueing GetUpdate with new retryCount 1 ...
2015.01.27 18:42:01 5: PM: AddToQueue called, initial send queue length : 0
2015.01.27 18:42:01 5: PM: AddToQueue adds type Update to URL https://192.168.178.8/api/menutree/read_datapoint.json?SessionId=$sid&Id=300, data , header
2015.01.27 18:42:01 5: PM: handle send queue called, qlen = 1
2015.01.27 18:42:01 5: PM: still waiting for reply to last request, delay sending from queue
2015.01.27 18:42:02 4: https://192.168.178.8/api/auth/login.json?user=testuser&pwd=testpw: HTTP response code 200
2015.01.27 18:42:02 4: HttpUtils https://192.168.178.8/api/auth/login.json?user=testuser&pwd=testpw: Got data, length: 104
2015.01.27 18:42:02 5: PM: Read Callback: Request type was Auth1,
header: HTTP/1.0 200 OK
Content-Type: application/json;charset=utf-8
Expires: 0
Cache-Control: must-revalidate, max-age=0, proxy-revalidate, s-maxage=0, post-check=0, pre-check=0
Content-Length: 104
Connection: close
Date: Tue, 27 Jan 2015 17:40:59 GMT
Server: Siemens Switzerland Ltd.,
buffer: {

"SessionId": "cb6954c1-3efb-4267-b910-1e072df1004f",

    "Result": {
"Success": "true"
    }
}

2015.01.27 18:42:02 1: PERL WARNING: Use of uninitialized value in concatenation (.) or string at ./FHEM/98_HTTPMOD.pm line 459.
2015.01.27 18:42:02 5: PM: set sid to
2015.01.27 18:42:02 5: PM: handle send queue called, qlen = 1
2015.01.27 18:42:02 4: PM: HandleSendQueue sends request type Update to URL https://192.168.178.8/api/menutree/read_datapoint.json?SessionId=$sid&Id=300, data , header , timeout 2
2015.01.27 18:42:02 4: HttpUtils url=https://192.168.178.8/api/menutree/read_datapoint.json?SessionId=$sid&Id=300
2015.01.27 18:42:03 4: https://192.168.178.8/api/menutree/read_datapoint.json?SessionId=$sid&Id=300: HTTP response code 200
2015.01.27 18:42:03 4: HttpUtils https://192.168.178.8/api/menutree/read_datapoint.json?SessionId=$sid&Id=300: Got data, length: 103
2015.01.27 18:42:03 5: PM: Read Callback: Request type was Update,
header: HTTP/1.0 200 OK
Content-Type: application/json;charset=utf-8
Expires: 0
Cache-Control: must-revalidate, max-age=0, proxy-revalidate, s-maxage=0, post-check=0, pre-check=0
Content-Length: 103
Connection: close
Date: Tue, 27 Jan 2015 17:41:01 GMT
Server: Siemens Switzerland Ltd.,
buffer: {
"Result": {
"Success": "false",
"Error": {
"Txt": "session not valid",
"Nr": "2"
}
}
}


Wir bräuchten da einmal Hilfe, bitte...
Wo könnten wir nachforschen, wieso $hash->{sid} nicht belegt wird.

Im Quellcode, Zeile 453 ff. geht FHEM zwar in den entsprechenden Abzweig, aber $1 scheint undefiniert/leer zu sein:


    if ($type =~ "Auth(.*)") {
        my $step = $1;
        # sid extrahieren
        if (AttrVal($name, "sid${step}IDRegex", undef)) {
            if ($buffer =~ AttrVal($name, "sid1IDRegex", undef)) {
                $hash->{sid} = $1;
                Log3 $name, 5, "$name: set sid to $hash->{sid}";
            } else {
                Log3 $name, 5, "$name: buffer did not match IDRegex " .
                        AttrVal($name, "sid${step}IDRegex", undef);
            }
        }
    } elsif ($type =~ "Set(.*)") {


Wo sollte denn die Session-Id idealerweise extrahiert und belegt werden?
- FHEMs in VM @ Mac mini & RPi, fhem2fhem
- Homematic, 1wire, Hue & Lightify & Tradfri & Xiaomi & Oblo via zigbee2mqtt/Conbee II, Rademacher DuoFern, Roto i8 & Hunter Ventile via HM-LC-Sw4-DR
- Interdomo GBK (via Siemens ozw672; dank HTTPMOD, lest Post #33765)
- homebridge(-fhem), Grafana, DBLog

Lanhydrock

So, die sid1IDRegex war falsch...

Stichwort: Regex und Capturing groups, denn:

So geht's:
attr PM sid1IDRegex .*"(.*-.*-.*-[0-9a-z]*).*

denn:


if ($buffer =~ AttrVal($name, "sid1IDRegex", undef)) {
    $hash->{sid} = $1;


belegt $hash->{sid} mit der ersten Capturing group der letzten Regex.

Man dankt.
- FHEMs in VM @ Mac mini & RPi, fhem2fhem
- Homematic, 1wire, Hue & Lightify & Tradfri & Xiaomi & Oblo via zigbee2mqtt/Conbee II, Rademacher DuoFern, Roto i8 & Hunter Ventile via HM-LC-Sw4-DR
- Interdomo GBK (via Siemens ozw672; dank HTTPMOD, lest Post #33765)
- homebridge(-fhem), Grafana, DBLog