【问题标题】:Replace implementation of an AuthPlugin with new AuthManager用新的 AuthManager 替换 AuthPlugin 的实现
【发布时间】:2017-05-19 10:44:13
【问题描述】:

我的问题,指南和文档有点复杂和令人困惑。 它仅设置为“实现 [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 建议了很多不同的类..

  • userExists() → PrimaryAuthenticationProvider::testUserExists()

  • authenticate() → PrimaryAuthenticationProvider::beginPrimaryAuthentication + PasswordAuthenticationRequest(如何将密码传递给进程?)

  • modifyUITemplate() → 来自 AuthenticationProvider 的 AuthenticationRequests(如何?) + AuthChangeFormFields 钩子。

  • autoCreate() 没有直接替换。 (AuthenticationResponse-> 从哪里来?只想强制自动创建)

  • strict() → 不要返回 ABSTAIN(我应该怎么做?不想进行本地身份验证)

  • 如何实例化我的类?与$wgAuth = new MyAuthPLugin() 被弃用的说文档。

或者当用户名,密码(哈希),sessionID和secretkey在html-Request中给出时,是否有一种简单的自动登录方法?

【问题讨论】:

    标签: php authentication mediawiki


    【解决方案1】:

    你需要:

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

    【讨论】:

    • 谢谢,这帮助我开始了。我如何实例化这个类? $wgAuth = new MyClass(); 有替代品吗?
    • 你不需要,你只是告诉 AuthManager 如何实例化它,通过Manual:$wgAuthManagerAutoConfig
    猜你喜欢
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 2020-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多