将LWA令牌传递给Cognito

day*_*mer 9 amazon-web-services ios amazon-cognito aws-sdk login-with-amazon

我正在使用一个使用Alexa语音服务并维护不同用户的应用程序,因此用户需要使用亚马逊(LWA)登录.我已经实现了它,就像它是在文档中编写的,它可以完美地工作.

LWA docs:https://developer.amazon.com/de/docs/login-with-amazon/use-sdk-ios.html

AMZNAuthorizationManager.shared().authorize(request, withHandler: {(result : AMZNAuthorizeResult?, userDidCancel : Bool, error : Error?) -> () in
            if error != nil {
                // Handle errors from the SDK or authorization server.
            }
            else if userDidCancel {
                // Handle errors caused when user cancels login.
            }
            else {
                // Authentication was successful.
                // Obtain the access token and user profile data.
                self.accessToken = result!.token
                self.user = result!.user!
            }
        })
Run Code Online (Sandbox Code Playgroud)

此外,我需要从DynamoDB检索信息,该信息使用Cognito进行身份验证.正如文档中所述,应该有一种方法将访问令牌从LWA传递给Cognito,但我找不到合适的位置.他们说LWA提供了AMZNAccessTokenDelegate,但它没有提供.委托方法提供Cognito需要的API结果.下面Cognito文档中的链接指的是我在上面发布的LWA文档中的完全相同的链接.

Cognito文档:https://docs.aws.amazon.com/cognito/latest/developerguide/amazon.html

func requestDidSucceed(apiResult: APIResult!) {
    if apiResult.api == API.AuthorizeUser {
        AIMobileLib.getAccessTokenForScopes(["profile"], withOverrideParams: nil, delegate: self)
    } else if apiResult.api == API.GetAccessToken {
        credentialsProvider.logins = [AWSCognitoLoginProviderKey.LoginWithAmazon.rawValue: apiResult.result]
    }
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

[编辑]

我今天爬过LWA来源,直到我终于找到了正确的委托方法.

使用AIAuthenticationDelegate而不是AMZNAccessTokenDelegate

但这让我坐在接下来的两个问题面前:

一世.

Value of type 'AWSCognitoCredentialsProvider' has no member 'logins'
Run Code Online (Sandbox Code Playgroud)

也许我必须使用以下?

.setValue([AWSCognitoLoginProviderKey.LoginWithAmazon.rawValue: apiResult.result], forKey: "logins")
Run Code Online (Sandbox Code Playgroud)

II.

Use of unresolved identifier 'AWSCognitoLoginProviderKey'
Run Code Online (Sandbox Code Playgroud)

我在这放什么?也许我从LWA获得的API密钥?

[EDIT2]

我想尝试一下,但requestDidSucceed永远不会被调用,即使我成功登录也是如此.

小智 0

class CustomIdentityProvider: NSObject, AWSIdentityProviderManager {
    func logins() -> AWSTask<NSDictionary> {
        return AWSTask(result: loginTokens)
    }

    var loginTokens : NSDictionary
    init(tokens: [String : String]) {
        self.loginTokens = tokens as NSDictionary
    }   
}
Run Code Online (Sandbox Code Playgroud)

在下面的授权代码中成功

    AMZNAuthorizationManager.shared().authorize(request) { (result, userDidCancel, error) in
            if ((error) != nil) {
                // Handle errors from the SDK or authorization server.
            } else if (userDidCancel) {
                // Handle errors caused when user cancels login.
            } else {
                let logins = [IdentityProvider.amazon.rawValue: result!.token]
                let customProviderManager = CustomIdentityProvider(tokens: logins)
                guard let apiGatewayEndpoint = AWSEndpoint(url: URL(string: "APIGATEWAYURL")) else {
                    fatalError("Error creating API Gateway endpoint url")
                }
                let credentialsProvider = AWSCognitoCredentialsProvider(regionType: .USWest2, identityPoolId: "IDENTITY_ID", identityProviderManager:customProviderManager)
                let configuration = AWSServiceConfiguration(region: .USWest2, endpoint: apiGatewayEndpoint, credentialsProvider: credentialsProvider)
    }
Run Code Online (Sandbox Code Playgroud)