Completed
Push — v2.0.x ( 2b2b8e...21aa40 )
by Florent
03:32
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
     * @param array $data
24
     *
25
     * @return \Jose\Object\JWEInterface
26
     */
27
    public static function loadSerializedJsonJWE(array $data)
28
    {
29
        $jwe = new JWE();
30
        $jwe = $jwe->withCiphertext(Base64Url::decode($data['ciphertext']));
31
32
        self::populateIV($jwe, $data);
33
        self::populateAAD($jwe, $data);
34
        self::populateTag($jwe, $data);
35
        self::populateSharedProtectedHeaders($jwe, $data);
36
        self::populateSharedHeaders($jwe, $data);
37
38
        foreach ($data['recipients'] as $recipient) {
39
            $object = new Recipient();
40
            self::populateRecipientHeaders($object, $recipient);
41
            self::populateRecipientEncryptedKey($object, $recipient);
42
43
            $jwe = $jwe->addRecipient($object);
44
        }
45
46
        return $jwe;
47
    }
48
49
    /**
50
     * @param \Jose\Object\JWEInterface $jwe
51
     * @param array                     $data
52
     */
53
    private static function populateIV(JWEInterface &$jwe, array $data)
54
    {
55
        if (array_key_exists('iv', $data)) {
56
            $jwe = $jwe->withIV(Base64Url::decode($data['iv']));
57
        }
58
    }
59
60
    /**
61
     * @param \Jose\Object\JWEInterface $jwe
62
     * @param array                     $data
63
     */
64
    private static function populateAAD(JWEInterface &$jwe, array $data)
65
    {
66
        if (array_key_exists('aad', $data)) {
67
            $jwe = $jwe->withAAD(Base64Url::decode($data['aad']));
68
        }
69
    }
70
71
    /**
72
     * @param \Jose\Object\JWEInterface $jwe
73
     * @param array                     $data
74
     */
75
    private static function populateTag(JWEInterface &$jwe, array $data)
76
    {
77
        if (array_key_exists('tag', $data)) {
78
            $jwe = $jwe->withTag(Base64Url::decode($data['tag']));
79
        }
80
    }
81
82
    /**
83
     * @param \Jose\Object\JWEInterface $jwe
84
     * @param array                     $data
85
     */
86
    private static function populateSharedProtectedHeaders(JWEInterface &$jwe, array $data)
87
    {
88
        if (array_key_exists('protected', $data)) {
89
            $jwe = $jwe->withEncodedSharedProtectedHeaders($data['protected']);
90
            $jwe = $jwe->withSharedProtectedHeaders(json_decode(Base64Url::decode($data['protected']), true));
91
        }
92
    }
93
94
    /**
95
     * @param \Jose\Object\JWEInterface $jwe
96
     * @param array                     $data
97
     */
98
    private static function populateSharedHeaders(JWEInterface &$jwe, array $data)
99
    {
100
        if (array_key_exists('unprotected', $data)) {
101
            $jwe = $jwe->withSharedHeaders($data['unprotected']);
102
        }
103
    }
104
105
    /**
106
     * @param \Jose\Object\RecipientInterface $recipient
107
     * @param array                           $data
108
     */
109
    private static function populateRecipientHeaders(RecipientInterface &$recipient, array $data)
110
    {
111
        if (array_key_exists('header', $data)) {
112
            $recipient = $recipient->withHeaders($data['header']);
113
        }
114
    }
115
116
    /**
117
     * @param \Jose\Object\RecipientInterface $recipient
118
     * @param array                           $data
119
     */
120
    private static function populateRecipientEncryptedKey(RecipientInterface &$recipient, array $data)
121
    {
122
        if (array_key_exists('encrypted_key', $data)) {
123
            $recipient = $recipient->withEncryptedKey(Base64Url::decode($data['encrypted_key']));
124
        }
125
    }
126
}
127