我现在实际上正在实施这一点。让我给你看一些代码。
这是我登录功能的一部分:
// 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);
}
除了用户的username 和password 之外,您还必须在登录时发布其他数据。
所以你的帖子请求将包含:
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
}