How to log in to AWS cognito user pool using PHP SDK - php

I am trying to log in the user to the AWS Cognito user pool using PHP SDK. I am following this tutorial, https://sanderknape.com/2017/02/getting-started-with-aws-cognito/. But I am getting the error.
Here is my code:
$credentials = array(
'key' => env('AWS_IAM_KEY', ''),
'secret' => env('AWS_IAM_SECRET', '')
);
//2014-06-30
$client = CognitoIdentityClient::factory(array('region' => env('AWS_REGION',''), 'version' => 'latest', $credentials));
$result = $client->adminInitiateAuth([
'AuthFlow' => 'ADMIN_NO_SRP_AUTH',
'ClientId' => COGNITO_APP_CLIENT_ID,
'UserPoolId' => COGNITO_USER_POOL_ID,
'AuthParameters' => [
'USERNAME' => "name",
'PASSWORD' => 'password',
],
]);
$accessToken = $result->get('AuthenticationResult')['AccessToken'];
When I run the code, I got this error:
InvalidArgumentException
Operation not found: AdminInitiateAuth
It is saying the AdminInitiiateAuth does not exist. But I am correctly following the tutorial. What is missing in my code?

The problem here is that CognitoIdentityClient does not contain the adminInitiateAuth functionality.
You will need to use the CognitoIdentityProviderClient

Related

Upload Object (image file) to Linode Object Storage using PHP (Laravel) with S3Client

What could be the possible solution for this? I'm trying to upload an Object (Image file) to my Linode Object Storage, I have already set up the configuration and credentials but I'm still having trouble doing it. Can someone help me?
Code:
$config = [
'version' => 'latest',
'region' => config('filesystems.disks.s3.region'),
'scheme' => 'http',
'endpoint' => config('filesystems.disks.s3.endpoint'),
'credentials' => [
'key' => config('filesystems.disks.s3.key'),
'secret' => config('filesystems.disks.s3.secret'),
],
'debug' => true
];
$client = new S3Client($config);
return $client->putObject([
'Bucket' => config('filesystems.disks.s3.bucket'),
'Key' => $filename,
'SourceFile' => $file
]);
Here's the exception error:
check me please
Hoping to have some feedback here, thank you!

User pool client {id}does not exist

Here is my code:
use Aws\CognitoIdentityProvider\CognitoIdentityProviderClient;
$args = [
'credentials' => [
'key' => 'valid',
'secret' => 'valid',
],
'region' => 'us-west-2',
'version' => 'latest',
'app_client_id' => 'valid',
'app_client_secret' => 'valid',
'user_pool_id' => 'valid',
];
$email = 'test32#test.com';
$client = new CognitoIdentityProviderClient($args);
$hash_key = cognitoSecretHash($email);
$login = $client->adminInitiateAuth([
'AuthFlow' => 'ADMIN_NO_SRP_AUTH',
'AuthParameters' => [
'Username' => $email,
'Password' => '12345678',
'SecretHash' => $hash_key,
],
'ClientId' => 'valid',
'UserPoolId' => 'valid',
]);
return $login;
function cognitoSecretHash($username)
{
$message = $username . 'app_client_id';
$hash = hash_hmac(
'sha256',
$message,
'app_client_secret',
true
);
return base64_encode($hash);
}
Its give me this error:
Fatal error: Uncaught exception 'Aws\CognitoIdentityProvider\Exception\CognitoIdentityProviderException' with message 'Error executing "AdminInitiateAuth" on "https://cognito-idp.us-west-2.amazonaws.com"; AWS HTTP error: Client error: POST https://cognito-idp.us-west-2.amazonaws.com resulted in a 400 Bad Request response: {"__type":"ResourceNotFoundException","message":"User pool client {id} does not exist."} ResourceNotFoundException (client): User pool client {id} does not exist. - {"__type":"ResourceNotFoundException","message":"User pool client {id} does not exist."}' GuzzleHttp\Exception\ClientException: Client error: POST https://cognito-idp.us-west-2.amazonaws.com resulted in a 400 Bad Request response: {"__type":"ResourceNotFoundException","message":"User pool client {id} does not exist."} in D:\xampp\htdocs\test\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php:113 Stack trace: #0 D:\xampp\htdocs\test\vend in D:\xampp\htdocs\test\vendor\aws\aws-sdk-php\src\WrappedHttpHandler.php on line 195
$login = $client->adminInitiateAuth([
'AuthFlow' => 'ADMIN_NO_SRP_AUTH',
'AuthParameters' => [
'USERNAME' => $email,
'PASSWORD' => $password,
'SECRET_HASH' => $hash_key,
],
'ClientId' => $clientId,
'UserPoolId' => $poolId,
]);
I had this issue and just had to do an amplify pull - problem was out of sync project on different computers and git pull didn't cover the aws backend stuffs I think.

