summaryrefslogtreecommitdiff
blob: 36abbb4c7f2db0b7f15949c78db9d4e248f044dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php

namespace MediaWiki\Extensions\OAuth;

use InvalidArgumentException;
use League\OAuth2\Server\AuthorizationServer;
use MediaWiki\Extensions\OAuth\Repository\AccessTokenRepository;
use MediaWiki\Extensions\OAuth\Repository\ClientRepository;
use MediaWiki\Extensions\OAuth\Repository\ScopeRepository;
use MediaWiki\MediaWikiServices;

class AuthorizationServerFactory {
	/** @var string */
	protected $privateKey;
	/** @var string */
	protected $encryptionKey;

	/**
	 * @return static
	 */
	public static function factory() {
		$services = MediaWikiServices::getInstance();
		$extConfig = $services->getConfigFactory()->makeConfig( 'mwoauth' );
		$mainConfig = $services->getMainConfig();
		$privateKey = $extConfig->get( 'OAuth2PrivateKey' );
		$encryptionKey = $extConfig->get( 'OAuthSecretKey' ) ?? $mainConfig->get( 'SecretKey' );

		return new static( $privateKey, $encryptionKey );
	}

	/**
	 * @param string $privateKey
	 * @param string $encryptionKey
	 */
	public function __construct( $privateKey, $encryptionKey ) {
		$this->privateKey = $privateKey;
		$this->encryptionKey = trim( $encryptionKey );

		if ( empty( $this->encryptionKey ) ) {
			// Empty encryption key would not break the workflow, but would cause security issues
			throw new InvalidArgumentException( 'Encryption key must be set' );
		}
	}

	/**
	 * @return AuthorizationServer
	 */
	public function getAuthorizationServer() {
		return new AuthorizationServer(
			new ClientRepository(),
			new AccessTokenRepository(),
			new ScopeRepository(),
			$this->privateKey,
			$this->encryptionKey
		);
	}
}