Last active
October 3, 2015 13:54
-
-
Save horike37/5198398dbb737a6c4f0d to your computer and use it in GitHub Desktop.
Cognitoでfacebook認証して、STSで認可して、S3に画像をアップロードするPHPスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once("vendor/autoload.php"); | |
use Aws\CognitoIdentity\CognitoIdentityClient; | |
use Aws\Sts\StsClient; | |
use Aws\S3\S3Client; | |
$facebook = new Facebook(array( | |
'appId' => '<facebookのappid>', | |
'secret' => '<facebookのsecret>', | |
)); | |
$fb_user = $facebook->getUser(); | |
if ($fb_user) { | |
$cognito = CognitoIdentityClient::factory(array('version' => '2014-06-30', 'region' => 'ap-northeast-1')); | |
$cognito_ret = $cognito->getOpenIdTokenForDeveloperIdentity(array( | |
'IdentityPoolId' => '<CognitoのIdentityPoolId>', | |
'Logins' => array('graph.facebook.com' => $facebook->getAccessToken(),) | |
)); | |
$sts = StsClient::factory(array('version' => '2011-06-15', 'region' => 'ap-northeast-1')); | |
$sts_ret = $sts->assumeRoleWithWebIdentity(array( | |
'RoleArn' => 'arn:aws:iam::<AWSのアカウントID>:role/Cognito_panpanpanAuth_Role', | |
'RoleSessionName' => 'panpanpan', | |
'WebIdentityToken' => $cognito_ret->get('Token'), | |
)); | |
$credentials = $sts_ret->get('Credentials'); | |
$s3 = S3Client::factory(array( | |
'version' => '2006-03-01', | |
'region' => 'ap-northeast-1', | |
'credentials' => array( | |
'key' => $credentials['AccessKeyId'], | |
'secret' => $credentials['SecretAccessKey'], | |
'token' => $credentials['SessionToken'], | |
) | |
)); | |
$result = $s3->putObject(array( | |
'Bucket' => 'panpan-cognito', | |
'Key' => date('Y/m/d H:i:s'), | |
'Body' => fopen('/Users/horike/Desktop/cognito-icon.png', 'r'), | |
)); | |
} else { | |
$fb_login_url = $facebook->getLoginUrl(); | |
echo '<a href="'.$fb_login_url.'">fbログイン</a>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment