使用laravel / lumen中的客户端ID和密钥访问API

时间:2016-09-02 16:05:48

标签: web-services api laravel jwt lumen

我为我的网络应用程序创建了一个API。现在我想要访问世界,但在提供访问权限之前我想要的机制类似于Facebook API,Twitter API,提供客户端ID和密钥的Google API。目前,我正在使用JWT AuthController,用户使用他的凭据登录并返回令牌,我不希望用户登录。

我希望用户可以使用客户端ID和密钥访问我的API吗?另一件事是,我将如何为用户创建客户端ID和密钥?

这是否可以使用JWT Auth实现?

任何帮助?

1 个答案:

答案 0 :(得分:1)

我已经阅读了这篇文章并且非常有前途,但是在发布几篇文章后它建议使用oauth2,在这里你去了:

https://laracasts.com/discuss/channels/lumen/api-authorization-via-public-and-secret-keys

引号:

  

只需将类添加到API配置中即可。

     

命名空间App \ Providers \ Guard;

     

使用Dingo \ Api \ Auth \ Provider \ Authorization;使用   巴丁格\阿比\路径\路径;使用Illuminate \ Http \ Request;使用   的Symfony \组件\ HttpKernel \异常\ UnauthorizedHttpException;

     

类GuardProvider扩展授权{       / **        *获取提供者授权方法。        *        * @return字符串        * /       公共函数getAuthorizationMethod()       {           返回' X-Authorization&#39 ;;       }

/**
 * Authenticate the request and return the authenticated user instance.
 *
 * @param \Illuminate\Http\Request $request
 * @param \Dingo\Api\Routing\Route $route
 *
 * @return mixed
 */
public function authenticate(Request $request, Route $route)
{
    $key = $request->header(env('API_AUTH_HEADER', 'X-Authorization'));
    if (empty($key)) $key = $request->input(env('API_AUTH_HEADER', 'X-Authorization'));
    if (empty($key)) throw new UnauthorizedHttpException('Guard', 'The supplied API KEY is missing or an invalid authorization header was sent');

    $user = app('db')->select("SELECT * FROM users WHERE users.key = ?", [$key]);
    if (!$user) throw new UnauthorizedHttpException('Guard', 'The supplied API KEY is not valid');

    return $user;
} }
相关问题