【问题标题】:Laravel - Oauth password grantLaravel - Oauth 密码授予
【发布时间】:2014-06-10 08:30:27
【问题描述】:

我发现很难理解密码授予身份验证过程。

我想要实现的目标:移动设备将注册用户的用户名和密码发送到 API,并以 JSON 形式获取访问令牌作为响应。

到目前为止我已经取得的成就: API 接收用户凭据(用户名和密码)并使用 Laravel 的内置 Auth 身份验证系统对用户进行身份验证。

我知道此时我应该进行 Oauth 身份验证。一旦凭据通过 Auth 步骤,我就有了用户的用户名、密码(在 users 表中指定),我可以从 oauth_clients 表中获取 client_idclient_secret,但我不确定如何完成其余的工作。我需要发出另一个 POST 请求吗?如果是,那么我该如何从控制器执行此操作?

【问题讨论】:

标签: laravel oauth-2.0


【解决方案1】:

我现在实际上正在实施这一点。让我给你看一些代码。

这是我登录功能的一部分:

// this does the process for getting the access token
$oauth  = AuthorizationServer::performAccessTokenFlow();
// some hacks
$oauth  = (array) $oauth;
// more hacks
$oauth  = json_decode($oauth["\0*\0data"], true);

// checks if a token was actually generated
if(!in_array('bearer', $oauth))
{
    // returns what was generated if the token is missing
    return Responser::error(400, $oauth);
}

除了用户的usernamepassword 之外,您还必须在登录时发布其他数据。

所以你的帖子请求将包含:

username=the_username
password=the_password
grant_type=password
client_id=the_client_id
client_secret=the_client_secret

注意grant_type=password 是常量。

现在你必须检查在app/config/packages/lucadegasperi/oauth2-server-laravel/oauth2.php 找到的包的配置:

你应该有以下代码:

'password' => array(
    'class'            => 'League\OAuth2\Server\Grant\Password',
    'access_token_ttl' => 604800,
    'callback'         => function($username, $password){

        $credentials = array(
            // change this to username if username is the field on your database
            'email' => $username,
            'password' => $password,
        );

        $valid = Auth::validate($credentials);

        if (!$valid) {
            return false;
        }

        return Auth::getProvider()->retrieveByCredentials($credentials)->id;
    }
),

你就完成了。


更新

上面生成令牌的代码在这个函数里面:

// allow user to login with username or email and password
$user_pass  = array('username'  => $username, 'password' => $password);
$email_pass = array('email'     => $username, 'password' => $password);

// check if input is email and use $email_pass combination or else use $user_pass combination
$login = ($isEmail->passes() ? $email_pass : $user_pass);

// try to authenticate username & password
if (Auth::attempt($login))
{
    // now you are authenticated here
    // get the client id and secret from the database
    // maybe use curl or some hacks
    // login stuff and token generation

}

【讨论】:

  • 我做了一个非常相似的实现,只是整个登录过程包含两个 POST 请求:第一个获取 client_idclient_secret(我使用默认的 laravel Auth 验证用户获取他/她的详细信息),第二个将数据发布到oauth/access_token,其中包含密码授予类型所需的所有数据。
  • 其实我只有一个帖子,这会验证用户并生成令牌。
  • 但是怎么事先知道用户的id和secret呢?获取此数据的唯一方法是从数据库中获取数据,这需要您首先对用户进行身份验证。
  • 我不明白AuthorizationServer::performAccessTokenFlow(); 如何从请求中获取 POST 数据。你不会在任何地方通过它,是吗?
  • @doitmyway 没错,我目前正在检查源代码是否可以传递数据而不是使用发布请求。如果我有任何地方我会更新
【解决方案2】:

这是一个有点旧的帖子,但如果你想在没有多个帖子请求的情况下这样做,你总是可以这样做:

//Get Oauth creds
$apiCreds = OauthClient::where('email', Input::get('email'))->first();

//Provide additional post data for password grant
Input::merge([
    'grant_type' => 'password',
    'client_id' => $apiCreds->id,
    'client_secret' => $apiCreds->secret,
    'username' => Input::get('email'),
    'password' => Input::get('password'),
    'scope' => 'all'
]);

//Login
return AuthorizationServer::performAccessTokenFlow();

【讨论】:

    猜你喜欢
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    • 2015-07-11
    • 2018-02-01
    • 2017-10-05
    • 2017-02-26
    • 1970-01-01
    相关资源
    最近更新 更多