Laravel 7 cognito用于web和应用程序

Laravel 7 cognito用于web和应用程序,laravel,amazon-cognito,laravel-7,Laravel,Amazon Cognito,Laravel 7,我想用aws cognito构建laravel 7并使用这个包 这个项目将为网络和应用程序 那么,laravel如何处理应用程序的登录,然后应用程序从web上使用来自cognito的相同令牌获取API呢 这是我第一次使用cognito,通常使用auth jwt。您可以为OAuth配置AWS cognito,并使用一个Laravel OAuth插件与cognito连接和验证。我希望您将在AWS Cognito用户池中存储用户凭据 完整的AWS Cognito高级文档- 用于OAuth配置的AWS

我想用aws cognito构建laravel 7并使用这个包

这个项目将为网络和应用程序

那么,laravel如何处理应用程序的登录,然后应用程序从web上使用来自cognito的相同令牌获取API呢


这是我第一次使用cognito,通常使用auth jwt。

您可以为OAuth配置AWS cognito,并使用一个Laravel OAuth插件与cognito连接和验证。我希望您将在AWS Cognito用户池中存储用户凭据

完整的AWS Cognito高级文档-

用于OAuth配置的AWS Cognito体系结构文档-


通过OAuth连接AWS Cognito并获得身份验证的Laravel Socialite文档。

在多次尝试后,没有使用软件包

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class CognitoController extends Controller
{

    private $key;
    private $secret;
    private $region;
    private $version;
    private $client_id;
    private $client_secret;
    private $user_pool_id;

    private $aws;
    private $client;

    public function __construct()
    {

        $this->key = 'xxxxxx';
        $this->secret = 'xxxxxx';
        $this->region = 'xxxxxx';
        $this->version = 'latest';
        $this->client_id = 'xxxxxx';
        $this->client_secret = 'xxxxxx';
        $this->user_pool_id = 'xxxxxx';

        $config = [
            'credentials' => [
                'key' => $this->key,
                'secret' => $this->secret,
            ],
            'region' => $this->region,
            'version' => $this->version,
            'app_client_id' => $this->client_id,
            'app_client_secret' => $this->client_secret,
            'user_pool_id' => $this->user_pool_id,
        ];

        $this->aws = new \Aws\Sdk($config);

        $this->client = $this->aws->createCognitoIdentityProvider();

    }

    public function login()
    {
        $username = 'guest';
        $password = 'password';

        $result = $this->client->adminInitiateAuth([
                                    'AuthFlow' => 'ADMIN_NO_SRP_AUTH',
                                    'AuthParameters' => [
                                        'USERNAME' => $username,
                                        'PASSWORD' => $password,
                                        'SECRET_HASH' => $this->secretHash($username),
                                    ],
                                    'ClientId' => $this->client_id,
                                    'UserPoolId' => $this->user_pool_id,
                                ]);

        $access_token = $result->get('AuthenticationResult')['AccessToken'];

        echo '<pre>';
        print_r( $result );
        echo '<br/>';
        print_r( $access_token );
        echo '</pre>';
    }

    public function register()
    {
        $username = 'guest';
        $password = 'password';
        $email = 'guest@email.com';
        $phone_number = '+819000000000';

        $result = $this->client->signUp([
                                    'ClientId' => $this->client_id,
                                    'Password' => $password,
                                    'SecretHash' => $this->secretHash($username),
                                    'UserAttributes' => [
                                        [
                                            'Name' => 'phone_number',
                                            'Value' => $phone_number,
                                        ],
                                        [
                                            'Name' => 'email',
                                            'Value' => $email,
                                        ]
                                    ],
                                    'Username' => $username
                                ]);

        echo '<pre>';
        print_r( $result );
        echo '</pre>';

    }

    public function register_confirm()
    {
        $username = 'guest';
        $confirmationCode = '123456'; // from email verification code

        $result = $this->client->confirmSignUp([
                                    'ClientId' => $this->client_id,
                                    'Username' =>  $username,
                                    'SecretHash' => $this->secretHash($username),
                                    'ConfirmationCode' => $confirmationCode,
                                ]);

        echo '<pre>';
        print_r( $result );
        echo '</pre>';
    }

    public function list()
    {
        $search = ['UserPoolId' => $this->user_pool_id];

        $users = $this->client->listUsers($search)->toArray();

        echo '<pre>';
        print_r( $users );
        echo '</pre>';
    }

    private function secretHash($username) {
        $clientId = $this->client_id;
        $clientSecret = $this->client_secret;
        $s = hash_hmac('sha256', $username . $clientId, $clientSecret, true);
        return base64_encode($s);
    }

}

来自auth cognito的令牌在使用时是否与令牌相同

拉威尔社交名流还没有一个cognito平台