Completed
Push — master ( 16bc05...356550 )
by sebastian
03:03
created

src/utils/factories/BasicJWTFactory.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Copyright 2015 OpenStack Foundation
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 * http://www.apache.org/licenses/LICENSE-2.0
8
 * Unless required by applicable law or agreed to in writing, software
9
 * distributed under the License is distributed on an "AS IS" BASIS,
10
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
 * See the License for the specific language governing permissions and
12
 * limitations under the License.
13
 **/
14
15
namespace utils\factories;
16
17
use jwe\impl\JWEFactory;
18
use jwe\impl\specs\JWE_CompactFormatSpecification;
19
use jws\impl\specs\JWS_CompactFormatSpecification;
20
use jws\JWSFactory;
21
use jwt\IBasicJWT;
22
use jwk\exceptions\InvalidJWKType;
23
use jwt\impl\UnsecuredJWT;
24
use jwt\utils\JOSEHeaderSerializer;
25
use utils\exceptions\InvalidCompactSerializationException;
26
27
/**
28
 * Class BasicJWTFactory
29
 * @package utils\factories
30
 */
31
final class BasicJWTFactory
32
{
33
    /**
34
     * https://tools.ietf.org/html/rfc7516#section-9
35
     * @param string $compact_serialization
36
     * @return IBasicJWT
37
     * @throws InvalidJWKType
38
     * @throws InvalidCompactSerializationException
39
     */
40
    static public function build($compact_serialization)
0 ignored issues
show
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
41
    {
42
        $segments = explode(IBasicJWT::SegmentSeparator, $compact_serialization);
43
        // JWSs have three segments separated by two period ('.') characters.
44
        // JWEs have five segments separated by four period ('.') characters.
45
        switch(count($segments))
46
        {
47
            case 3:
48
                // JWS or unsecured one
49
                $header = JOSEHeaderSerializer::deserialize($segments[0]);
50
                if($header->getAlgorithm()->getString() === 'none' && empty($segments[2]))
51
                    return UnsecuredJWT::fromCompactSerialization($compact_serialization);
52
                return JWSFactory::build( new JWS_CompactFormatSpecification($compact_serialization) );
53
            break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
54
            case 5:
55
                // JWE
56
                return JWEFactory::build( new JWE_CompactFormatSpecification($compact_serialization) );
57
            break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
58
            default:
59
                throw new InvalidCompactSerializationException;
60
            break;
0 ignored issues
show
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
61
        }
62
        return null;
63
    }
64
}