Wsse::authenticate()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 13
cp 0
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * A php library for using the Emarsys API.
5
 *
6
 * @link      https://github.com/quitoque/emarsys-php-client
7
 * @package   emarsys-php-client
8
 * @license   MIT
9
 * @copyright Copyright (c) 2017 Quitoque <[email protected]>
10
 */
11
12
namespace Emarsys\HttpClient\Message\Authentication;
13
14
use Http\Message\Authentication;
15
use Psr\Http\Message\RequestInterface;
16
17
/**
18
 * Class Wsse.
19
 *
20
 * @author Claude Khedhiri <[email protected]>
21
 */
22
final class Wsse implements Authentication
23
{
24
    /**
25
     * @var string
26
     */
27
    private $username;
28
29
    /**
30
     * @var string
31
     */
32
    private $apiKey;
33
34
    /**
35
     * @param string $username
36
     * @param string $password
0 ignored issues
show
Bug introduced by
There is no parameter named $password. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
37
     * @param mixed  $apiKey
38
     */
39 3
    public function __construct($username, $apiKey)
40
    {
41 3
        $this->username = $username;
42 3
        $this->apiKey = $apiKey;
43 3
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function authenticate(RequestInterface $request)
49
    {
50
        // the current time encoded as an ISO 8601 date string
51
        $created = new \DateTime();
52
        $iso8601 = $created->format(\DateTime::ISO8601);
53
        // the md5 of a random string . e.g. a timestamp
54
        $nonce = md5($created->modify('next friday')->getTimestamp());
55
56
        // The algorithm to generate the digest is as follows:
57
        // Concatenate: Nonce + Created + Secret
58
        // Hash the result using the SHA1 algorithm
59
        // Encode the result to base64
60
        $digest = base64_encode(sha1($nonce.$iso8601.$this->apiKey));
61
        $wsse = sprintf(
62
            'UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"',
63
            $this->username,
64
            $digest,
65
            $nonce,
66
            $iso8601
67
        );
68
69
        return $request
70
            ->withHeader('Authorization', 'WSSE profile="UsernameToken"')
71
            ->withHeader('X-WSSE', $wsse)
72
        ;
73
    }
74
}
75