Completed
Push — v2.0.x ( 2b2b8e...21aa40 )
by Florent
03:32
created

Loader   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 31
Bugs 10 Features 4
Metric Value
wmc 28
c 31
b 10
f 4
lcom 1
cbo 2
dl 0
loc 151
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 11 3
B convert() 0 15 7
B fromFlattenedSerializationRecipientToSerialization() 0 20 5
A fromFlattenedSerializationSignatureToSerialization() 0 19 4
A fromCompactSerializationToSerialization() 0 12 3
A fromCompactSerializationRecipientToSerialization() 0 18 4
A fromCompactSerializationSignatureToSerialization() 0 14 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;
13
14
use Jose\Util\JWELoader;
15
use Jose\Util\JWSLoader;
16
17
/**
18
 * Class able to load JWS or JWE.
19
 * JWS object can also be verified.
20
 */
21
final class Loader implements LoaderInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public static function load($input)
27
    {
28
        $json = self::convert($input);
29
        if (array_key_exists('signatures', $json)) {
30
            return JWSLoader::loadSerializedJsonJWS($json);
31
        }
32
        if (array_key_exists('recipients', $json)) {
33
            return JWELoader::loadSerializedJsonJWE($json);
34
        }
35
        throw new \InvalidArgumentException('Unable to load the input');
36
    }
37
38
    /**
39
     * @param string $input
40
     *
41
     * @return array
42
     */
43
    private static function convert($input)
44
    {
45
        if (is_array($data = json_decode($input, true))) {
46
            if (array_key_exists('signatures', $data) || array_key_exists('recipients', $data)) {
47
                return $data;
48
            } elseif (array_key_exists('signature', $data)) {
49
                return self::fromFlattenedSerializationSignatureToSerialization($data);
50
            } elseif (array_key_exists('ciphertext', $data)) {
51
                return self::fromFlattenedSerializationRecipientToSerialization($data);
52
            }
53
        } elseif (is_string($input)) {
54
            return self::fromCompactSerializationToSerialization($input);
55
        }
56
        throw new \InvalidArgumentException('Unsupported input');
57
    }
58
59
    /**
60
     * @param $input
61
     *
62
     * @return array
63
     */
64
    private static function fromFlattenedSerializationRecipientToSerialization($input)
65
    {
66
        $recipient = [];
67
        foreach (['header', 'encrypted_key'] as $key) {
68
            if (array_key_exists($key, $input)) {
69
                $recipient[$key] = $input[$key];
70
            }
71
        }
72
        $recipients = [
73
            'ciphertext' => $input['ciphertext'],
74
            'recipients' => [$recipient],
75
        ];
76
        foreach (['ciphertext', 'protected', 'unprotected', 'iv', 'aad', 'tag'] as $key) {
77
            if (array_key_exists($key, $input)) {
78
                $recipients[$key] = $input[$key];
79
            }
80
        }
81
82
        return $recipients;
83
    }
84
85
    /**
86
     * @param $input
87
     *
88
     * @return array
89
     */
90
    private static function fromFlattenedSerializationSignatureToSerialization($input)
91
    {
92
        $signature = [
93
            'signature' => $input['signature'],
94
        ];
95
        foreach (['protected', 'header'] as $key) {
96
            if (array_key_exists($key, $input)) {
97
                $signature[$key] = $input[$key];
98
            }
99
        }
100
101
        $temp = [];
102
        if (!empty($input['payload'])) {
103
            $temp['payload'] = $input['payload'];
104
        }
105
        $temp['signatures'] = [$signature];
106
107
        return $temp;
108
    }
109
110
    /**
111
     * @param string $input
112
     *
113
     * @return array
114
     */
115
    private static function fromCompactSerializationToSerialization($input)
116
    {
117
        $parts = explode('.', $input);
118
        switch (count($parts)) {
119
            case 3:
120
                return self::fromCompactSerializationSignatureToSerialization($parts);
121
            case 5:
122
                return self::fromCompactSerializationRecipientToSerialization($parts);
123
            default:
124
                throw new \InvalidArgumentException('Unsupported input');
125
        }
126
    }
127
128
    /**
129
     * @param array $parts
130
     *
131
     * @return array
132
     */
133
    private static function fromCompactSerializationRecipientToSerialization(array $parts)
134
    {
135
        $recipient = [];
136
        if (!empty($parts[1])) {
137
            $recipient['encrypted_key'] = $parts[1];
138
        }
139
140
        $recipients = [
141
            'recipients' => [$recipient],
142
        ];
143
        foreach ([3 => 'ciphertext', 0 => 'protected', 2 => 'iv', 4 => 'tag'] as $part => $key) {
144
            if (!empty($parts[$part])) {
145
                $recipients[$key] = $parts[$part];
146
            }
147
        }
148
149
        return $recipients;
150
    }
151
152
    /**
153
     * @param array $parts
154
     *
155
     * @return array
156
     */
157
    private static function fromCompactSerializationSignatureToSerialization(array $parts)
158
    {
159
        $temp = [];
160
161
        if (!empty($parts[1])) {
162
            $temp['payload'] = $parts[1];
163
        }
164
        $temp['signatures'] = [[
165
            'protected' => $parts[0],
166
            'signature' => $parts[2],
167
        ]];
168
169
        return $temp;
170
    }
171
}
172