Completed
Push — master ( fe4c2e...b05117 )
by Michael
12s
created

TokenReader   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 20.78 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 13.03%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 8
c 1
b 1
f 0
lcom 1
cbo 3
dl 16
loc 77
rs 10
ccs 3
cts 23
cp 0.1303

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fromString() 0 5 1
A fromCookie() 8 8 2
A fromRequest() 8 8 2
A fromHeader() 0 14 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
namespace Xmf\Jwt;
13
14
use Xmf\Request;
15
16
/**
17
 * Validate and get payload from a token string
18
 *
19
 * @category  Xmf\Jwt\TokenReader
20
 * @package   Xmf
21
 * @author    Richard Griffith <[email protected]>
22
 * @copyright 2016 XOOPS Project (http://xoops.org)
23
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
24
 * @version   Release: 1.0
25
 * @link      http://xoops.org
26
 */
27
class TokenReader
28
{
29
    /**
30
     * Validate and decode a JSON Web Token string
31
     *
32
     * @param string             $keyName      name of the key to used to sign the token
33
     * @param string             $token        the token string to validate and decode
34
     * @param array|\Traversable $assertClaims traversable set of claims, claim => value, to assert
35
     *
36
     * @return object|false payload as stdClass, or false if token was invalid
37
     */
38 1
    public static function fromString($keyName, $token, $assertClaims = array())
39
    {
40 1
        $jwt = new JsonWebToken(KeyFactory::build($keyName));
41 1
        return $jwt->decode($token, $assertClaims);
42
    }
43
44
    /**
45
     * Validate and decode a JSON Web Token string from a cookie
46
     *
47
     * @param string             $keyName      name of the key to used to sign the token
48
     * @param string             $cookieName   name of cookie that sources the token
49
     * @param array|\Traversable $assertClaims traversable set of claims, claim => value, to assert
50
     *
51
     * @return object|false payload as stdClass, or false if token was invalid
52
     */
53 View Code Duplication
    public static function fromCookie($keyName, $cookieName, $assertClaims = array())
54
    {
55
        $token = Request::getString($cookieName, '', 'COOKIE');
56
        if (empty($token)) {
57
            return false;
58
        }
59
        return static::fromString($keyName, $token, $assertClaims);
60
    }
61
62
    /**
63
     * Validate and decode a JSON Web Token string from a request (i.e. POST body)
64
     *
65
     * @param string             $keyName       name of the key to used to sign the token
66
     * @param string             $attributeName name of cookie that sources the token
67
     * @param array|\Traversable $assertClaims  traversable set of claims, claim => value, to assert
68
     *
69
     * @return object|false payload as stdClass, or false if token was invalid
70
     */
71 View Code Duplication
    public static function fromRequest($keyName, $attributeName, $assertClaims = array())
72
    {
73
        $token = Request::getString($attributeName, '');
74
        if (empty($token)) {
75
            return false;
76
        }
77
        return static::fromString($keyName, $token, $assertClaims);
78
    }
79
80
    /**
81
     * Validate and decode a JSON Web Token string from a header
82
     *
83
     * @param string             $keyName      name of the key to used to sign the token
84
     * @param array|\Traversable $assertClaims traversable set of claims, claim => value, to assert
85
     * @param string             $headerName   name of header that sources the token
86
     *
87
     * @return object|false payload as stdClass, or false if token was invalid
88
     */
89
    public static function fromHeader($keyName, $assertClaims = array(), $headerName = 'Authorization')
90
    {
91
        $header = Request::getHeader($headerName, '');
92
        if (empty($header)) {
93
            return false;
94
        }
95
        $header = trim($header);
96
        $space = strpos($header, ' '); // expecting "Bearer base64-token-string"
97
        if (false !== $space) {
98
            $header = substr($header, $space);
99
        }
100
        $token = trim($header);
101
        return static::fromString($keyName, $token, $assertClaims);
102
    }
103
}
104