2016-11-05 10 views
0

これを行う明確な方法はドキュメントにはありません。ユーザープールを作成してから、ユーザープールに対してCognitoでプロバイダを作成したら、ユーザー名とパスワードを認証するにはどうすればよいですか?ユーザアイデンティティプールAmazon CognitoでJavaユーザを認証する - Java SDK

このファイルはsampleですが、パスワードはCognitoではなく別のデータベースで管理されているようです。

答えて

0

Android用モバイルSDKを使用していて、すべての設定が完了していると仮定しています。まず、ユーザープールに接続することになるでしょう:

CognitoUserPool userPool = new CognitoUserPool(
          context, userPoolId, clientId, clientSecret); 

を次に、あなたが認証するユーザー選択:authentication handlerを書き、その後

​​

を。 Cognitoは、あなたが呼び出すのではなく、ユーザー名とパスワードが必要な場合にコードを呼び出します。

AuthenticationHandler handler = new AuthenticationHandler { 
    @Override 
    public void onSuccess(CognitoUserSession userSession) { 
     // Authentication was successful, the "userSession" will have the current valid tokens 
    } 

    @Override 
    public void getAuthenticationDetails(final AuthenticationContinuation continuation, final String userID) { 
     // User authentication details, userId and password are required to continue. 
     // Use the "continuation" object to pass the user authentication details 

     // After the user authentication details are available, wrap them in an AuthenticationDetails class 
     // Along with userId and password, parameters for user pools for Lambda can be passed here 
     // The validation parameters "validationParameters" are passed in as a Map<String, String> 
     AuthenticationDetails authDetails = new AuthenticationDetails(userId, password, validationParameters); 

     // Now allow the authentication to continue 
     continuation.setAuthenticationDetails(authDetails); 
     continuation.continueTask(); 
    } 

    /* Handle 2FA, challenges, etc as needed */ 
}; 

最後に、新しいセッションを取得し、ハンドラを提供してください。

user.getSession(handler); 

すべてがうまくいく場合は、有効なトークンを持つセッションが必要です。

この例は、developer guideに基づいており、新しいユーザーの登録、ログアウトなどの例もあります。

+0

申し訳ありませんが、私はMobile SDKを使用していません。私はJava SDKを使用しています。 – bdparrish

0

ユーザープールがある場合は、ユーザープールに対して認証する必要があります。 http://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-authentication-flow.htmlを参照してください。あなたはプロバイダー間でアイデンティティを集約する予定がある場合にのみ、連携アイデンティティ・プールを必要とする

private String calculateSecretHash(@Nonnull String userName) { 

    SecretKeySpec signingKey = new SecretKeySpec(m_clientSecret.getBytes(StandardCharsets.UTF_8), HmacAlgorithms.HMAC_SHA_256.toString()); 
    try { 
    Mac mac = Mac.getInstance(HmacAlgorithms.HMAC_SHA_256.toString()); 
    mac.init(signingKey); 
    mac.update(userName.getBytes(StandardCharsets.UTF_8)); 
    byte[] rawHmac = mac.doFinal(m_clientId.getBytes(StandardCharsets.UTF_8)); 
    return Base64.encodeBase64String(rawHmac); 

    } catch (Exception ex) { 
    throw new PgkbRuntimeException("Error calculating secret hash", ex); 
    } 
} 

Map<String, String> params = new HashMap<>(); 
params.put("USERNAME", userId); 
params.put("SECRET_HASH", calculateSecretHash(userId)); 
params.put("PASSWORD", rawPassword); 

AdminInitiateAuthRequest request = new AdminInitiateAuthRequest() 
    .withUserPoolId("YOUR_USER_POOL_ID") 
    .withClientId("YOUR_USER_POOL_APP_CLIENT_ID") 
    .withAuthFlow(AuthFlowType.ADMIN_NO_SRP_AUTH) 
    .withAuthParameters(params); 

AWSCognitoIdentityProvider identityProvider = AWSCognitoIdentityProviderClientBuilder.standard() 
     .withCredentials(credentialsProvider) 
     .withRegion(Regions.US_WEST_2) 
     .build(); 
AdminInitiateAuthResult result = identityProvider.adminInitiateAuth(request); 

ヘルパー機能:バックエンド用

、あなたはこのようなものを使用したいです。この場合、依然としてユーザープールに対して認証を行い、認証されたユーザーのIDをIDプールに対して使用する必要があります。

関連する問題