Completed
Push — master ( 7aad34...96e43e )
by Florent
02:35
created

Loader::convert()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 15
rs 8.2222
cc 7
eloc 11
nc 6
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;
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
    }
36
37
    /**
38
     * @param string $input
39
     *
40
     * @return array
41
     */
42
    private static function convert($input)
43
    {
44
        if (is_array($data = json_decode($input, true))) {
45
            if (array_key_exists('signatures', $data) || array_key_exists('recipients', $data)) {
46
                return $data;
47
            } elseif (array_key_exists('signature', $data)) {
48
                return self::fromFlattenedSerializationSignatureToSerialization($data);
49
            } elseif (array_key_exists('ciphertext', $data)) {
50
                return self::fromFlattenedSerializationRecipientToSerialization($data);
51
            }
52
        } elseif (is_string($input)) {
53
            return self::fromCompactSerializationToSerialization($input);
54
        }
55
        throw new \InvalidArgumentException('Unsupported input');
56
    }
57
58
    /**
59
     * @param $input
60
     *
61
     * @return array
62
     */
63
    private static function fromFlattenedSerializationRecipientToSerialization($input)
64
    {
65
        $recipient = [];
66
        foreach (['header', 'encrypted_key'] as $key) {
67
            if (array_key_exists($key, $input)) {
68
                $recipient[$key] = $input[$key];
69
            }
70
        }
71
        $recipients = [
72
            'ciphertext' => $input['ciphertext'],
73
            'recipients' => [$recipient],
74
        ];
75
        foreach (['ciphertext', 'protected', 'unprotected', 'iv', 'aad', 'tag'] as $key) {
76
            if (array_key_exists($key, $input)) {
77
                $recipients[$key] = $input[$key];
78
            }
79
        }
80
81
        return $recipients;
82
    }
83
84
    /**
85
     * @param $input
86
     *
87
     * @return array
88
     */
89
    private static function fromFlattenedSerializationSignatureToSerialization($input)
90
    {
91
        $signature = [
92
            'signature' => $input['signature'],
93
        ];
94
        foreach (['protected', 'header'] as $key) {
95
            if (array_key_exists($key, $input)) {
96
                $signature[$key] = $input[$key];
97
            }
98
        }
99
100
        $temp = [];
101
        if (!empty($input['payload'])) {
102
            $temp['payload'] = $input['payload'];
103
        }
104
        $temp['signatures'] = [$signature];
105
106
        return $temp;
107
    }
108
109
    /**
110
     * @param string $input
111
     *
112
     * @return array
113
     */
114
    private static function fromCompactSerializationToSerialization($input)
115
    {
116
        $parts = explode('.', $input);
117
        switch (count($parts)) {
118
            case 3:
119
                return self::fromCompactSerializationSignatureToSerialization($parts);
120
            case 5:
121
                return self::fromCompactSerializationRecipientToSerialization($parts);
122
            default:
123
                throw new \InvalidArgumentException('Unsupported input');
124
        }
125
    }
126
127
    /**
128
     * @param array $parts
129
     *
130
     * @return array
131
     */
132
    private static function fromCompactSerializationRecipientToSerialization(array $parts)
133
    {
134
        $recipient = [];
135
        if (!empty($parts[1])) {
136
            $recipient['encrypted_key'] = $parts[1];
137
        }
138
139
        $recipients = [
140
            'recipients' => [$recipient],
141
        ];
142
        foreach ([3 => 'ciphertext', 0 => 'protected', 2 => 'iv', 4 => 'tag'] as $part => $key) {
143
            if (!empty($parts[$part])) {
144
                $recipients[$key] = $parts[$part];
145
            }
146
        }
147
148
        return $recipients;
149
    }
150
151
    /**
152
     * @param array $parts
153
     *
154
     * @return array
155
     */
156
    private static function fromCompactSerializationSignatureToSerialization(array $parts)
157
    {
158
        $temp = [];
159
160
        if (!empty($parts[1])) {
161
            $temp['payload'] = $parts[1];
162
        }
163
        $temp['signatures'] = [[
164
            'protected' => $parts[0],
165
            'signature' => $parts[2],
166
        ]];
167
168
        return $temp;
169
    }
170
}
171