http: Support HTTP Basic Authentication
authorChrisludt <christian.ludt@isceco.admin.ch>
Tue, 8 Nov 2016 13:22:50 +0000 (13:22 +0000)
committerKunal Mehta <legoktm@member.fsf.org>
Thu, 1 Dec 2016 19:54:07 +0000 (11:54 -0800)
Adds two new options (username and password) to the
MWHttpRequest (and HTTP helper class) to enable
support for HTTP Basic Authentication on outgoing HTTP
connections.

Change-Id: If83f025bbe63769ba7bb4a824c5f12d5f1ec640a

includes/http/Http.php
includes/http/MWHttpRequest.php

index 43ae2d0..133d420 100644 (file)
@@ -51,6 +51,8 @@ class Http {
         *    - userAgent           A user agent, if you want to override the default
         *                          MediaWiki/$wgVersion
         *    - logger              A \Psr\Logger\LoggerInterface instance for debug logging
+        *    - username            Username for HTTP Basic Authentication
+        *    - password            Password for HTTP Basic Authentication
         * @param string $caller The method making this request, for profiling
         * @return string|bool (bool)false on failure or a string on success
         */
index 08883ae..9318605 100644 (file)
@@ -116,6 +116,12 @@ class MWHttpRequest implements LoggerAwareInterface {
                if ( isset( $options['userAgent'] ) ) {
                        $this->setUserAgent( $options['userAgent'] );
                }
+               if ( isset( $options['username'] ) && isset( $options['password'] ) ) {
+                       $this->setHeader(
+                               'Authorization',
+                               'Basic ' . base64_encode( $options['username'] . ':' . $options['password'] )
+                       );
+               }
 
                $members = [ "postData", "proxy", "noProxy", "sslVerifyHost", "caInfo",
                                "method", "followRedirects", "maxRedirects", "sslVerifyCert", "callback" ];