Failed Conditions
Push — ng ( ede6c5...efffe8 )
by Florent
11:50
created

PromptParameterChecker::process()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 12
nc 10
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Component\Server\AuthorizationEndpoint\ParameterChecker;
15
16
use OAuth2Framework\Component\Server\AuthorizationEndpoint\Authorization;
17
use OAuth2Framework\Component\Server\AuthorizationEndpoint\Exception\OAuth2AuthorizationException;
18
use OAuth2Framework\Component\Server\Core\Exception\OAuth2Exception;
19
20
final class PromptParameterChecker implements ParameterChecker
21
{
22
    public const PROMPT_NONE = 'none';
23
24
    public const PROMPT_LOGIN = 'login';
25
26
    public const PROMPT_CONSENT = 'consent';
27
28
    public const PROMPT_SELECT_ACCOUNT = 'select_account';
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function check(Authorization $authorization): Authorization
34
    {
35
        try {
36
            if ($authorization->hasQueryParam('prompt')) {
37
                $prompt = $authorization->getPrompt();
38
                $diff = array_diff($prompt, $this->getAllowedPromptValues());
39
                if (!empty($diff)) {
40
                    throw new \InvalidArgumentException(sprintf('Invalid parameter "prompt". Allowed values are %s', implode(', ', $this->getAllowedPromptValues())));
41
                }
42
                if (in_array('none', $prompt) && 1 !== count($prompt)) {
43
                    throw new \InvalidArgumentException('Invalid parameter "prompt". Prompt value "none" must be used alone.');
44
                }
45
            }
46
47
            return $authorization;
48
        } catch (\InvalidArgumentException $e) {
49
            throw new OAuth2AuthorizationException(400, OAuth2Exception::ERROR_INVALID_REQUEST, $e->getMessage(), $authorization, $e);
50
        }
51
    }
52
53
    /**
54
     * @return string[]
55
     */
56
    private function getAllowedPromptValues(): array
57
    {
58
        return [
59
            self::PROMPT_NONE,
60
            self::PROMPT_LOGIN,
61
            self::PROMPT_CONSENT,
62
            self::PROMPT_SELECT_ACCOUNT,
63
        ];
64
    }
65
}
66