如何使用OAuth2

时间:2015-06-29 22:01:14

标签: php oauth-2.0 api-design

我想为已有大型数据库(包括用户表)的现有Web应用程序开发API。内容检索端点的OAuth2.0实现已经适用于范围和我需要的一切。

不幸的是,到目前为止我还无法掌握如何实现登录。 API端点可以工作,但现在我也想在这里实现OAuth2。据我所知,我正在使用的库的示例,它提供了自己的用户表。 https://bshaffer.github.io/oauth2-server-php-docs/grant-types/user-credentials/

到目前为止我的代码看起来像这样:

$app->put('/login', function() use ($app) {
    global $feedback, $sess_id, $server, $response;

    $request = OAuth2\Request::createFromGlobals();
    $response = new OAuth2\Response();
    $scopeRequired = 'UserAccess'; 

    if (!$server->verifyResourceRequest($request, $response, $scopeRequired)) {
      // if the scope required is different from what the token allows, this will send a "401 insufficient_scope" error
      $response->send();
      die;
    }

    // check for required params
    verifyRequiredParams(array('un', 'pw', 'cc'));

    // reading post params
    $user_name          = $app->request()->put('un');
    $password           = $app->request()->put('pw');
    $area_provided['cc']= $app->request()->put('cc');
    $response = array();

    user_login($user_name,$password, true, $area_provided);
    $userdata = get_userdata_2();

    if (isset($feedback) AND $feedback == 'ok'){
        $response["error"]      = false;
        // $response['user_name']  = $userdata['user_name'];
        // $response['email']      = $userdata['email'];
        // $response['apiKey']     = $userdata['api_key'];
        $response['sess_id']    = $sess_id;
    }elseif ($feedback == 'max_login_tries'){ // client login
        $response['error'] = true;
        $response['message'] = "Maximale Anzahl von Versuchen ereicht";  
    }
    elseif($feedback == 'nopass'){
        $response['error'] = true;
        $response['message'] = "Bitte geben Sie ihr Passwort an";  
    }
    elseif($feedback == 'user_unknown'){
        $response['error'] = true;
        $response['message'] = "Dieser Benutzername ist uns nicht bekannt";  
    }
    elseif($feedback == 'wrong_password'){
        $response['error'] = true;
        $response['message'] = "Das Passwort ist falsch";  
    }
    elseif($feedback == 'deactivated'){
        $response['error'] = true;
        $response['message'] = "Ihr Benutzerkonto wurde deaktiviert";  
    }
    elseif($feedback == 'wrong_country'){
        $response['error'] = true;
        $response['message'] = "Ihr Benutzerkonto gilt für ein anderes Land";  
    }
    else{ // other
        $response['error'] = true;
        $response['message'] = "Unbekannter Fehler";  
    }

    echoRespnse(200, $response);
});  

我怎么可能

  1. 将我现有的用户表与此概念一起使用?
  2. 检查图示的验证,然后返回访问代码
  3. 感谢您对此有任何帮助!

0 个答案:

没有答案