如何在 slim php 中授予客户端凭据 OAuth 2.0

问题描述 投票:0回答:1

我是新人,正在尝试使用 slim php 学习 api,最近我被介绍给邮差,发现你可以在代码之前测试 api,这很有趣,然后我更深入地研究它并发现了 Oauth 2.0,现在问题是在邮递员中,我可以轻松获取不记名令牌并探索一些 api postmen,但我找不到任何资源向我展示如何在我的苗条 php 文件上实现这一点。顺便说一句,我正在测试的项目是text 我之前不知道邮递员,但这个 api 文档介绍了它,然后我在 youtube 上尝试了一些教程,我得到了授权,但那是在邮递员中,所以我将如何在 slim-php 上实现

我一无所知,因为我没有看到任何示例,所以我无法基于此编写任何代码。看起来 OAuth 2.0 很新

php api oauth-2.0 slim bearer-token
1个回答
0
投票

OAuth 2.0 是一种允许第三方应用程序获得对 HTTP 服务的有限访问的协议。客户端凭据授予是一个简化的流程,适用于客户端是资源所有者的机器对机器身份验证。

要在 Slim PHP 中实施 OAuth 2.0 客户端凭据授予,请按照以下步骤操作:

运行:

composer require bshaffer/oauth2-server-php

创建表格

使用以下架构创建默认数据库:

https://bshaffer.github.io/oauth2-server-php-docs/cookbook/

运行以下 SQL 创建 OAuth 客户端:

INSERT INTO oauth_clients (client_id, client_secret, redirect_uri) VALUES ("testclient", "testpass", "http://fake/");

设置 OAuth 2.0 服务器

配置 OAuth PDO 连接:

use OAuth2\Storage\Pdo;
use OAuth2\Server;
use OAuth2\GrantType\ClientCredentials;
// ...

// Set up your database connection
$dsn = 'mysql:dbname=test;host=localhost';
$username = 'root';
$password = '';
$storage = new Pdo(['dsn' => $dsn, 'username' => $username, 'password' => $password]);

// Pass a storage object to the OAuth2 server class
$oauth = new Server($storage);

// Add the Client Credentials grant type (it is the simplest of the grant types)
$oauth->addGrantType(new ClientCredentials($storage));

添加端点:

客户端将从该端点请求访问令牌。

$app->post('/token', function (ServerRequestInterface $request, ResponseInterface $response) use ($oauth) {
    // Map PSR-7 request to OAuth2 Request
    $oAuthRequest = new OAuth2\Request(
        $request->getQueryParams(),
        (array)$request->getParsedBody(),
        [],
        $request->getCookieParams(),
        $request->getUploadedFiles(),
        $request->getServerParams()
    );

    // Map OAuth2 response to PSR-7 response
    $oauthResponse = $oauth->handleTokenRequest($oAuthRequest);
    $response = $response->withStatus($oauthResponse->getStatusCode());
    foreach ($oauthResponse->getHttpHeaders() as $header => $value) {
        $response = $response->withHeader($header, $value);
    }

    $params = $oauthResponse->getParameters();
    $response->getBody()->write(json_encode($params));

    return $response;
});

这是受保护的资源,需要访问令牌才能访问。

$app->get('/resource', function (ServerRequestInterface $request, ResponseInterface $response) use ($oauth) {
    // Map PSR-7 request to OAuth2 Request
    // ...

    if (!$oauth->verifyResourceRequest($oAuthRequest)) {
        // Map OAuth2 response to PSR-7 response
        // ...

        return $response;
    }

    // The access token is valid, return the protected resource
    $response->getBody()->write(json_encode(['success' => true, 'message' => 'You accessed my APIs!']));
    return $response->withHeader('Content-Type', 'application/json');
});

发送 POST 请求(使用 Postman)来生成令牌:

发布http://localhost/token

{
  "grant_type": "client_credentials",
  "client_id": "testclient",
  "client_secret": "testpass"
}

结果:

{
  "access_token": "674f15fe84a6ee96420b7bd34df6bb5c16ee5103",
  "expires_in": 3600,
  "token_type": "Bearer",
  "scope": null
}

使用

Authorization
标头向受保护端点发送 GET 请求:

GET http://localhost/resource

Authorization: Bearer 674f15fe84a6ee96420b7bd34df6bb5c16ee5103

回应:

{
  "success": true,
  "message": "You accessed my APIs!"
}
© www.soinside.com 2019 - 2024. All rights reserved.