如何实施基于角色的身份验证(流明框架,firebase令牌)

时间:2019-12-05 16:11:34

标签: php laravel jwt lumen

在过去的几天里,我一直在研究流明框架。 现在,我想对Firebase库使用基于令牌的无状态身份验证。

我已经阅读了几本教程,并最终将不同样本代码的功能性拼凑实现到一个新的,最新的laravel发行项目中:D 它不能做很多,但是如果您想尝试一下,可以通过从github获取存储库来完成: https://github.com/Nokletometre/lumenAuthFirebase

其中包含有关可用于刺激API的URL的说明。 当然,您需要在后台运行的apache(最新的XAMPP),并且需要composer并从cmd的lumen-api文件夹中运行“ composer install”。

现在,我的问题是我从未有过通过令牌进行无状态身份验证的工作,并且有很多技术可以做到这一点。 出于现状,我找不到适合我的特定设置的任何教程,而且我也不知道我可以在不同程度上使用教程的程度:(

我已经查看了源代码中启用身份验证/令牌功能的控制器和提供程序。 在存储库中,其为AuthController.php:

<?php

namespace App\Providers;

use App\User;
use Firebase\JWT\JWT;
use Illuminate\Support\ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Boot the authentication services for the application.
     *
     * @return void
     */
     public function boot()
     {
       // Here you may define how you wish users to be authenticated for your Lumen
       // application. The callback which receives the incoming request instance
       // should return either a User instance or null. You're free to obtain
       // the User instance via an API token or any other method necessary.

       $this->app['auth']->viaRequest('api', function ($request) {
         $key = 'pawifjopawiejfpoaiwejfpoji';
         $jwt = preg_replace('/^Bearer (.*)/', '$1', $request->header('Authorization'));
         $decoded = JWT::decode($jwt, $key, ['HS256']);

         return User::where('email', $decoded->email)->first();
       });
     }
}

SecretController.php:

<?php

namespace App\Http\Controllers;

//use Illuminate\Http\Request;


use App\User;
use Firebase\JWT\JWT;
use Illuminate\Http\Request;
use Illuminate\Http\Response;


class SecretController extends Controller
{
  public function index(Request $request)
  {
    return new Response('Hello ' . $request->user()->name,
      200, ['Content-Type', 'text/plain']);
  }

  public function pub()
  {
    return new Response('Hello World', 200, ['Content-Type', 'text/plain']);

  }


}

和AuthServiceProvider.php:

<?php

namespace App\Providers;

use App\User;
use Firebase\JWT\JWT;
use Illuminate\Support\ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Boot the authentication services for the application.
     *
     * @return void
     */
     public function boot()
     {
       // Here you may define how you wish users to be authenticated for your Lumen
       // application. The callback which receives the incoming request instance
       // should return either a User instance or null. You're free to obtain
       // the User instance via an API token or any other method necessary.

       $this->app['auth']->viaRequest('api', function ($request) {
         $key = 'pawifjopawiejfpoaiwejfpoji';
         $jwt = preg_replace('/^Bearer (.*)/', '$1', $request->header('Authorization'));
         $decoded = JWT::decode($jwt, $key, ['HS256']);

         return User::where('email', $decoded->email)->first();
       });
     }
}

此外,在bootstrap / app.php中 我没有评论

 $app->withEloquent();

 $app->routeMiddleware([
    'auth' => App\Http\Middleware\Authenticate::class,
   ]);

 $app->register(App\Providers\AuthServiceProvider::class);

现在,问题是我根本不了解提供者和控制器之间的交互。 身份验证功能是一种中间件,因此在运行时,身份验证可能必须在将请求传播到控制器之前运行,对吗?据我所知,因为我的中间件控制着请求可以进入我的API的深度?

但是我只是不知道这些组件如何相互通信。 如果来自客户端的令牌有效(仍然有效),则从AuthServiceProver.php返回用户实例。但是我不知道该返回到哪儿呢Oo因此,我也不知道程序流是什么样子,因此,我根本不知道从哪里开始编写代码 用于基于角色的身份验证:(

我基本上想构建一个API,该API至少可以通过Firebase令牌技术区分完全未注册的用户,已注册的用户和管理员。 到目前为止,我怀疑代码的这些部分尤其对于利用流明/火底技术至关重要:

AuthController.php的一部分:

  public function login(Request $request)
  {
    $user = User::where('email', $request->email)->first();

    if (!is_null($user)) {
      if (password_verify($request->password, $user->password)) {
        $key = 'pawifjopawiejfpoaiwejfpoji';
        $token = [
          'iss' => 'http://jwt-test.dev.local',
          'name' => $user->name,
          'email' => $user->email,
          'admin' => $user->id === 2
        ];

        $jwt = JWT::encode($token, $key);

        return new Response($jwt, 200, ['Content-Type' => 'text/plain']);
      }
      return 'wrong password';
    }
    return 'user not found';
  }

AuthServiceProvider.php的一部分:

 public function boot()
 {
   // Here you may define how you wish users to be authenticated for your Lumen
   // application. The callback which receives the incoming request instance
   // should return either a User instance or null. You're free to obtain
   // the User instance via an API token or any other method necessary.

   $this->app['auth']->viaRequest('api', function ($request) {
     $key = 'pawifjopawiejfpoaiwejfpoji';
     $jwt = preg_replace('/^Bearer (.*)/', '$1', $request->header('Authorization'));
     $decoded = JWT::decode($jwt, $key, ['HS256']);

     return User::where('email', $decoded->email)->first();
   });
 }

但是,正如我说的那样,我真的很迷失:=(如果您能指出我的一段文档,我已经非常感激了,该文档为我提供了更深入的解释,我需要做些什么来实现这一目标关闭。

0 个答案:

没有答案