用新的 AuthManager 替换 AuthPlugin 的实现

Replace implementation of an AuthPlugin with new AuthManager

我的问题,指南和文档有点复杂和混乱。 它只设置了“实现一个[ClassName]”,但没有显示示例。

我给定的代码(必须重写的旧代码)如下:

class MyAuthPlugin extends AuthPlugin {

  protected $isAuthenticated = false;
           
  function modifyUITemplate( &$template ) {
      $template->set( 'usedomain', false );
      $template->set( 'useemail',  false ); 
      $template->set( 'canreset',  false );
      $template->set( 'create',    false );
  }


  function autoCreate() {
      return true;
  }

  function userExists( $username ) {  
      return true;  //already handled in ohter function
  }

  function strict() {   
      return true;  
  }

  /* Being called twice:
   * Login->attemptAutoCreate() (SpecialUserLogin.php) (only for new)
   * User->checkPassword() (User.php) external PW-authentication.  
   */   
  function authenticate( $username, $password ) {
      global $BBredirect, $BBconnection, $wgRequest;

      if($this->isAuthenticated) return true;
    
      $BBConnection['Parameters']  = 'cmd=authenticate&sessionId='.$wgRequest->getVal('sessionId');
    
      $myRequest = new SimpleHttpRequest($BBConnection);
      $responseGET = $myRequest->doRequest(SimpleHttpRequest::HTTP_GET);
      echo ($responseGET[Content]);
    
      $auth = simplexml_load_string($responseGET[Content]);

      if($auth->response->authentication ==  'false') {
          return false;
      }
        
      if($auth->response->authentication == 'ok') {
          $this->isAuthenticated = true;
      }
      return $this->isAuthenticated;
  }

  function isAuthenticated() {
      return $this->isAuthenticated;
  }

}

如何将此代码转换为新的 AuthManager 样式? 这个 guide 提出了很多不同的 classes..

或者在 html-请求中提供用户名、密码(哈希)、sessionID 和密钥时,是否有一种简单的自动登录方法?

您需要:

  • 创建一个 AbstractPrimaryAuthenticationProvider 子类
  • 实施getAuthenticationRequests;您可能想要 return 在 ACTION_LOGIN 上创建一个新的 PasswordAuthenticationRequest 对象,除此之外别无他法(因为您不想支持创建 BB 用户/通过 wiki 更改他们的密码)。
  • 实施beginPrimaryAuthentication;它将与旧的 authenticate 大致相同,除了您将凭据作为 AuthenticationRequest 对象的数组并使用 AuthenticationRequest::getRequestByClass( $reqs, PasswordAuthenticationRequest::class ) 获得正确的凭据,然后 return AuthenticationResponse::newXXX 表示结果(newPassnewFail - 不弃权,因为您不想支持退回到另一种身份验证方法)。如果用户在本地不存在,则自动创建。
  • 实施testUserExists。这应该在 BB 数据库中查找用户。 (只要一直 return 为 true 或 false 也可能会起作用 - 这主要是为了让提供者可以保留名称并防止其他提供者使用它。)
  • 实施 beginPrimaryAccountCreationaccountCreationType。由于您可能不想通过 wiki 支持创建 BB 帐户,因此只需分别 return AuthenticationResponse::newFailTYPE_NONE
  • 实施 providerAllowsAuthenticationDataChangeproviderChangeAuthenticationData。第一个应该 return 在收到密码身份验证请求时出错,否则忽略它。第二个应该是空的。