我正在使用护照为我的API建立注册方法。当用户进行注册时,我想将访问令牌还给他,就像我们要求访问令牌时一样。为此,使用授权密码客户端。
我要做的是在注册数据中沿着client_id
询问client_secret
。
然后我正在寻找的是我的验证规则能够验证client_secret
对应于client_id
。
这是我的注册方法:
/**
* Register a new user in the system.
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function register(Request $request)
{
$vb = User::ValidationBook();
$data = $request->validate($vb["rules"], $vb["messages"]);
// Neccesary data to get a token at registration
$password = $data["user"]["password"];
$clientId = $data["user"]["client_id"];
$clientSecret = $data["user"]["client_secret"];
// If validation passes, create user
$user = $this->userService->store($data);
$request->request->add([
'grant_type' => 'password',
'client_id' => $clientId,
'client_secret' => $clientSecret,
'username' => $user->email,
'password' => $password,
'scope' => null,
]);
// Fire off the internal request.
$token = Request::create(
'oauth/token',
'POST'
);
return \Route::dispatch($token);
}
这是我的用户模型的简化版本,我拥有验证书方法中的所有规则。
class User extends Authenticatable
{
/**
* Returns an array that contains two indexes:
* 'rules' for the validation
* 'messages' messages given by the validation
*
* @return array
**/
public static function ValidationBook($except = [], $append = [])
{
$book = ['rules' => [], 'messages' => []];
$book['rules'] = [
... the other rules
//Extra data for register
'user.client_id' => 'required|exists:oauth_clients,id',
'user.client_secret' => 'required|exists:oauth_clients,secret'
];
$book['messages'] = [
... the other messages
// Extras
'user.client_id.required' => 'The client id is required',
'user.client_secret.required' => 'The client secret is required',
];
if (!empty($except)) {
$except = array_flip($except);
$book['rules'] = array_diff_key($book['rules'], $except);
}
if (!empty($append)) {
$book = array_merge_recursive($book, $append);
}
return $book;
}
}
如何为user.client_secret
规则添加规则以验证机密是否与该特定ID相对应?
也许这不是在注册后返回访问令牌的最佳选择,如果有一种简单的避免方法,我将很高兴了解它。
谢谢。
答案 0 :(得分:0)
解决方案很简单。在验证规则的user.client_secret
上,添加以下值:
$book['rules'] = [
... the other rules
'user.client_id' => 'required|exists:oauth_clients,id',
'user.client_secret' => 'required|exists:oauth_clients,secret,id,'
];
在此方法中,在验证秘密是否存在的旁边,我添加了查询以检查具有指定ID的所有记录。
然后在验证之前,我将所需的id
添加到规则中:
$vb = User::ValidationBook();
$vb["rules"]["user.client_secret"] .= $request->input("user")["client_id"];
$data = $request->validate($vb["rules"], $vb["messages"]);