Completed
Push — master ( 05e2ef...22b3b9 )
by Florent
02:34
created

JWSFactory::createJWSWithDetachedPayload()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 1
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\Factory;
13
14
use Assert\Assertion;
15
use Jose\Object\JWKInterface;
16
use Jose\Object\JWS;
17
use Jose\Signer;
18
use Psr\Log\LoggerInterface;
19
20
final class JWSFactory
21
{
22
    /**
23
     * @param mixed $payload
24
     * @param bool  $is_payload_detached
25
     *
26
     * @return \Jose\Object\JWSInterface
27
     */
28
    public static function createJWS($payload, $is_payload_detached = false)
29
    {
30
        $jws = new JWS();
31
        $jws = $jws->withPayload($payload);
32
        if (true === $is_payload_detached) {
33
            $jws = $jws->withDetachedPayload();
34
        } else {
35
            $jws = $jws->withAttachedPayload();
36
        }
37
38
        return $jws;
39
    }
40
41
    /**
42
     * @param mixed                         $payload
43
     * @param \Jose\Object\JWKInterface     $signature_key
44
     * @param array                         $protected_headers
45
     * @param \Psr\Log\LoggerInterface|null $logger
46
     *
47
     * @return string
48
     */
49
    public static function createJWSToCompactJSON($payload, JWKInterface $signature_key, array $protected_headers, LoggerInterface $logger = null)
50
    {
51
        $jws = self::createJWSAndSign($payload, $signature_key, $protected_headers, [], $logger);
52
53
        return $jws->toCompactJSON(0);
54
    }
55
56
    /**
57
     * @param mixed                         $payload
58
     * @param \Jose\Object\JWKInterface     $signature_key
59
     * @param array                         $protected_headers
60
     * @param \Psr\Log\LoggerInterface|null $logger
61
     *
62
     * @return string
63
     */
64
    public static function createJWSWithDetachedPayloadToCompactJSON($payload, JWKInterface $signature_key, array $protected_headers, LoggerInterface $logger = null)
65
    {
66
        $jws = self::createJWSWithDetachedPayloadAndSign($payload, $signature_key, $protected_headers, [], $logger);
67
68
        return $jws->toCompactJSON(0);
69
    }
70
71
    /**
72
     * @param mixed                         $payload
73
     * @param \Jose\Object\JWKInterface     $signature_key
74
     * @param array                         $protected_headers
75
     * @param array                         $headers
76
     * @param \Psr\Log\LoggerInterface|null $logger
77
     *
78
     * @return string
79
     */
80
    public static function createJWSToFlattenedJSON($payload, JWKInterface $signature_key, array $protected_headers = [], $headers = [], LoggerInterface $logger = null)
81
    {
82
        $jws = self::createJWSAndSign($payload, $signature_key, $protected_headers, $headers, $logger);
83
84
        return $jws->toFlattenedJSON(0);
85
    }
86
87
    /**
88
     * @param mixed                         $payload
89
     * @param \Jose\Object\JWKInterface     $signature_key
90
     * @param array                         $protected_headers
91
     * @param array                         $headers
92
     * @param \Psr\Log\LoggerInterface|null $logger
93
     *
94
     * @return string
95
     */
96
    public static function createJWSWithDetachedPayloadToFlattenedJSON($payload, JWKInterface $signature_key, array $protected_headers = [], $headers = [], LoggerInterface $logger = null)
97
    {
98
        $jws = self::createJWSWithDetachedPayloadAndSign($payload, $signature_key, $protected_headers, $headers, $logger);
99
100
        return $jws->toFlattenedJSON(0);
101
    }
102
103
    /**
104
     * @param mixed                         $payload
105
     * @param \Jose\Object\JWKInterface     $signature_key
106
     * @param array                         $protected_headers
107
     * @param \Psr\Log\LoggerInterface|null $logger
108
     *
109
     * @return \Jose\Object\JWSInterface
110
     */
111
    private static function createJWSAndSign($payload, JWKInterface $signature_key, array $protected_headers = [], $headers = [], LoggerInterface $logger = null)
112
    {
113
        $jws = self::createJWS($payload);
114
115
        $jws = $jws->addSignatureInformation($signature_key, $protected_headers, $headers);
116
117
        $complete_headers = array_merge($protected_headers, $headers);
118
        Assertion::keyExists($complete_headers, 'alg', 'No "alg" parameter set in the header');
119
        $signer = Signer::createSigner([$complete_headers['alg']], $logger);
120
        $signer->sign($jws);
121
122
        return $jws;
123
    }
124
125
    /**
126
     * @param mixed                         $payload
127
     * @param \Jose\Object\JWKInterface     $signature_key
128
     * @param array                         $protected_headers
129
     * @param \Psr\Log\LoggerInterface|null $logger
130
     *
131
     * @return \Jose\Object\JWSInterface
132
     */
133
    private static function createJWSWithDetachedPayloadAndSign($payload, JWKInterface $signature_key, array $protected_headers = [], $headers = [], LoggerInterface $logger = null)
134
    {
135
        $jws = self::createJWS($payload, true);
136
137
        $jws = $jws->addSignatureInformation($signature_key, $protected_headers, $headers);
138
139
        $complete_headers = array_merge($protected_headers, $headers);
140
        Assertion::keyExists($complete_headers, 'alg', 'No "alg" parameter set in the header');
141
        $signer = Signer::createSigner([$complete_headers['alg']], $logger);
142
        $signer->sign($jws);
143
144
        return $jws;
145
    }
146
}
147