首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为科尼托承担跨账户角色?

如何为科尼托承担跨账户角色?
EN

Stack Overflow用户
提问于 2020-05-11 21:15:10
回答 1查看 787关注 0票数 1
  • 我在AWS帐户acc-1上有一个认知用户池,在acc-2上运行一个Java代码,它使用"adminInitiateAuth“进行身份验证,而且出于某些原因,我不能使用clientInitiateAuth。
  • 我在acc-1上创建了一个跨帐户角色,由我在acc-2上的Java代码承担。

问题:当我向科尼托发送身份验证请求时,我如何承担这个角色?可以使用withRoleArn()吗?

我遇到了页面,它解释了如何“使用API控制台为REST配置跨帐户Amazon授权程序”。但这不是我想要做的。

我的守则:

代码语言:javascript
复制
    protected AdminInitiateAuthRequest createInitialRequest(String username, String password) {
        Map<String, String> authParams = new HashMap<>();
        authParams.put("USERNAME", username);
        authParams.put("PASSWORD", password);

        return new AdminInitiateAuthRequest()
                .withAuthFlow(AuthFlowType.ADMIN_NO_SRP_AUTH)
                .withAuthParameters(authParams)
                .withClientId(whoAmIService.getCognitoClientId())
                .withUserPoolId(whoAmIService.getCognitoPoolId());
    }

protected boolean isAuthenticatedByCognito(String username, String password) {
        AWSCognitoIdentityProvider awsCognitoIDPClient = createCognitoIDPClient();
        AdminInitiateAuthRequest authRequest = createInitialRequest(username, password);

        try {
            AdminInitiateAuthResult authResponse = awsCognitoIDPClient.adminInitiateAuth(authRequest);
            AuthenticationResultType authenticationResultType = authResponse.getAuthenticationResult();
            String cognitoAccessToken = authenticationResultType.getAccessToken();
            whoAmIService.setCognitoAccessToken(cognitoAccessToken);

            Map<String, String> challengeParams = authResponse.getChallengeParameters();
            String cognitoUserIdForSrp = challengeParams.get("USER_ID_FOR_SRP");
            String cognitoUserAttributes = challengeParams.get("userAttributes");
            logger.debug("Cognito authenticated user ID: {} with user attributes: {}"
                    , cognitoUserIdForSrp, cognitoUserAttributes);
            return true;
        } catch (NotAuthorizedException nae) {
            logger.error("Invalid Cognito username/password provided for {}", username);
            return false;
        } catch (AWSCognitoIdentityProviderException acipe) {
            logger.error("Base exception for all service exceptions thrown by Amazon Cognito Identity Provider", acipe);
            return false;
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2020-05-20 23:11:09

我找到了如何用STS做这件事。更改这一行:

代码语言:javascript
复制
AWSCognitoIdentityProvider awsCognitoIDPClient = createCognitoIDPClient();

至:

代码语言:javascript
复制
String roleARN= "YOUR_CROSS_ACCOUNT_ROLE_ARN";
String roleSessionName = "GIVE_A_SESSION_NAME";
AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder
        .standard()
        .withCredentials(new ProfileCredentialsProvider())
        .build();
AssumeRoleRequest roleRequest = new AssumeRoleRequest()
        .withRoleArn(roleARN)
        .withRoleSessionName(roleSessionName);
AssumeRoleResult roleResponse = stsClient.assumeRole(roleRequest);
Credentials sessionCredentials = roleResponse.getCredentials();
BasicSessionCredentials awsCredentials = new BasicSessionCredentials(
        sessionCredentials.getAccessKeyId(),
        sessionCredentials.getSecretAccessKey(),
        sessionCredentials.getSessionToken());
AWSCognitoIdentityProvider cognitoIPCB = AWSCognitoIdentityProviderClientBuilder
        .standard()
        .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
        .build();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61739467

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档