AWS Credential error

I am trying to connect to AWS from my php application(Laravel). My intention is to use the AWS Media convert Service. Please see the code I have used.
$client = new MediaConvertClient([
'profile' => 'default',
'version' => '2017-08-29',
'region' => 'us-west-1',
'credentials' => [
'key' => $key,
'secret' => $secret,
],
]);
$URI = $client->getEndpoint();
$endpoints= $client->describeEndpoints();
This is the error I am getting.
Cannot read credentials from /.aws/credentials
Does anyone have an idea about how to resolve this issues?
Finally, I got the answer. Removing the below line helped me to read the credential from $key and $secret (which is defined in the configuration file)
'profile' => 'default',

Specify version when creating a aws-sdk-php client

I'm using Laravel Stapler to save model attachments to s3. Reading through the docs I came up with the following code and stuck it in the model's save method.
$this->hasAttachedFile('recording', [
'url' => '/recording/:attachment/:id_partition/:style/:filename',
'storage' => 's3',
's3_client_config' => [
'key' => 'accessKey',
'secret' => 'secretKey',
'region' => 'us-east-1'
],
's3_object_config' => [
'Bucket' => 'bucket.aws.com'
],
]);
When I find() a model, set the attachment, and run save() I get:
PHP Fatal error: Uncaught exception 'InvalidArgumentException' with message 'version is a required configuration setting when creating a client.' in /Users/myName/Code/projectName/vendor/aws/aws-sdk-php/src/Common/ClientFactory.php:99
The ClientFactory create() method required a 'version' to be set. Looking through the laravel-stapler and stapler docs I don't see anything mentioning a version. I'm using the most recent version of laravel-stapler with laravel 4.
Is there something I'm missing here?
Even though it wasn't in the docs I had to actually add a version when configuring s3.
's3_client_config' => [
'key' => 'accessKey',
'secret' => 'secretKey',
'region' => 'us-east-1',
'version' => 'latest',
],

[AWS][DynamoDB] Connection to the DB through the PhP SDK

I am new to AWS. I have installed an EC2 server which processes PhP code. I am able to administrate the DB through the Amazon website. I'm trying to access my DynamoDB table with the following code:
use Aws\DynamoDb\DynamoDbClient;
try {
$client = DynamoDbClient::factory(array(
'profile' => 'default', // access ID + secret are in the .aws/credentials file
'region' => Region::EU_WEST_1 // also tried with "eu-west-1"
));
echo "after client instanciation"; // this is not displayed
$response = $client->getItem([
'TableName' => 'Child',
'Key' => [
'ChildID' => 'Nicolas'
]
]);
print_r ($response['Item']);
} catch (Exception $e) {
echo '<p>Exception received : ', $e->getMessage(), "\n</p>";
}
I'm not getting any exception. The child I'm trying to get isn't displayed (I did create it). Also tried with the putItem method but it didn't add anything to the DB.
I think you are missing the data type in the key:
$response = $dynamodb->getItem([
'TableName' => 'Child',
'Key' => [
'ChildID' => [ 'S' => 'Nicolas' ]
]
]);
Now you should get the output, you can refer this Link.
Try using the following code, it allows you to pass public and secret keys through parameters.
$client = new DynamoDbClient([
'version' => 'latest',
'region' => 'ap-northeast-1',
'credentials' => [
'key' => 'A5ITUTLAK7W47NNNNQ',
'secret' => 'DrsEjmEMs4PUPIY5/12a/cpUB7JVVcKLahFz826p'
]
]);
try {
$result = $client->getItem(array(
'ConsistentRead' => true,
'TableName' => 'fruits',
'Key' => array(
'id' => array('S' => '1')
)
));
Replace the keys with yours.
Reference:
https://solutionarchitects.guru/viewtopic.php?f=30&t=27

Categories