Completed
Push — master ( 1a59c2...c73113 )
by Florent
02:55
created

JWELoader   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 16
c 4
b 1
f 0
lcom 1
cbo 3
dl 0
loc 110
rs 10

8 Methods

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