Completed
Push — v2.0.x ( 24925e...149802 )
by Florent
02:52
created

JWELoader::populateSharedProtectedHeaders()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 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 Base64Url\Base64Url;
15
use Jose\Object\JWE;
16
use Jose\Object\JWEInterface;
17
use Jose\Object\Recipient;
18
use Jose\Object\RecipientInterface;
19
20
final class JWELoader
21
{
22
    /**
23
     * Loader constructor.
24
     */
25
    private function __construct()
26
    {
27
    }
28
29
    /**
30
     * @param array $data
31
     *
32
     * @return \Jose\Object\JWEInterface
33
     */
34
    public static function loadSerializedJsonJWE(array $data)
35
    {
36
        $jwe = new JWE();
37
        $jwe = $jwe->withCiphertext(Base64Url::decode($data['ciphertext']));
38
39
        self::populateIV($jwe, $data);
40
        self::populateAAD($jwe, $data);
41
        self::populateTag($jwe, $data);
42
        self::populateSharedProtectedHeaders($jwe, $data);
43
        self::populateSharedHeaders($jwe, $data);
44
45
        foreach ($data['recipients'] as $recipient) {
46
            $object = new Recipient();
47
            self::populateRecipientHeaders($object, $recipient);
48
            self::populateRecipientEncryptedKey($object, $recipient);
49
50
            $jwe = $jwe->addRecipient($object);
51
        }
52
53
        return $jwe;
54
    }
55
56
    /**
57
     * @param \Jose\Object\JWEInterface $jwe
58
     * @param array                     $data
59
     */
60
    private static function populateIV(JWEInterface &$jwe, array $data)
61
    {
62
        if (array_key_exists('iv', $data)) {
63
            $jwe = $jwe->withIV(Base64Url::decode($data['iv']));
64
        }
65
    }
66
67
    /**
68
     * @param \Jose\Object\JWEInterface $jwe
69
     * @param array                     $data
70
     */
71
    private static function populateAAD(JWEInterface &$jwe, array $data)
72
    {
73
        if (array_key_exists('aad', $data)) {
74
            $jwe = $jwe->withAAD(Base64Url::decode($data['aad']));
75
        }
76
    }
77
78
    /**
79
     * @param \Jose\Object\JWEInterface $jwe
80
     * @param array                     $data
81
     */
82
    private static function populateTag(JWEInterface &$jwe, array $data)
83
    {
84
        if (array_key_exists('tag', $data)) {
85
            $jwe = $jwe->withTag(Base64Url::decode($data['tag']));
86
        }
87
    }
88
89
    /**
90
     * @param \Jose\Object\JWEInterface $jwe
91
     * @param array                     $data
92
     */
93
    private static function populateSharedProtectedHeaders(JWEInterface &$jwe, array $data)
94
    {
95
        if (array_key_exists('protected', $data)) {
96
            $jwe = $jwe->withEncodedSharedProtectedHeaders($data['protected']);
97
            $jwe = $jwe->withSharedProtectedHeaders(json_decode(Base64Url::decode($data['protected']), true));
98
        }
99
    }
100
101
    /**
102
     * @param \Jose\Object\JWEInterface $jwe
103
     * @param array                     $data
104
     */
105
    private static function populateSharedHeaders(JWEInterface &$jwe, array $data)
106
    {
107
        if (array_key_exists('unprotected', $data)) {
108
            $jwe = $jwe->withSharedHeaders($data['unprotected']);
109
        }
110
    }
111
112
    /**
113
     * @param \Jose\Object\RecipientInterface $recipient
114
     * @param array                           $data
115
     */
116
    private static function populateRecipientHeaders(RecipientInterface &$recipient, array $data)
117
    {
118
        if (array_key_exists('header', $data)) {
119
            $recipient = $recipient->withHeaders($data['header']);
120
        }
121
    }
122
123
    /**
124
     * @param \Jose\Object\RecipientInterface $recipient
125
     * @param array                           $data
126
     */
127
    private static function populateRecipientEncryptedKey(RecipientInterface &$recipient, array $data)
128
    {
129
        if (array_key_exists('encrypted_key', $data)) {
130
            $recipient = $recipient->withEncryptedKey(Base64Url::decode($data['encrypted_key']));
131
        }
132
    }
133
}
134