Skip to content

Commit 65cd6df

Browse files
authored
Merge pull request #2 from toban/main
Some fixes for running in local cluster
2 parents f657632 + 6cdbb8f commit 65cd6df

2 files changed

Lines changed: 40 additions & 17 deletions

File tree

public_html/php/WbstackMagnusOauth.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
class WbstackMagnusOauth {
44

5+
public const platformIngressHostAndPort = "platform-nginx.default.svc.cluster.local:8080";
6+
57
/**
68
* @var bool
79
*/
@@ -156,6 +158,10 @@ public static function getOauthParams(
156158
return $params;
157159
}
158160

161+
public static function isLocalHost(): bool {
162+
return substr($_SERVER['SERVER_NAME'], -10, 10) === '.localhost';
163+
}
164+
159165
/**
160166
* @param string $toolUrlTail Example: "/tools/widar"
161167
* @return mixed
@@ -165,37 +171,29 @@ public static function getSite(
165171
) {
166172
// XXX: this same logic is in quickstatements.php and platform api WikiController backend
167173
$domain = $_SERVER['SERVER_NAME'];
168-
if ( substr($domain,-10, 10) === '.localhost' ){
169-
// XXX: this wont actually work on localhost currently as this is talking to mediawiki via
170-
// the public hostname which doesnt exist.
171-
// If we wanted this to work we would have to also add a way for the host header to be sent with
172-
// the requests...
173-
174-
die('Will not currently work for localhost dev...');
174+
if ( self::isLocalHost() ){
175175

176176
// localhost development, with a full domain prefixing .localhost
177177
// eg. wiki.addshore.com.localhost
178-
$wbRoot = $domain . ":8083";
179-
$toolRoot = $domain . ":8086";
178+
$wbRoot = $domain;
179+
$toolRoot = 'http://' . $domain . $toolUrlTail;
180180

181181
// Directly for config
182-
$publicMwOAuthUrl = $wbRoot . '/w/index.php?title=Special:OAuth';
183-
$mwOAuthUrl = $wbRoot . '/w/index.php?title=Special:OAuth';
182+
$publicMwOAuthUrl = 'http://' . $domain . '/w/index.php?title=Special:OAuth';
183+
$mwOAuthUrl = 'http://' . self::platformIngressHostAndPort . '/w/index.php?title=Special:OAuth';
184184
$wbPublicHostAndPort = $wbRoot;
185-
$wbApi = $wbRoot . '/w/api.php';
185+
$wbApi = 'http://' . self::platformIngressHostAndPort . '/w/api.php';
186186
$wbPageBase = $wbRoot . '/wiki/';
187187
$toolbase = $toolRoot;
188-
} else if ( $domain === 'localhost' ) {
189-
die('Should be accessed with a subdomain of localhost..');
190188
} else {
191189
$wbRoot = $domain;
192190
$toolRoot = $domain . $toolUrlTail;
193191

194192
// Directly for config
195-
$publicMwOAuthUrl = 'https://' . $wbRoot . '/w/index.php?title=Special:OAuth';
193+
$publicMwOAuthUrl = 'https://' . $wbRoot . '/w/index.php?title=Special:OAuth'; // TODO this could use the internal network
196194
$mwOAuthUrl = 'https://' . $wbRoot . '/w/index.php?title=Special:OAuth';
197195
$wbPublicHostAndPort = $wbRoot;
198-
$wbApi = 'https://' . $wbRoot . '/w/api.php';
196+
$wbApi = 'https://' . $wbRoot . '/w/api.php'; // TODO this could use the internal network
199197
$wbPageBase = 'https://' . $wbRoot . '/wiki/';
200198
$toolbase = 'https://' . $toolRoot;
201199
}

public_html/php/oauth.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ function fetchAccessToken() {
149149
curl_setopt( $ch, CURLOPT_URL, $url );
150150
//curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
151151
curl_setopt( $ch, CURLOPT_USERAGENT, $this->gUserAgent );
152+
152153
curl_setopt( $ch, CURLOPT_HEADER, 0 );
154+
$this->setCurlHttpHeaders( $ch );
155+
153156
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
154157
$data = curl_exec( $ch );
155158

@@ -200,6 +203,7 @@ function fetchAccessToken() {
200203
function sign_request( $method, $url, $params = [] ) {
201204
// global $gConsumerSecret, $gTokenSecret;
202205

206+
$url = str_replace( WbstackMagnusOauth::platformIngressHostAndPort, $_SERVER['SERVER_NAME'], $url );
203207
$parts = parse_url( $url );
204208

205209
// We need to normalize the endpoint URL
@@ -238,6 +242,24 @@ function sign_request( $method, $url, $params = [] ) {
238242
return base64_encode( hash_hmac( 'sha1', $toSign, $key, true ) );
239243
}
240244

245+
/**
246+
* Set the HTTP Headers for the curl handle
247+
*
248+
* Sets the HOST parameter when internally talking to wbstack platform ingress
249+
*
250+
*/
251+
function setCurlHttpHeaders( $curlHandle, $headers = [] ) {
252+
253+
if( WbstackMagnusOauth::isLocalHost() ) {
254+
$domain = $_SERVER['SERVER_NAME'];
255+
$headers[] = 'HOST: ' . $domain;
256+
}
257+
258+
if( !empty($headers) ) {
259+
curl_setopt( $curlHandle, CURLOPT_HTTPHEADER, $headers );
260+
}
261+
}
262+
241263
/**
242264
* Request authorization
243265
* @return void
@@ -270,6 +292,7 @@ function doAuthorizationRedirect($callback='') {
270292
//curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
271293
curl_setopt( $ch, CURLOPT_USERAGENT, $this->gUserAgent );
272294
curl_setopt( $ch, CURLOPT_HEADER, 0 );
295+
$this->setCurlHttpHeaders( $ch );
273296
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
274297
$data = curl_exec( $ch );
275298
if ( !$data ) {
@@ -482,10 +505,12 @@ function doApiQuery( $post, &$ch = null , $mode = '' , $iterations_left = 5 , $l
482505
curl_setopt( $ch, CURLOPT_POST, true );
483506
curl_setopt( $ch, CURLOPT_URL, $url );
484507
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_fields );
485-
curl_setopt( $ch, CURLOPT_HTTPHEADER, [ $header ] );
486508
//curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
487509
curl_setopt( $ch, CURLOPT_USERAGENT, $this->gUserAgent );
510+
488511
curl_setopt( $ch, CURLOPT_HEADER, 0 );
512+
$this->setCurlHttpHeaders( $ch, [ $header ] );
513+
489514
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
490515

491516
$data = curl_exec( $ch );

0 commit comments

Comments
 (0)