Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
SignatureMethod
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
0.00% covered (danger)
0.00%
0 / 1
 getName
n/a
0 / 0
n/a
0 / 0
0
 buildSignature
n/a
0 / 0
n/a
0 / 0
0
 checkSignature
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2declare( strict_types = 1 );
3
4/**
5 * @section LICENSE
6 * Copyright (c) 2007 Andy Smith
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including without
11 * limitation the rights to use, copy, modify, merge, publish, distribute,
12 * sublicense, and/or sell copies of the Software, and to permit persons to
13 * whom the Software is furnished to do so, subject to the following
14 * conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 *
27 * @file
28 */
29
30namespace MediaWiki\OAuthClient;
31
32/**
33 * A class for implementing a Signature Method
34 * See section 9 ("Signing Requests") in the spec
35 */
36abstract class SignatureMethod {
37    /**
38     * Needs to return the name of the Signature Method (ie HMAC-SHA1)
39     * @return string
40     */
41    abstract public function getName();
42
43    /**
44     * Build up the signature
45     * NOTE: The output of this function MUST NOT be urlencoded.
46     * the encoding is handled in Request when the final request
47     * is serialized
48     * @param Request $request
49     * @param Consumer $consumer
50     * @param Token|null $token
51     * @return string
52     */
53    abstract public function buildSignature(
54        Request $request,
55        Consumer $consumer,
56        ?Token $token = null
57    );
58
59    /**
60     * Verifies that a given signature is correct
61     * @param Request $request
62     * @param Consumer $consumer
63     * @param Token|null $token
64     * @param string $signature
65     * @return bool
66     */
67    public function checkSignature(
68        Request $request,
69        Consumer $consumer,
70        /*Token*/ $token,
71        $signature
72    ) {
73        $built = $this->buildSignature( $request, $consumer, $token );
74
75        // Check for zero length, although unlikely here
76        if ( strlen( $built ) === 0 || strlen( $signature ) === 0 ) {
77            return false;
78        }
79
80        if ( strlen( $built ) !== strlen( $signature ) ) {
81            return false;
82        }
83
84        // Avoid a timing leak with a (hopefully) time insensitive compare
85        $result = 0;
86        $len = strlen( $signature );
87        for ( $i = 0; $i < $len; $i++ ) {
88            $result |= ord( $built[$i] ) ^ ord( $signature[$i] );
89        }
90
91        return $result == 0;
92    }
93}