【问题标题】:Amplify Auth and Android: temporary credentials - accessKey, secretKey invalid in postman放大 Auth 和 Android:临时凭证 - 邮递员中的 accessKey、secretKey 无效
【发布时间】:2020-10-01 14:48:21
【问题描述】:

我已经设置了一个安卓应用来使用放大认证。

我已允许它接受未经授权的(访客)用户和谷歌联合登录。

这一切似乎都按预期工作。我可以使用 google 登录和 cognito 登录。

我在使用为 google 登录和访客用户生成的临时凭据时遇到问题。

我在 apigateway 中设置了一个 api(导入到我的 apigateway 中的宠物示例)。我可以使用具有允许调用 api 的策略的用户访问端点。我使用用户 accessKey 和 secretKey 在邮递员中进行测试,它可以工作。

当我使用 cognito 登录用户的 idToken 时,它也可以工作。

(我已将允许调用 api 添加到所用身份池的 auth 和 unauth 角色的策略中)

如果我使用此代码为访客生成的 accessKey 和 secretKey:

 public void getGuestCredentials(View view) {
    Log.i(TAG, "inside getGuestCredentials()...");
    Amplify.Auth.fetchAuthSession(
            result -> {
                AWSCognitoAuthSession cognitoAuthSession = (AWSCognitoAuthSession) result;
                Log.i(TAG, "Is user signed in: "+cognitoAuthSession.isSignedIn());

                switch(cognitoAuthSession.getIdentityId().getType()) {
                    case SUCCESS:
                        Log.i(TAG, "success IdentityId: " + cognitoAuthSession.getIdentityId().getValue());
                        Log.i(TAG, "success access key: " + cognitoAuthSession.getAWSCredentials().getValue().getAWSAccessKeyId());
                        Log.i(TAG, "success secret key: " + cognitoAuthSession.getAWSCredentials().getValue().getAWSSecretKey());
                        break;
                    case FAILURE:
                        Log.i(TAG, "failure IdentityId not present because: " + cognitoAuthSession.getIdentityId().getError().toString());
                        break;
                    default:
                        Log.i(TAG, "default IdentityId: " + cognitoAuthSession.getIdentityId().getValue());
                        Log.i(TAG, "default access key: " + cognitoAuthSession.getAWSCredentials().getValue().getAWSAccessKeyId());
                        Log.i(TAG, "default secret key: " + cognitoAuthSession.getAWSCredentials().getValue().getAWSSecretKey());
                        break;

                }
            },
            error -> Log.i("AuthQuickStart", error.toString())
    );

我明白了:

"message": "请求中包含的安全令牌无效。"

在邮递员中。

与使用此代码为 google 登录生成的密钥相同:

//            sign in as federated user using google token (using escape hatch)
        AWSMobileClient mobileClient = (AWSMobileClient) Amplify.Auth.getPlugin("awsCognitoAuthPlugin").getEscapeHatch();

// mobileClient.federatedSignIn(IdentityProvider.GOOGLE.toString(), account.getIdToken(), new Callback() { mobileClient.federatedSignIn("accounts.google.com", account.getIdToken(), new Callback() {

            @Override
            public void onResult(final UserStateDetails userStateDetails) {
                //Handle the result
                Log.i(TAG, "mobileClient login result: " + userStateDetails.getUserState().toString());
                Log.i(TAG, "success google federation, going to authenticated user page.... ");

// *************************************************** ***

                AWSCredentials credentials = mobileClient.getCredentials();
                Log.i(TAG, "***** secret key: "+credentials.getAWSSecretKey());
                Log.i(TAG, "***** access key: "+credentials.getAWSAccessKeyId());

....

感谢任何帮助解决这个问题。谢谢

【问题讨论】:

    标签: android authentication postman credentials amplify


    【解决方案1】:

    设法得到排序。

    “当您使用临时安全凭证进行调用时,调用必须包含一个会话令牌,该令牌与这些临时凭证一起返回。AWS 使用会话令牌来验证临时安全凭证。临时凭证在指定时间后过期间隔。”

    我使用此代码获得了 sessionToken:

    ((AWSSessionCredentials) AWSMobileClient.getInstance().getCredentials()).getSessionToken();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-27
      • 2020-07-18
      • 2022-01-13
      • 1970-01-01
      • 2017-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多