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

JWSLoader   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 15
c 3
b 0
f 0
lcom 1
cbo 5
dl 0
loc 87
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A loadSerializedJsonJWS() 0 17 2
A getProtectedHeaders() 0 6 2
A getHeaders() 0 8 2
B populatePayload() 0 21 7
A isPayloadEncoded() 0 4 2
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