【问题标题】:Laravel: API with OAuth 2.0Laravel:使用 OAuth 2.0 的 API
【发布时间】:2015-01-15 03:32:07
【问题描述】:

我目前正在开发一个我计划使用 oauth2 保护的 API。

我选择了:https://github.com/lucadegasperi/oauth2-server-laravel/

我已经按照安装指南设法保护了端点(在我的 api 路由中使用 before=>oauth),但我不知道如何能够验证和访问端点。

我知道您首先需要通过发送 client_id 和 client_secret 来请求 access_token,但我不知道在 oauth 服务器上的哪里设置这些?

我看到 oauth 控制器有这样的端点:

http://somedomain.com/oauth/authorize

http://somedomain.com/oauth/access_token

但我不知道如何处理它们。我只是设法得出结论,它需要一个 client_id、client_secret 和有关范围的东西。

我可以在哪里设置这些值供 api 客户端使用?

提前感谢您的帮助。

【问题讨论】:

    标签: laravel oauth-2.0


    【解决方案1】:

    我不了解 Laravel,但总的来说,authorization endpoint(在您的情况下为http://somedomain.com/oauth/authorize)的行为与RFC 6749 中的描述相同。

    规范定义了四个流程。如果您在流中使用Authorization Code Flow,则应使用以下请求参数访问授权端点。

    1. response_type=code(必填)
    2. client_id={your-client-id}(必填)
    3. scope={space-delimited-scope-names}(可选)
    4. redirect_uri={your-redirect-uri}(有条件的可选)
    5. state={any-arbitrary-string}(可选)

    例如,

    http://somedomain.com/oauth/authorize?response_type=code&client_id=your-client-id&scope=profile+email

    授权端点生成一个授权码并将其返回给您的浏览器。

    下一步是使用授权端点发出的授权码访问token endpoint(在您的情况下为http://somedomain.com/oauth/access_token)。像这样,

    发布
    http://somedomain.com/oauth/access_token?grant_type=authorization_code&code=issued-authorization-code&client_id=your-client-id&client_secret=your-client-secret

    无论如何,我建议你阅读RFC 6749

    【讨论】:

    • 是的,我了解所需的参数。但就像我说的,我不知道从哪里获得 {your-client-id},我在哪里设置?我要手动将其插入数据库并告诉客户端吗?
    • 我认为 Laravel 有(并且应该有)一个功能来发布和管理客户端 ID。尽管 RFC 6749 中没有提到管理客户端应用程序,但它是 OAuth 服务器预期实现的一项功能。如果您必须手动将客户端 ID 插入数据库,这意味着 Laravel 缺少一个重要功能。例如,Authlete (authlete.com) 是 OAuth 2.0/OpenID Connect as BaaS 的实现,它提供 API 集来管理客户端应用程序 (authlete.com/authlete_web_apis_client.html)。 Laravel 应该有一个等价物。
    猜你喜欢
    • 2017-04-03
    • 2014-07-28
    • 2015-08-25
    • 2016-10-22
    • 2020-07-22
    • 2014-09-10
    • 2015-11-11
    • 2015-02-07
    • 1970-01-01
    相关资源
    最近更新 更多