sparqapi/api/application/libraries/AWS_COGNITO.php
2023-02-09 16:53:39 +05:30

113 lines
3.3 KiB
PHP

<?php defined('BASEPATH') OR exit('No direct script access allowed');
include_once APPPATH.'/vendor/autoload.php';
use Aws\CognitoIdentityProvider\CognitoIdentityProviderClient;
use Aws\Exception\AwsException;
class AWS_COGNITO
{
private $client;
private $name = "TEST";
private $CI;
private $cognito_properties;
private $_auth_result;
public function __construct()
{
// Construct the parent class
//parent::__construct();
$this->CI =& get_instance();
$this->cognito_properties = array('region'=>'ap-south-1','version'=>'latest','credentials' => array(
'key' =>$this->CI->config->item('cognito_resource_key'),
'secret' => $this->CI->config->item('cognito_resource_secret')
));
$this->client = CognitoIdentityProviderClient::factory($this->cognito_properties);
// echo "Called";
// $this->setCors('lender8');die();
}
public function adminResetPassword($user_name)
{
$result = $this->client->adminResetUserPassword([
'UserPoolId' => $this->CI->config->item('sine_egde_pool_id'), // REQUIRED
'Username' => $user_name, // REQUIRED
]);
print_r($result);
}
public function adminSetUserPassword($user_name)
{
//print_r('adminSetUserPassword');
$result = $this->client->adminSetUserPassword([
'Password' => 'temp1234', // REQUIRED
'Permanent' => false,
'UserPoolId' => $this->CI->config->item('sine_egde_pool_id'), // REQUIRED
'Username' => $user_name, // REQUIRED
]);
print_r($result);
}
//duplicated of adminSetUserPassword because otp generation on restapi
public function resetUserPassword($user_name,$key)
{
$result = $this->client->adminSetUserPassword([
'Password' => 'temp1234', // REQUIRED
'Permanent' => false,
'UserPoolId' => $this->CI->config->item($key), // REQUIRED
'Username' => $user_name, // REQUIRED
]);
}
/**
* Calls the _authenticate() method to set the $_auth_result array. Will return the AccessToken value if found
* otherwise false.
*
* @param $username
* @param $password
* @return false|mixed
*/
public function login($username, $password) {
$this->_authenticate($username, $password);
if (is_array($this->_auth_result)) {
return array(true,$this->_auth_result['IdToken']);
}
return array(false,$this->_auth_result);
}/**
* Function that makes the authentication call to the Cognito User Pool and gets back the result. It then sets it
* as a private class variable.
*
* @param $username
* @param $password
*/
private function _authenticate($username, $password) {
if (isset($this->client)) {
try{
$auth_result = $this->client->adminInitiateAuth([
'AuthFlow' => 'ADMIN_USER_PASSWORD_AUTH',
'ClientId' => $this->CI->config->item('lender_web_client_id'),
'UserPoolId' => $this->CI->config->item('lender_pool_id'),
// 'UserPoolId' => 'ap-south-1_7Xvnpr2At', // REQUIRED
'AuthParameters' => [
'USERNAME' => $username,
'PASSWORD' => $password,
]
]);if ($auth_result->get('AuthenticationResult')) {
$this->_auth_result = $auth_result->get('AuthenticationResult');
}
} catch(AwsException $e){
$this->_auth_result = $e->getMessage();
}
}
}
}
?>