doushui20090526 2015-06-29 22:01
浏览 165

如何使用OAuth2实现登录API端点

I want to develop an API for an existing web application that has already a large database including user table. OAuth2.0 implementation for content retrieval end points already works with scope and everything I need.

Unfortunatelly I could not grasp so far how to implement login though. The API Endpoint works, but now I want to implement OAuth2 here as well. As far as I can see the example of the library I am using, it offers its own user table. https://bshaffer.github.io/oauth2-server-php-docs/grant-types/user-credentials/

My code so far looks like this:

$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);
});  

How could I

  1. Use my existing user table with this concept?
  2. Check the illustrated validation and then return the access code

Thank you for any help on this!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c程序不知道为什么得不到结果
    • ¥40 复杂的限制性的商函数处理
    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置