通过移动应用程序使用 Laravel 护照

Using Laravel Passport with mobile application

我正在制作一个带有 laravel API 的移动应用程序,我看到自从 Laravel 5.3(?) 他们添加了一个名为 "Passport" 的东西来处理 OAuth2/verification 的应用程序,所以我想我会试一试。在使用 Laravel Passport 文档 (https://laravel.com/docs/5.4/passport) 完成设置后,我遵循了几种不同的解释来说明如何让它工作。现在这是我根据其他教程/SO 文章得出的代码

1.) 用于创建 user/oAuth2 客户端的控制器

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_clientstable

use Illuminate\Database\Eloquent\Model;

class OAuthClient extends Model
{
    protected $table = 'oauth_clients';
} 

3.) 我将 oauth_clients table 主键从递增整数更改为用户电子邮件。我基本上只是在关注这篇 SO 文章 Laravel Passport Password Grant Tokens: own mobile app

4.) 创建 user/oauth_client 后,通过 POSTMAN 检索令牌 post 使用参数 oauth/token 请求

问题是,这对我来说真的很不对劲。 oauth_clients 上有一个 user_id 列,所以它真的让我相信在尝试获取 oauth_client 令牌时我应该能够做一些 post 请求它会带用户然后得到关联的oauth_client,对吧?

在我看来,我应该如何使用 Passport 为我的移动应用程序进行用户身份验证的意义如下: 1.) 注册新用户

2.) 注册用户时,为该用户创建oauth_client

3.) 登录时,一旦用户 email/pw 被验证,查找 oauth_client 然后检索 oath_client 令牌

4.) 对 API 的任何请求都使用 oauth_client 令牌,然后再发送给经过验证的身份验证用户。

这样想对吗??我敢肯定这是显而易见的,但这个过程让我感到困惑,所以任何指导将不胜感激。

好的,如果有人感兴趣或有任何未来的建议,这就是我最后所做的工作。

我为移动注册创建了一条新路线,

    Route::post('/mobile_register', 'Auth\RegisterController@mobileRegister');

然后我基本上只是复制了实际的注册方法,但添加了一个新函数来根据成功的用户注册信息

创建一个新的oauth_client
 /**
 * 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 将被发送回移动应用程序,然后在您的移动应用程序中您可以创建需要发送的 'client_secret' 以获得 oauth 令牌,您可以使用从 API 返回的 client_id 作为 oauth/token post 请求中的 'client_id'。然后您将发送第二个 post 请求以检索用户的实际 oauth 令牌。

我觉得这是最好的,因为那样我就不需要在应用程序上存储任何敏感的用户信息,即使我发回了客户端 ID,除非一些邪恶的一方知道你将用于什么'client_secret',他们也无法访问您的 oauth 客户端以检索令牌。他们也不会知道您的用户,因此他们无法从您的用户那里确定您的 oauth 客户端。 我也喜欢这个解决方案,因为我仍在验证用户是否存在,并且在对 oauth_client 信息进行第二次验证之前密码是否正确。

免责声明::这是我第一次尝试使用 Passport 或真正进行任何类型的身份验证,所以这肯定有问题。如果您看到任何内容,请发表评论或 post 让我知道!我想确保它尽可能好,所以我将不胜感激!