1

I need to add credentias for my soap server, but i don't have much experience in this. What i have to use for add the user and password?

<?php
require_once "vendor/econea/nusoap/src/nusoap.php";
$namespace = "testeSoap";
$server = new soap_server();
$server->configureWSDL("PinchesSOAP",$namespace);
$server->wsdl->schemaTargetNamespace = $namespace;

$server->wsdl->addComplexType(
    'Cliente',
    'complexType',
    'struct',
    'all',
    '',
    array(
        'name' => array('name' => 'name', 'type'=>'xsd:string'),
    )
);

$server->wsdl->addComplexType(
    'response',
    'complexType',
    'struct',
    'all',
    '',
    array(
        'status' => array('name'=>'status', 'type'=>'xsd:string')
    )
);

$server->register(
    'notifyCustomerCreationRequest',
    array('name' => 'tns:Cliente'),
    array('name' => 'tns:response'),
    $namespace,
    false,
    'rpc',
    'encoded',
    ''
);

function notifyCustomerCreationRequest(){
    return array(
        "status" => "PEN"
    );
}

$POST_DATA = file_get_contents("php://input");
$server->service($POST_DATA);
exit();

I will use the soapUI and need authenticate the info, but dont have any idea.

1 Answers1

0

For simple auth based in heads you can do the following:

In your main method (notifyCustomerCreationRequest):

function notifyCustomerCreationRequest(){
  global $server;
  $requestHeaders = $server->requestHeader;
  if(ValidateUser($requestHeaders)){
     //your business logic here
  }
}

function ValidateUser($headers){
    if(empty($requestHeaders)){
        //$log->info("Envelope without headers");
        return false;
    }
    if(array_key_exists("Security", $requestHeaders)){
        if(array_key_exists("UsernameToken", $requestHeaders["Security"])){
            if(!array_key_exists("Username", $requestHeaders["Security"]["UsernameToken"])){
                //$log->info("Empty Username -> Headers");
                return false;
            }
            if(!array_key_exists("Password", $requestHeaders["Security"]["UsernameToken"])){
                //$log->info("Empty Password -> Headers");
                return false;
            }

            $user = $requestHeaders["Security"]["UsernameToken"]["Username"];
            $pass = $requestHeaders["Security"]["UsernameToken"]["Password"];
            //$log->info("User:".$usuario);
            //$log->info("Pass:".PRINT_R($pass));
            if(($user== "blabla") && ($pass == "blabla")){
                return true;
            }

            return false;
        }else{
            //$log->info("Empty UsernameToken -> Headers");
            return false;
        }
    }else{
        $log->info("Empty Security -> Headers");
        return false;
    }
    return false;
}

The SOAP Envelope:

<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">
   <soapenv:Header>
   <wsse:Security soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    <wsse:UsernameToken soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next" soapenv:mustUnderstand="0">
    <wsse:Username soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next">USER</wsse:Username>
     <wsse:Password soapenv:actor="http://schemas.xmlsoap.org/soap/actor/next">PASSWORD</wsse:Password>
     </wsse:UsernameToken>
     </wsse:Security>
   </soapenv:Header>
   <soapenv:Body>
   </soapenv:Body>
</soapenv:Envelope>
santip97
  • 21
  • 3