AWS Cognito和API Gateway中的Google+身份验证未经授权

时间:2017-06-21 20:02:19

标签: amazon-web-services authentication aws-sdk amazon-cognito google-authentication

AWS Wizards,

我已经创建了一个POC网络应用,用于测试项目的AWS CognitoFederated Identities

我用Google+ authentication扩展了POC。当用户通过Google进行身份验证时,Google Signin的计数器在联合身份验证中的数量正在增加,但它们显然已经过验证。

现在我正在尝试使用带有安全AWS API Gateway端点的凭据而没有运气。根据网关,用户将保持不通知状态。我已经在API网关授权程序配置中尝试了测试功能,如果我使用Cognito用户CognitoUserSession.idToken进行测试,则测试会返回声明。

如果我与Google用户idToken进行相同的测试,则表示未经身份验证。成功完成Google登录后,将执行相同的AWS Javascript SDK代码(作为Cognito用户登录)。凭证使用身份ID和会话令牌更新,但没有ID令牌。

由于我是AWS上的总菜鸟,我可能在这里遗漏了一些东西。我该怎么做才能获得有效的idToken?它应该以不同的方式完成吗?

GoogleLogin是React组件)

render() {
    return (
        <GoogleLogin
            clientId="44xxx11.apps.googleusercontent.com"
            buttonText="Google Login"
            onSuccess={this.responseGoogle}
            onFailure={this.responseGoogle}
        />
    )
}

responseGoogle(response) {
    console.log(response.tokenId)
    cognitoIdentityCredentials(response.tokenId)
}

cognitoIdentityCredentials(idToken) {

    const loginsObj = {
        ['accounts.google.com']: idToken
    }

    AWS.config.credentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId: IDENTITY_POOL_ID, // your identity pool id here
        Logins: loginsObj
    })

    AWS.config.credentials.get(function(err) {
        if ( err ) {
            console.log(err);
        } else {
            console.log("---- AWS credentials ----")
            console.log(AWS.config.credentials)
            // Got credentials, but no valid idToken
        }
    })
}

1 个答案:

答案 0 :(得分:0)

此问题的解决方案是您无法像我假设的那样真正使用AWS Security。在AWS API Gateway中,可以下载生成的库以专门访问该API,并且在使用lib时,我们发现请求中添加了更多字段,是的,它们已经签名。所有这些。

对不同的环境和应用程序使用不同的SDK库会很麻烦,因此我们只需使用其中一个SDK并将其作为一个通用库,我们可以将其用于所有Web项目中的所有请求。 Github上已有类似的项目,但不幸的是,他们只与Cognito用户合作,而不是联合身份。

快乐的熊猫了。