• Aktuell
    • Tags
    • Beliebt
    • Benutzer
    • Gruppen
    • Suche
    • Registrieren
    • Anmelden

    Gelöst Hilfe zum CSRFTOKEN benötigt (alte API)

    ChurchTools Schnittstellen
    2
    3
    246
    Lade mehr Beiträge
    • Älteste zuerst
    • Neuste zuerst
    • Meiste Stimmen
    Antworten
    • In einem neuen Thema antworten
    Anmelden zum Antworten
    Dieses Thema wurde gelöscht. Nur Nutzer mit entsprechenden Rechten können es sehen.
    • A
      AchimK
      zuletzt editiert von

      Ich nutze die API für den Abruf von Kalenderdaten und habe mir das Script mit Hilfe einiger Beispielscripts zusammengebaut. Allerdings enthalten die Beispielscripts noch nicht den Abruf des Tokens.

      In der Hilfe heißt es:
      Um den Token zu bekommen, ruft man, nachdem man eingeloggt ist die Api /api/csrftoken auf.

      Könnte mir jemand auf die Sprünge helfen, wie das in PHP aussehen würde?

      Danke für eure Mühe, einem Laien auf die Sprünge zu helfen.

      1 Antwort Letzte Antwort Antworten Zitieren 0
      • F
        fodinabor
        zuletzt editiert von fodinabor

        Hi,
        bin da auch gerade drüber gestolpert.. hab das jetzt mal so gelöst:

        function CT_getCoreDomain($domain) {
            $pos = strpos($domain, "://");
            if($pos !== false)
                $domain = substr($domain, $pos + 3);
            
            $pos = strpos($domain, "/");
            if($pos !== false)
                $domain = substr($domain, 0, $pos);
            
            $pos = strpos($domain, "?");
            if($pos !== false)
                $domain = substr($domain, 0, $pos);
            
            return $domain;
        }
        
        function &getCookies(){
        	static $CT_cookies = array();
        	return $CT_cookies;
        }
        
        function CT_getCookies() {
        	$CT_cookies = &getCookies();
        	$res = "";
        	foreach ($CT_cookies as $key => $cookie) {
        		$res .= "$key=$cookie; ";
        	}
        	return $res;
        }
        
        function CT_saveCookies($r) {
        	$CT_cookies = &getCookies();
        	foreach ($r as $hdr) {
        		if (preg_match('/^Set-Cookie:\s*([^;]+)/', $hdr, $matches)) {
        			parse_str($matches[1], $tmp);
        			$CT_cookies += $tmp;
        		}
        	}
        }
        
        function CTNew_getRequest($url, $data) {
            $header = "Cookie: ". CT_getCookies() . "\r\nContent-type: application/x-www-form-urlencoded\r\n";
               
            $options = array(
        		'http'=>array(
        			'header' => $header,
        			'method' => 'GET',
        			'content' => http_build_query($data),
                    'ignore_errors' => true,
        		)
        	);
        	$context = stream_context_create($options);
        	$result = file_get_contents($url, false, $context);
            
            $status_line = $http_response_header[0];
            preg_match('{HTTP\/\S*\s(\d{3})}', $status_line, $match);
            $status = $match[1];
                if($status !== "200")
                throw new Exception("$url request failed, with $status.");
                
            $obj = json_decode($result);
        	CT_saveCookies($http_response_header);
        	return $obj;
        }
        
        function CT_getCSRFToken($url){
            static $token = ""; // cache
            if(!empty($token))
                return $token;
            try {
                $result = CTNew_getRequest("https://" . CT_getCoreDomain($url) . "/api/csrftoken", array());
                $token = $result->data;
                return $token;
            } catch(Exception $ex) {
                echo 'CT Error: ' .  $ex->getMessage() . "<br>";
            }
            return "";
        }
        
        function CT_sendRequest($url, $data, $csrf_token_required = true) {
            $header = "Cookie: ". CT_getCookies() . "\r\nContent-type: application/x-www-form-urlencoded\r\n";
            if($csrf_token_required)
                $header .= "csrf-token: " . CT_getCSRFToken($url) . "\r\n";
        	$options = array(
        		'http'=>array(
        			'header' => $header,
        			'method' => 'POST',
        			'content' => http_build_query($data),
        		)
        	);
            $context = stream_context_create($options);
        	$result = file_get_contents($url, false, $context);
            $obj = json_decode($result);
        	if ($obj->status == 'error') {
        		echo "There is an error: $obj->message";
        	}
        	CT_saveCookies($http_response_header);
        	return $obj;
        }
        
        function CT_login($domain){
        	$url = $domain . 'login/ajax';
        
        	// Login info of EGP Bot's account
        	$token = ""; // todo - fill me
        	$id = 0; // todo - fill me
        
        	// Now use token to login 
        	$data = array('func' => 'loginWithToken', 
        				  'token' => $token,
        				  'id' => $id,
        				  'directtool' => 'Me' // todo: fillme
        			);
        	$result = CT_sendRequest($url, $data, false);
        	return ($result->status == "success");
        }
        
        function CT_logout($domain){
        	$url = $domain . 'login/ajax';
        
        	$data = array('func' => 'logout');
        	$result = CT_sendRequest($url, $data);
        }
        

        merke: beim Login, kann man noch kein CSRF Token angeben -> extra parameter bei CT_sendRequest

        Die $domain parameter von CT_logout und CT_login sind jeweils https://xxx/?q= daher muss für CTNew_getRequest der query teil abgeschnitten werden..

        Hoffe das hilft, viele Grüße,
        Joachim

        Edit: solltest du zufälligerweise irgenwelche von meinen Beispielen (https://github.com/fodinabor/) herangezogen haben: die sind jetzt alle up-to-date.

        A 1 Antwort Letzte Antwort Antworten Zitieren 0
        • A
          AchimK @fodinabor
          zuletzt editiert von

          @fodinabor
          HERZLICHEN DANK! Das hat mir sehr geholfen.

          1 Antwort Letzte Antwort Antworten Zitieren 0
          • Erster Beitrag
            Letzter Beitrag