Login with Amazon (ID プール)
Amazon Cognito は Login with Amazon と統合して、モバイルアプリケーションとウェブアプリケーションのユーザーにフェデレートされた認証を提供します。このセクションでは、ID プロバイダー (IdP) として Login with Amazon を使用してアプリケーションを登録し、セットアップする方法について説明します。
デベロッパーポータル
注記
Login with Amazon を Xamarin アプリケーションに統合するには、Xamarin 入門ガイド
注記
Login with Amazon を Unity プラットフォームでネイティブに統合することはできません。代わりに、ウェブビューを使用して、ブラウザのサインインフローに従います。
Login with Amazon のセットアップ
Login with Amazon の実装
Amazon デベロッパーポータル
Amazon は、新しいセキュリティプロファイルに対して OAuth 2.0 クライアント ID を発行します。クライアント ID は、セキュリティプロファイルの [Web Settings] (ウェブ設定) タブにあります。ID プールの Login with Amazon IdP の [Amazon App ID] (Amazon アプリ ID) フィールドにクライアント ID を入力します。
Amazon Cognito コンソールで外部プロバイダーを設定する
Login with Amazon の使用: Android
Amazon ログインを認証したら、TokenListener インターフェイスの onSuccess メソッドで Amazon Cognito 認証情報プロバイダーにトークンを渡すことができます。コードは次のようになります。
@Override public void onSuccess(Bundle response) { String token = response.getString(AuthzConstants.BUNDLE_KEY.TOKEN.val); Map<String, String> logins = new HashMap<String, String>(); logins.put("www.amazon.com", token); credentialsProvider.setLogins(logins); }
Login with Amazon の使用: iOS - Objective-C
Amazon のログインを認証したら、AMZNAccessTokenDelegate の requestDidSucceed メソッドで Amazon Cognito 認証情報プロバイダーにトークンを渡すことができます。
- (void)requestDidSucceed:(APIResult \*)apiResult { if (apiResult.api == kAPIAuthorizeUser) { [AIMobileLib getAccessTokenForScopes:[NSArray arrayWithObject:@"profile"] withOverrideParams:nil delegate:self]; } else if (apiResult.api == kAPIGetAccessToken) { credentialsProvider.logins = @{ @(AWSCognitoLoginProviderKeyLoginWithAmazon): apiResult.result }; } }}
Login with Amazon の使用: iOS - Swift
Amazon のログインを認証したら、AMZNAccessTokenDelegate
の requestDidSucceed
メソッドで Amazon Cognito 認証情報プロバイダーにトークンを渡すことができます。
func requestDidSucceed(apiResult: APIResult!) { if apiResult.api == API.AuthorizeUser { AIMobileLib.getAccessTokenForScopes(["profile"], withOverrideParams: nil, delegate: self) } else if apiResult.api == API.GetAccessToken { credentialsProvider.logins = [AWSCognitoLoginProviderKey.LoginWithAmazon.rawValue: apiResult.result] } }
Login with Amazon の使用: JavaScript
ユーザーが Login with Amazon と認証し、ウェブサイトにリダイレクトされると、Login with Amazon access_token がクエリ文字列で提供されます。このトークンを認証情報ログインマップに渡します。
AWS.config.credentials = new AWS.CognitoIdentityCredentials({ IdentityPoolId: 'IDENTITY_POOL_ID', Logins: { 'www.amazon.com': 'Amazon Access Token' } });
Login with Amazon の使用: Xamarin
Xamarin for Android
AmazonAuthorizationManager manager = new AmazonAuthorizationManager(this, Bundle.Empty); var tokenListener = new APIListener { Success = response => { // Get the auth token var token = response.GetString(AuthzConstants.BUNDLE_KEY.Token.Val); credentials.AddLogin("www.amazon.com", token); } }; // Try and get existing login manager.GetToken(new[] { "profile" }, tokenListener);
Xamarin for iOS
AppDelegate.cs
で、次のコードを挿入します。
public override bool OpenUrl (UIApplication application, NSUrl url, string sourceApplication, NSObject annotation) { // Pass on the url to the SDK to parse authorization code from the url bool isValidRedirectSignInURL = AIMobileLib.HandleOpenUrl (url, sourceApplication); if(!isValidRedirectSignInURL) return false; // App may also want to handle url return true; }
次に、ViewController.cs
で、以下の作業を行います。
public override void ViewDidLoad () { base.LoadView (); // Here we create the Amazon Login Button btnLogin = UIButton.FromType (UIButtonType.RoundedRect); btnLogin.Frame = new RectangleF (55, 206, 209, 48); btnLogin.SetTitle ("Login using Amazon", UIControlState.Normal); btnLogin.TouchUpInside += (sender, e) => { AIMobileLib.AuthorizeUser (new [] { "profile"}, new AMZNAuthorizationDelegate ()); }; View.AddSubview (btnLogin); } // Class that handles Authentication Success/Failure public class AMZNAuthorizationDelegate : AIAuthenticationDelegate { public override void RequestDidSucceed(ApiResult apiResult) { // Your code after the user authorizes application for requested scopes var token = apiResult["access_token"]; credentials.AddLogin("www.amazon.com",token); } public override void RequestDidFail(ApiError errorResponse) { // Your code when the authorization fails InvokeOnMainThread(() => new UIAlertView("User Authorization Failed", errorResponse.Error.Message, null, "Ok", null).Show()); } }