WsseAuthHeader.php 1.62 KB
Newer Older
Dainis Abols's avatar
Dainis Abols committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php

namespace Lu\VIISSchools\Helpers;

/**
 * Class WsseAuthHeader
 * Creates SOAP header object with main connection details
 *
 * @author  Dainis Abols <dainis.abols@lu.lv>
 * @since   30.09.2020
 *
 * @package Lu\VIISSchools
 */
class WsseAuthHeader extends \SoapHeader
{
    /**
     * SOAP Structure standard
     *
     * @var string
     */
    private $wss_ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';

    /**
     * WsseAuthHeader constructor.
     *
     * @param      $user
     * @param      $pass
     * @param null $ns
     */
    public function __construct($user, $pass, $ns = null)
    {
        // Use standard ns if no other provided
        if ($ns) {
            $this->wss_ns = $ns;
        }

        // Make connection object
        $auth           = new \stdClass();
        $auth->Username = new \SoapVar($user, XSD_STRING, null, $this->wss_ns, null, $this->wss_ns);
        $auth->Password = new \SoapVar($pass, XSD_STRING, null, $this->wss_ns, null, $this->wss_ns);

        // Make connection token
        $username_token                = new \stdClass();
        $username_token->UsernameToken = new \SoapVar($auth, SOAP_ENC_OBJECT, null, $this->wss_ns, 'UsernameToken', $this->wss_ns);

        // Make security object
        $security_sv = new \SoapVar(
            new \SoapVar($username_token, SOAP_ENC_OBJECT, null, $this->wss_ns, 'UsernameToken', $this->wss_ns),
            SOAP_ENC_OBJECT, null, $this->wss_ns, 'Security', $this->wss_ns
        );

        // Continue to SOAP defaults
        parent::__construct($this->wss_ns, 'Security', $security_sv, true);
    }
}