放大Auth和Android:临时凭据-邮递员中的accessKey,secretKey无效

问题描述

我已经设置了一个Android应用程序以使用放大身份验证。

我已允许其接受未经授权的(来宾)用户和Google联合登录

一切似乎都按预期进行。我可以使用Google登录和Cognito登录

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

我已经在apigateway中建立了一个api(将宠物示例导入到我的apigateway中)。我可以使用允许策略调用API的用户来访问端点。我使用用户accessKey和secretKey在邮递员中进行了测试,并且可以正常工作。

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

((我已为所使用的身份池添加了allow调用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())
    );

我得到:

“消息”:“请求中包含的安全令牌无效。”

邮递员

与使用以下代码为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(),新的Callback(){ mobileClient.federatedSignIn(“ accounts.google.com”,account.getIdToken(),新的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());

....

感谢您提供帮助以解决此问题。谢谢

解决方法

设法进行排序。

”当您使用临时安全凭证进行呼叫时,该呼叫必须包含会话令牌,该令牌连同这些临时凭证一起返回。AWS使用会话令牌来验证临时安全凭证。临时凭证在指定的时间后失效间隔。”

我使用以下代码获取了sessionToken:

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