summaryrefslogtreecommitdiff
blob: 177fbaff109b136256a5cb1d0cf6812033f28668 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php

namespace MediaWiki\Extensions\OAuth\AuthorizationProvider;

use Config;
use DateInterval;
use Exception;
use League\OAuth2\Server\AuthorizationServer;
use League\OAuth2\Server\Grant\GrantTypeInterface;
use MediaWiki\Extensions\OAuth\AuthorizationServerFactory;
use MediaWiki\Extensions\OAuth\Repository\AuthCodeRepository;
use MediaWiki\Extensions\OAuth\Repository\RefreshTokenRepository;
use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use User;

abstract class AuthorizationProvider implements IAuthorizationProvider {
	/**
	 * @var AuthorizationServer
	 */
	protected $server;

	/**
	 * @var Config|null
	 */
	protected $config;

	/**
	 * @var User
	 */
	protected $user;

	/**
	 * @var LoggerInterface
	 */
	protected $logger;

	/**
	 * @var GrantTypeInterface
	 */
	protected $grant;

	/**
	 * @return AuthorizationProvider
	 * @throws Exception
	 */
	public static function factory() {
		$services = MediaWikiServices::getInstance();
		$config = $services->getConfigFactory()->makeConfig( 'mwoauth' );
		$serverFactory = AuthorizationServerFactory::factory();
		$logger = LoggerFactory::getInstance( 'OAuth' );

		// @phan-suppress-next-line PhanTypeInstantiateAbstractStatic
		return new static( $config, $serverFactory->getAuthorizationServer(), $logger );
	}

	/**
	 * @param Config $config
	 * @param AuthorizationServer $server
	 * @param LoggerInterface $logger
	 * @throws Exception
	 */
	public function __construct( $config, $server, $logger ) {
		$this->config = $config;
		$this->server = $server;
		$this->logger = $logger;

		$this->decorateAuthServer();
	}

	/**
	 * @inheritDoc
	 */
	public function setUser( User $user ) {
		$this->user = $user;
	}

	/**
	 * @inheritDoc
	 */
	public function needsUserApproval() {
		return false;
	}

	/**
	 * @return GrantTypeInterface
	 */
	abstract protected function getGrant() : GrantTypeInterface;

	/**
	 * @return GrantTypeInterface
	 */
	protected function getGrantSingleton() {
		if ( !$this->grant ) {
			$this->grant = $this->getGrant();
		}

		return $this->grant;
	}

	/**
	 * @throws Exception
	 */
	protected function decorateAuthServer() {
		$grant = $this->getGrantSingleton();
		$grant->setRefreshTokenTTL( $this->getRefreshTokenTTL() );
		$this->server->setDefaultScope( '#default' );
		$this->server->enableGrantType(
			$grant,
			$this->getGrantExpirationInterval()
		);
	}

	/**
	 * @return RefreshTokenRepository
	 */
	protected function getRefreshTokenRepo() {
		/** @var RefreshTokenRepository $repo */
		$repo = RefreshTokenRepository::factory();
		return $repo;
	}

	/**
	 * @return AuthCodeRepository
	 */
	protected function getAuthCodeRepo() {
		/** @var AuthCodeRepository $repo */
		$repo = AuthCodeRepository::factory();
		return $repo;
	}

	/**
	 * @return DateInterval
	 * @throws Exception
	 */
	protected function getGrantExpirationInterval() {
		$intervalSpec = 'PT1H';
		if ( $this->config->has( 'OAuth2GrantExpirationInterval' ) ) {
			$intervalSpec = $this->parseExpiration( $this->config->get( 'OAuth2GrantExpirationInterval' ) );
		}
		return new DateInterval( $intervalSpec );
	}

	/**
	 * @return DateInterval
	 * @throws Exception
	 */
	protected function getRefreshTokenTTL() {
		$intervalSpec = 'PT1M';
		if ( $this->config->has( 'OAuth2RefreshTokenTTL' ) ) {
			$intervalSpec = $this->parseExpiration( $this->config->get( 'OAuth2RefreshTokenTTL' ) );
		}

		return new DateInterval( $intervalSpec );
	}

	/**
	 * @param ServerRequestInterface $request
	 * @param string $default
	 * @return mixed|string
	 */
	protected function getClientIdFromRequest( ServerRequestInterface $request, $default = '' ) {
		$params = (array)$request->getParsedBody();

		return $params['client_id'] ?? $default;
	}

	private function parseExpiration( $expiration ) {
		if ( $expiration === false || $expiration === 'infinity' ) {
			// Effectively non-expiring tokens
			$expiration = 'P292277000000Y';
		}

		return $expiration;
	}
}