-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tuleap.php
201 lines (179 loc) · 4.95 KB
/
Tuleap.php
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
namespace TuleapIntegration\Provider;
use Config;
use Exception;
use Firebase\JWT\JWK;
use Firebase\JWT\JWT;
use League\OAuth2\Client\OptionProvider\HttpBasicAuthOptionProvider;
use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Token\AccessToken;
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
use MediaWiki\Session\Session;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use TuleapIntegration\TuleapResourceOwner;
class Tuleap extends AbstractProvider {
use BearerAuthorizationTrait;
/** @var Config */
private $config;
/** @var LoggerInterface */
private $logger;
/** @var Session */
private $session;
/**
* @param Config $config
* @param LoggerInterface $logger
*/
public function __construct( Config $config, LoggerInterface $logger ) {
$this->config = $config;
$this->logger = $logger;
parent::__construct( $config->get( 'TuleapOAuth2Config' ), [
'optionProvider' => new HttpBasicAuthOptionProvider()
] );
}
/**
* @param Session $session
*/
public function setSession( Session $session ) {
$this->session = $session;
}
/**
* @return string
* @throws Exception
*/
public function getBaseAuthorizationUrl() {
return $this->compileUrl( '/mediawiki_standalone/oauth2_authorize' );
}
/**
* @param array $params
* @return string
* @throws Exception
*/
public function getBaseAccessTokenUrl( array $params ) {
return $this->compileUrl( '/oauth2/token' );
}
/**
* @param AccessToken $token
* @return string
* @throws Exception
*/
public function getResourceOwnerDetailsUrl( AccessToken $token ) {
return $this->compileUrl( '/oauth2/userinfo' );
}
/**
* @return string[]
*/
protected function getDefaultScopes() {
return [
"openid",
"email",
"profile",
"read:project",
"read:mediawiki_standalone"
];
}
/**
* @param ResponseInterface $response
* @param array|string $data
* @throws Exception
*/
protected function checkResponse( ResponseInterface $response, $data ) {
if ( isset( $data['id_token'] ) ) {
$this->verifyJWT( $data['id_token'] );
}
if ( $response->getStatusCode() !== 200 ) {
$this->logger->error( 'Response verification failed: {code} {reason}', [
'code' => $response->getStatusCode(),
'reason' => $response->getReasonPhrase(),
] );
throw new Exception( "Invalid response from Tuleap authentication" );
}
}
/**
* @param string $token
* @throws \SodiumException
*/
private function verifyJWT( $token ) {
JWT::$leeway = 10;
// Checks validity period, signature
$d = JWT::decode( $token, $this->getJWTKeys() );
if ( $d->iss !== $this->getBaseUrl() ) {
$this->logger->error( 'Verify JWT: iss not valid: iss={iss} expected={base}', [
'iss' => $d->iss,
'base' => $this->getBaseUrl(),
] );
throw new Exception( 'Verify JWT: iss not valid' );
}
$clientId = $this->config->get( 'TuleapOAuth2Config' )['clientId'] ?? null;
if ( $d->aud !== $clientId ) {
$this->logger->error( 'Verify JWT: iss not valid: aud={aud} expected={client}', [
'aud' => $d->aud,
'client' => $clientId,
] );
throw new Exception( 'Verify JWT: aud not valid' );
}
if ( !property_exists( $d, 'sub' ) ) {
$this->logger->error( 'Verify JWT: sub claim is missing' );
throw new Exception( 'Verify JWT: sub claim missing' );
}
$nonce = $d->nonce ?? null;
if ( !$this->session ) {
throw new Exception( 'Session must be set before obtaining AccessToken' );
}
$storedNonce = $this->session->get( 'tuleapOauth2nonce' );
if ( !$nonce || !hash_equals( $nonce, $storedNonce ) ) {
throw new Exception( 'Verify JWT: nonce does not match' );
}
}
/**
* @return array
*/
private function getJWTKeys() {
$keyResponse = $this->getResponse(
$this->getRequest( 'GET', $this->compileUrl( '/oauth2/jwks' ) )
);
if ( $keyResponse->getStatusCode() !== 200 ) {
$this->logger->error( 'Failed to retrive JWKS: {code} {reason}', [
'code' => $keyResponse->getStatusCode(),
'reason' => $keyResponse->getReasonPhrase(),
] );
throw new Exception( "Could not retrieve JWT public key" );
}
$keys = json_decode( $keyResponse->getBody(), 1 );
return JWK::parseKeySet( $keys );
}
/**
* @param array $response
* @param AccessToken $token
* @return TuleapResourceOwner
*/
protected function createResourceOwner( array $response, AccessToken $token ) {
return TuleapResourceOwner::factory( $response );
}
/**
* @return string
* @throws Exception
*/
private function getBaseUrl() {
$url = $this->config->get( 'TuleapUrl' );
if ( !$url ) {
$this->logger->error( 'Config variable \$wgTuleapUrl must be set' );
throw new Exception( 'Config variable \$wgTuleapUrl must be set' );
}
return rtrim( $url, '/' );
}
/**
* @param string $path
* @return string
* @throws Exception
*/
public function compileUrl( $path ) {
return $this->getBaseUrl() . $path;
}
/**
* @return Config
*/
public function getConfig() {
return $this->config;
}
}