Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
MWOAuthSignatureMethod_RSA_SHA1
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 3
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
 fetch_public_cert
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 fetch_private_cert
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace MediaWiki\Extension\OAuth\Backend;
4
5use MediaWiki\Extension\OAuth\Lib\OAuthDataStore;
6use MediaWiki\Extension\OAuth\Lib\OAuthException;
7use MediaWiki\Extension\OAuth\Lib\OAuthRequest;
8use MediaWiki\Extension\OAuth\Lib\OAuthSignatureMethod_RSA_SHA1;
9
10class MWOAuthSignatureMethod_RSA_SHA1 extends OAuthSignatureMethod_RSA_SHA1 {
11    /** @var MWOAuthDataStore */
12    protected $store;
13    /** @var string PEM encoded RSA private key */
14    private $privateKey;
15
16    /**
17     * @param OAuthDataStore $store
18     * @param string|null $privateKey RSA private key, passed to openssl_get_privatekey
19     */
20    public function __construct( OAuthDataStore $store, $privateKey = null ) {
21        $this->store = $store;
22        $this->privateKey = $privateKey;
23
24        if ( $privateKey !== null ) {
25            $key = openssl_pkey_get_private( $privateKey );
26            if ( !$key ) {
27                throw new OAuthException( "Invalid private key given" );
28            }
29            $details = openssl_pkey_get_details( $key );
30            if ( $details['type'] !== OPENSSL_KEYTYPE_RSA ) {
31                throw new OAuthException( "Key is not an RSA key" );
32            }
33            openssl_pkey_free( $key );
34        }
35    }
36
37    /**
38     * Get the public certificate, used to verify the request. In our case, we get
39     * the Consumer's key, and lookup the registered cert from the datastore.
40     * @param OAuthRequest &$request request recieved by the server, that we're going to verify
41     * @return string representing the public certificate
42     */
43    protected function fetch_public_cert( &$request ) {
44        $consumerKey = $request->get_parameter( 'oauth_consumer_key' );
45        return $this->store->getRSAKey( $consumerKey );
46    }
47
48    /**
49     * If you want to reuse this code to write your Consumer, implement
50     * this function to get your private key, so you can sign the request.
51     * @param OAuthRequest &$request
52     * @return string
53     * @throws OAuthException
54     */
55    protected function fetch_private_cert( &$request ) {
56        if ( $this->privateKey === null ) {
57            throw new OAuthException( "No private key was set" );
58        }
59        return $this->privateKey;
60    }
61}