summaryrefslogtreecommitdiffstats
path: root/vendor/web-token/jwt-signature/Serializer/JSONFlattenedSerializer.php
blob: 0391c67e9a27a38a96242d110a678bbf299e449e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php

declare(strict_types=1);

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2018 Spomky-Labs
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

namespace Jose\Component\Signature\Serializer;

use Base64Url\Base64Url;
use Jose\Component\Core\Converter\JsonConverter;
use Jose\Component\Signature\JWS;

final class JSONFlattenedSerializer extends Serializer
{
    public const NAME = 'jws_json_flattened';

    /**
     * @var JsonConverter|\Jose\Component\Core\Util\JsonConverter|null
     */
    private $jsonConverter;

    /**
     * JSONFlattenedSerializer constructor.
     */
    public function __construct(?JsonConverter $jsonConverter = null)
    {
        $this->jsonConverter = $jsonConverter ?? new \Jose\Component\Core\Util\JsonConverter();
    }

    public function displayName(): string
    {
        return 'JWS JSON Flattened';
    }

    public function name(): string
    {
        return self::NAME;
    }

    public function serialize(JWS $jws, ?int $signatureIndex = null): string
    {
        if (null === $signatureIndex) {
            $signatureIndex = 0;
        }
        $signature = $jws->getSignature($signatureIndex);

        $data = [];
        $values = [
            'payload' => $jws->getEncodedPayload(),
            'protected' => $signature->getEncodedProtectedHeader(),
            'header' => $signature->getHeader(),
        ];

        foreach ($values as $key => $value) {
            if (!empty($value)) {
                $data[$key] = $value;
            }
        }
        $data['signature'] = Base64Url::encode($signature->getSignature());

        return $this->jsonConverter->encode($data);
    }

    public function unserialize(string $input): JWS
    {
        $data = $this->jsonConverter->decode($input);
        if (!\is_array($data) || !\array_key_exists('signature', $data)) {
            throw new \InvalidArgumentException('Unsupported input.');
        }

        $signature = Base64Url::decode($data['signature']);

        if (\array_key_exists('protected', $data)) {
            $encodedProtectedHeader = $data['protected'];
            $protectedHeader = $this->jsonConverter->decode(Base64Url::decode($data['protected']));
        } else {
            $encodedProtectedHeader = null;
            $protectedHeader = [];
        }
        if (\array_key_exists('header', $data)) {
            if (!\is_array($data['header'])) {
                throw new \InvalidArgumentException('Bad header.');
            }
            $header = $data['header'];
        } else {
            $header = [];
        }

        if (\array_key_exists('payload', $data)) {
            $encodedPayload = $data['payload'];
            $payload = $this->isPayloadEncoded($protectedHeader) ? Base64Url::decode($encodedPayload) : $encodedPayload;
        } else {
            $payload = null;
            $encodedPayload = null;
        }

        $jws = JWS::create($payload, $encodedPayload, null === $encodedPayload);
        $jws = $jws->addSignature($signature, $protectedHeader, $encodedProtectedHeader, $header);

        return $jws;
    }
}