AWS Cognito работает с OAuth 2.0.Вам необходимо понять, как работает OAuth 2.0, чтобы настроить приложение.См. Ссылку ниже.
1 - Вам необходимо настроить пул, затем настроить «URL-адрес обратного вызова» и получить идентификатор клиента приложения и секретный идентификатор.
2 - После входа в систему«URL обратного вызова» вернет «Код».
3 - с кодом, идентификатором клиента и секретным идентификатором;вы будете запрашивать токен доступа.
Общие сведения о пуле пользователей Amazon Cognito OAuth 2.0 предоставляет: https://aws.amazon.com/blogs/mobile/understanding-amazon-cognito-user-pool-oauth-2-0-grants/
class Cognito_auth{
protected $appClientId;
protected $appClientSecret;
protected $userPoolId;
protected $domain;
protected $callbackUrl;
protected $SignOutUrl;
protected $Authorize;
protected $Token;
protected $UserInfo;
public function __construct(){
$this->appClientId = AWS_COGNITO_APP_ID;
$this->appClientSecret = AWS_COGNITO_APP_SECRET;
$this->userPoolId = AWS_COGNITO_POOL_ID;
$this->domain = AWS_COGNITO_DOMAIN;
$this->callbackUrl = AWS_COGNITO_CALLBACK_URL;
$this->SignOutUrl = AWS_COGNITO_SIGN_OUT_URL;
$this->Authorize = $this->domain.'/oauth2/authorize';
$this->Token = $this->domain.'/oauth2/token';
$this->UserInfo = $this->domain.'/oauth2/userInfo';
}
/**
* Get Login URL
*
*/
public function getLoginURL( $id = NULL )
{
$url = $this->Authorize."/?client_id=".$this->appClientId."&scope=openid&redirect_uri=".$this->callbackUrl."&response_type=code&state=".$id;
return $url;
}
/**
* Get Login URL
*
*/
public function getSignupURL()
{
$url = $this->domain."/signup?client_id=".$this->appClientId."&scope=openid&redirect_uri=".$this->callbackUrl."&response_type=code&state=".$id;
return $url;
}
/**
* Get Logout URL
*
*/
public function getLogoutURL()
{
$url = $this->domain."/logout?client_id=".$this->appClientId."&logout_uri=".$this->SignOutUrl;
return $url;
}
/**
* Get Token
*
*/
public function getToken( $code )
{
$data = array(
'grant_type' => 'authorization_code',
'client_id' => $this->appClientId,
'code' => $code,
'redirect_uri' => $this->callbackUrl,
);
$fields_string = http_build_query( $data );
$authorization = $this->appClientId.':'.$this->appClientSecret;
$headers = array(
"Authorization: Basic ".base64_encode($authorization),
"Content-Type: application/x-www-form-urlencoded",
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->Token );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
$err = curl_error($ch);
if ($err) {
//echo "cURL Error #:" . $err;
return false;
}
curl_close($ch);
$result = json_decode($result);
return $result;
}
/**
* Get User Info
*
*/
public function getUserInfo( $token )
{
$headers = array(
"Authorization: Bearer ".$token,
"cache-control: no-cache"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->UserInfo );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
$err = curl_error($ch);
if ($err) {
//echo "cURL Error #:" . $err;
return false;
}
curl_close($ch);
$result = json_decode($result);
return $result;
}
}
С уважением.Ed.