首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MediaWiki Api + LDAPAuthentication

MediaWiki Api + LDAPAuthentication
EN

Stack Overflow用户
提问于 2013-06-19 05:45:47
回答 2查看 1.4K关注 0票数 2

我正在尝试实现帐户管理网站和MediaWiki站点之间的单点登录。

MediaWiki站点使用LDAP进行身份验证,将登录限制为管理员(以限制编辑、移动等管理权限,但5000用户需要登录帐户管理站点才能续订帐户、查看磁盘空间等。

到目前为止,我可以使用以下功能成功连接到Mediawiki,但无法使用用户名和密码进行身份验证:

代码语言:javascript
复制
function do_post_request($url, $data, $optional_headers = null)   {
   $params = array('http' => array(
                     'method' => 'POST',
                     'content' => $data));

   if ($optional_headers !== null) {
      $params['http']['header'] = $optional_headers;
   }

   $ctx = stream_context_create($params);

   if (!$fp = @fopen($url, 'r', false, $ctx)) return FALSE;
   $response = @stream_get_contents($fp);

   return $response;   
}

function mediawiki_login($username, $password) {
      /*
       *  STEP 1: request mediawiki login via api
       */

       $url='/wiki/api.php'; // EDIT THIS TO POINT TO YOUR WIKI API!
      $data=http_build_query(array('format'=>'json',
           'action'   =>'login',
           'lgname'   =>$username,
           'lgpassword'=>$password));
  $headers="Content-type: application/x-www-form-urlencoded\r\n".
        "Content-length: ".strlen($data)."\r\n".
        "Connection: close\r\n";

  $contents=do_post_request($url, $data, $headers);                  
  if ($contents===FALSE) return FALSE;

  $mwdata = json_decode($contents, true);    
  // check if the api answers as expected
  if($mwdata["login"]["result"] != "NeedToken"){
     return FALSE;         
  }

  $token=         $mwdata['login']['token'];
  $cookieprefix=   $mwdata['login']['cookieprefix'];
  $sessionid=      $mwdata['login']['sessionid'];

  /*
   *  STEP 2: send token using sessionid cookie
   */
  $data=http_build_query(array('format'=>'json',
                 'action' =>'login',
                 'lgname'   =>$username,
                 'lgpassword'=>$password,
                 'lgtoken' => $token));

  $headers="Content-type: application/x-www-form-urlencoded\r\n".
              "Content-length: ".strlen($data)."\r\n".
              "Cookie: ".$cookieprefix."_session=".$sessionid."\r\n".
              "Connection: close\r\n";

  $contents=do_post_request($url, $data, $headers);
  if ($contents===FALSE) return FALSE;

  $mwdata = json_decode($contents, true);
  if($mwdata["login"]["result"] != "Success") return FALSE;

  // login success, set the mediawiki cookies
  $cookieprefix=   $mwdata['login']['cookieprefix'];
  $sessionid=      $mwdata['login']['sessionid'];
  $userid=      $mwdata['login']['lguserid'];
  $username=      $mwdata['login']['lgusername'];
  setcookie($cookieprefix.'UserID', $userid, 0, '/', '.yourdomain.tld', FALSE, TRUE);     // INSERT YOUR DOMAIN
  setcookie($cookieprefix.'UserName', $username, 0, '/', '.yourdomain.tld', FALSE, TRUE);
  setcookie($cookieprefix.'_session', $sessionid, 0, '/', '.yourdomain.tld', FALSE, TRUE);

  return TRUE;
} 

在添加我自己的调试值之后,我发现在第二个do_post_request之后返回的是WrongPass,因为我们使用的是MediaWiki的LDAPAuthentication扩展,我相信该do_post_request没有使用该扩展进行身份验证,因此没有在MediaWiki使用的用户数据库中找到用户名密码组合

是否有人成功地使用LDAP在使用MediaWiki身份验证的同时对用户进行身份验证?

为了实现这一点,对上面的代码、LocalSettings.php或api.php做了哪些更改?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-28 02:02:03

在花了一周的时间研究这个问题后,我放弃了上面的选择。我创建了一个扩展来创建帐户管理网站的身份验证cookie,作为MediaWiki登录功能的一部分。我遇到的一个问题,其他人可能会发现有用的是,在使用'setcookie‘函数后,cookie实际上不会被设置,直到刷新页面,因为已经有html写入页面,所以为了验证目的,我必须为帐户管理网站编写新的身份验证代码,然后在管理员Mediawiki登录之前使用它作为过滤器。

票数 0
EN

Stack Overflow用户

发布于 2013-12-04 06:22:18

我正面临着一个类似的问题。解决方案是在我的帖子数据中包含一个额外的参数lgdomain,并将其设置为正确的LDAP域。

它在MediaWiki API documentation上被简单地提到过。看起来您的示例代码中缺少它。如果没有该参数,您将获得"WrongPass“作为登录结果。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17179556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档