Completed
Push — master ( 05e2ef...22b3b9 )
by Florent
02:34
created

JWSLoader::isPayloadEncoded()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace Jose\Util;
13
14
use Assert\Assertion;
15
use Base64Url\Base64Url;
16
use Jose\Object\JWS;
17
use Jose\Object\JWSInterface;
18
use Jose\Object\SignatureInterface;
19
20
final class JWSLoader
21
{
22
    /**
23
     * @param array $data
24
     *
25
     * @return \Jose\Object\JWSInterface
26
     */
27
    public static function loadSerializedJsonJWS(array $data)
28
    {
29
        $jws = new JWS();
30
31
32
        foreach ($data['signatures'] as $signature) {
33
            $bin_signature = Base64Url::decode($signature['signature']);
34
            $protected_headers = self::getProtectedHeaders($signature);
35
            $headers = self::getHeaders($signature);
36
37
            $jws = $jws->addSignatureFromLoadedData($bin_signature, $protected_headers, $headers);
38
        }
39
40
        self::populatePayload($jws, $data);
41
42
        return $jws;
43
    }
44
45
    /**
46
     * @param array $data
47
     *
48
     * @return string|null
49
     */
50
    private static function getProtectedHeaders(array $data)
51
    {
52
        if (array_key_exists('protected', $data)) {
53
            return $data['protected'];
54
        }
55
    }
56
57
    /**
58
     * @param array $data
59
     *
60
     * @return array
61
     */
62
    private static function getHeaders(array $data)
63
    {
64
        if (array_key_exists('header', $data)) {
65
            return $data['header'];
66
        }
67
68
        return [];
69
    }
70
71
    /**
72
     * @param \Jose\Object\JWSInterface $jws
73
     * @param array                     $data
74
     */
75
    private static function populatePayload(JWSInterface &$jws, array $data)
76
    {
77
        $is_encoded = null;
78
        foreach($jws->getSignatures() as $signature) {
79
            if (null === $is_encoded) {
80
                $is_encoded = self::isPayloadEncoded($signature);
81
            }
82
            Assertion::eq($is_encoded, self::isPayloadEncoded($signature), 'Foreign payload encoding detected. The JWS cannot be loaded.');
83
        }
84
        if (array_key_exists('payload', $data)) {
85
            $payload = $data['payload'];
86
            if (false !== $is_encoded) {
87
                $payload = Base64Url::decode($payload);
88
            }
89
            $json = json_decode($payload, true);
90
            if (null !== $json && !empty($payload)) {
91
                $payload = $json;
92
            }
93
            $jws = $jws->withPayload($payload);
94
        }
95
    }
96
97
    /**
98
     * @param \Jose\Object\SignatureInterface $signature
99
     *
100
     * @return bool
101
     */
102
    private static function isPayloadEncoded(SignatureInterface $signature)
103
    {
104
        return !$signature->hasProtectedHeader('b64') || true === $signature->getProtectedHeader('b64');
105
    }
106
}
107