微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

初始化后向请求对象添加额外数据

如何解决初始化后向请求对象添加额外数据

以下公共函数根据用户名和密码返回 oauth 令牌。但是,我有一个要求,必须首先从电子邮件 ID 查询用户名。在函数的第一部分,我需要以某种方式将用户名添加到请求对象中。该请求是根据我的理解创建的 using laminas

函数taken is here 的完整代码

  /**
   * Processes POST requests to /oauth/token.
   */
  public function token(ServerRequestInterface $request) {

    ////////////////
    //////////////// 
    // ADD LOGIC TO GET EMAIL FROM REQUEST & GET USERNAME
    // ADD USERNAME TO $request
    ////////////////
    ////////////////
    
    //Extract the grant type from the request body.
    $body = $request->getParsedBody();
    $grant_type_id = !empty($body['grant_type']) ? $body['grant_type'] : 'implicit';
    $client_drupal_entity = NULL;
    if (!empty($body['client_id'])) {
      $consumer_storage = $this->entityTypeManager()->getStorage('consumer');
      $client_drupal_entities = $consumer_storage
        ->loadByProperties([
          'uuid' => $body['client_id'],]);
      if (empty($client_drupal_entities)) {
        return OAuthServerException::invalidClient($request)
          ->generateHttpResponse(new Response());
      }
      $client_drupal_entity = reset($client_drupal_entities);
    }
    // Get the auth server object from that uses the League library.
    try {
      // Respond to the incoming request and fill in the response.
      $auth_server = $this->grantManager->getAuthorizationServer($grant_type_id,$client_drupal_entity);
      $response = $this->handletoken($request,$auth_server);
    }
    catch (OAuthServerException $exception) {
      watchdog_exception('simple_oauth',$exception);
      $response = $exception->generateHttpResponse(new Response());
    }
    return $response;
  }

请求作为表单数据发送: 请参阅下面的示例 js 代码: (接受用户名添加电子邮件参数以演示所需内容

var formdata = new FormData();
formdata.append("grant_type","password");
formdata.append("client_id","828472a8-xxxx-xxxx-xxx-ab041d3b313a");
formdata.append("client_secret","secret-xxx-xxx-xxx");
//formdata.append("username","username");
formdata.append("email","email@email.com");
formdata.append("password","password");

var requestOptions = {
  method: 'POST',body: formdata,redirect: 'follow'
};

fetch("{{base_url}}oauth/token",requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error',error));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。