Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
AuthorizationServerFactory
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 3
20
0.00% covered (danger)
0.00%
0 / 1
 factory
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 getAuthorizationServer
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\OAuth;
4
5use InvalidArgumentException;
6use League\OAuth2\Server\AuthorizationServer;
7use MediaWiki\Extension\OAuth\Repository\AccessTokenRepository;
8use MediaWiki\Extension\OAuth\Repository\ClaimStore;
9use MediaWiki\Extension\OAuth\Repository\ClientRepository;
10use MediaWiki\Extension\OAuth\Repository\ScopeRepository;
11use MediaWiki\MediaWikiServices;
12
13class AuthorizationServerFactory {
14    /** @var string */
15    protected $privateKey;
16    /** @var string */
17    protected $encryptionKey;
18    /** @var string */
19    private $canonicalServer;
20
21    /**
22     * @return static
23     */
24    public static function factory() {
25        $services = MediaWikiServices::getInstance();
26        $extConfig = $services->getConfigFactory()->makeConfig( 'mwoauth' );
27        $mainConfig = $services->getMainConfig();
28        $privateKey = $extConfig->get( 'OAuth2PrivateKey' );
29        $encryptionKey = $extConfig->get( 'OAuthSecretKey' ) ?? $mainConfig->get( 'SecretKey' );
30        $canonicalServer = $mainConfig->get( 'CanonicalServer' );
31        return new static( $privateKey, $encryptionKey, $canonicalServer );
32    }
33
34    /**
35     * @param string $privateKey
36     * @param string $encryptionKey
37     * @param string $canonicalServer
38     */
39    public function __construct(
40        string $privateKey,
41        string $encryptionKey,
42        string $canonicalServer
43    ) {
44        $this->privateKey = $privateKey;
45        $this->encryptionKey = trim( $encryptionKey );
46
47        if ( $this->encryptionKey === '' ) {
48            // Empty encryption key would not break the workflow, but would cause security issues
49            throw new InvalidArgumentException( 'Encryption key must be set' );
50        }
51
52        $this->canonicalServer = $canonicalServer;
53    }
54
55    /**
56     * @return AuthorizationServer
57     */
58    public function getAuthorizationServer() {
59        return new AuthorizationServer(
60            new ClientRepository(),
61            new AccessTokenRepository( $this->canonicalServer ),
62            new ScopeRepository(),
63            $this->privateKey,
64            $this->encryptionKey,
65            null,
66            new ClaimStore()
67        );
68    }
69}