2016-11-05 1 views
0

문서를 통해 분명히 할 수있는 방법이 없다고 생각됩니다. 사용자 풀을 만든 다음 사용자 풀에 대해 Cognito에서 공급자를 만든 다음 사용자 이름과 암호를 인증하려면 어떻게해야합니까?사용자 신원 확인 풀 Amazon Cognito로 사용자 인증 - Java SDK

이 메시지는 sample이지만 암호는 Cognito가 아닌 별도의 데이터베이스에서 관리되는 것처럼 보입니다.

답변

0

Android 용 모바일 SDK를 사용 중이며 모든 설정을 완료했다고 가정합니다. 첫째, 당신은 사용자 풀에 연결하기를 원할 것입니다 :

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

을 그런 다음 인증 할 사용자 선택하십시오 authentication handler 쓰기, 그리고

CognitoUser user = userPool.getUser(userId); 

합니다. 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을 참조하십시오. 백 엔드에 대한

, 당신은 같은 것을 사용하십시오 :

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 풀을 필요

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); 
    } 
} 

. 이 경우에도 여전히 사용자 풀에 대해 인증하고 ID 풀에 대해 인증 된 사용자의 ID를 사용해야합니다.

관련 문제