在移动应用程序中使用Laravel Passport
我正在用一个LaravelAPI制作一个移动应用程序,我看到自从Laravel5.3(?)以来,他们添加了一个叫做“Passport”的东西,用于处理应用程序的OAuth2/验证,所以我想我会尝试一下。在使用Laravel Passport文档()完成设置后,我遵循了几种不同的解释来说明如何让它工作。现在,这是我根据其他教程/SO文章提出的代码 1.)用于创建用户/oAuth2客户端的控制器在移动应用程序中使用Laravel Passport,laravel,api,oauth-2.0,laravel-passport,Laravel,Api,Oauth 2.0,Laravel Passport,我正在用一个LaravelAPI制作一个移动应用程序,我看到自从Laravel5.3(?)以来,他们添加了一个叫做“Passport”的东西,用于处理应用程序的OAuth2/验证,所以我想我会尝试一下。在使用Laravel Passport文档()完成设置后,我遵循了几种不同的解释来说明如何让它工作。现在,这是我根据其他教程/SO文章提出的代码 1.)用于创建用户/oAuth2客户端的控制器 class OAuthController extends Controller { publi
class OAuthController extends Controller
{
public function registerUser(Request $request){
$email = $request->email;
$password = $request->password;
$name = $request->name;
$user = User::create([
'name' => $name,
'email' => $email,
'password' => bcrypt($password)
]);
$oAuthClient = new OAuthClient();
$oAuthClient->user_id = $user->id;
$oAuthClient->id = $user->email;
$oAuthClient->name = $user->name;
$oAuthClient->secret = base64_encode(hash_hmac('sha256',$password, 'secret', true));
$oAuthClient->password_client=1;
$oAuthClient->redirect = '';
$oAuthClient->personal_access_client = 0;
$oAuthClient->revoked = 0;
$oAuthClient->save();
return response()->json(['message', 'User successfully created']);
}
}
2.)我制作的模型引用了oauth_clients表
use Illuminate\Database\Eloquent\Model;
class OAuthClient extends Model
{
protected $table = 'oauth_clients';
}
3.)我将oauth_clients表primarykey从递增整数更改为用户电子邮件。我基本上只是在看这篇文章
4.)一旦创建了用户/oauth_客户端,就可以通过POSTMAN w/post请求使用参数检索令牌到oauth/token
问题是,我觉得这真的不对。oauth_客户机上有一个user_id列,因此当我试图获取oauth_客户机令牌时,它确实让我相信我应该能够执行一些post请求,在那里它将接收用户,然后获取相关的oauth_客户机,对吗
在我看来,如何使用Passport对我的移动应用程序进行用户身份验证是有意义的,如下所示:
1.)注册新用户
2.)注册用户时,为该用户创建oauth_客户端
3.)登录时,验证用户电子邮件/pw后,查找oauth_客户端,然后检索誓言客户端令牌
4.)在对API的任何请求上使用oauth_客户端令牌,以转发给经过验证的用户
这是正确的想法吗??我确信这是显而易见的,但这一过程让我感到困惑,因此任何指导都将不胜感激。好吧,如果有人感兴趣或有任何未来的建议,下面是我最后为使这项工作发挥作用所做的 我为手机注册创建了一条新路线
Route::post('/mobile_register', 'Auth\RegisterController@mobileRegister');
然后,我基本上只是复制了实际的register方法,但添加了一个新函数,以根据成功的用户注册信息创建一个新的oauth_客户机
/**
* Register a new user through the mobile application. Send back the oauth_client ID which will be used
* to retrieve the oauth token
*
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function mobileRegister(Request $request){
$this->validator($request->all())->validate();
$password = $request->password;
event(new Registered($user = $this->create($request->all())));
$oAuthClient = $this->registerOAuthClient($user, $password);
return response()->json(['message' => 'user created successfully', 'client_id' => $oAuthClient->id]);
}
/**
* Create the associated oauth_client for this user.
*
* @param User $user
* @param $password
* @return \Illuminate\Http\JsonResponse|OAuthClient
*/
public function registerOAuthClient(User $user, $password){
$oAuthClient = new OAuthClient();
$oAuthClient->user_id = $user->id;
$oAuthClient->name = $user->name;
$oAuthClient->secret = base64_encode(hash_hmac('sha256',{{whatever you want your secret to be based off of}}, 'secret', true));
$oAuthClient->password_client=1;
$oAuthClient->redirect = '';
$oAuthClient->personal_access_client = 0;
$oAuthClient->revoked = 0;
if(!$oAuthClient->save())
return response()->json(['error' => 'Unable to create oAuthClient! User will need one made to access website']);
return $oAuthClient;
}
因此,现在新的oAuthClient ID将被发送回移动应用程序,然后在移动应用程序中,您可以创建需要发送以获取oauth令牌的“client_secret”,并且您也可以在oauth/token post请求中使用从API发送回的client_ID作为“client_ID”。然后,您将发送第二个post请求,为用户检索实际的oauth令牌
我觉得这是最好的,因为这样我就不需要在应用程序上存储任何敏感的用户信息,而且即使我正在发回客户端id,除非某个邪恶的一方也知道您将使用什么来获取“client_secret”,否则他们无法访问您的oauth客户端来检索令牌。。而且他们也不了解您的用户,因此他们无法从您的用户确定您的oauth客户端。
我还喜欢这个解决方案,因为我仍然在验证用户是否存在&在对oauth_客户机信息进行第二次验证之前,pw是正确的
免责声明::这是我第一次尝试使用Passport或真正进行任何类型的身份验证,因此这肯定有可能存在问题。如果你看到任何,请评论或张贴让我知道!我想确保这是尽可能好,所以我会非常感激 您好,先生,您是否正在使用inAppBrowser打开/mobile\u注册路线?