0
votes

Following the answer here: https://github.com/aws/aws-sdk-ios/issues/357 At the very bottom there is a mini guide on getting swift and cognito working.

I've made a AWSCustomIdentityProvider as such:

import Foundation
import AWSCognitoIdentityProvider
import AWSCognito

class AWSCustomIdentityProvider: NSObject, AWSIdentityProviderManager
{

private var dict = NSDictionary()

func addToken(value:NSString)
{
    dict = ["graph.facebook.com":value]
}

public func logins() -> AWSTask<NSDictionary>
{
    return AWSTask(result: dict)
}
}

And I have a login method from facebook:

public func loginButtonDidCompleteLogin(_ loginButton: FacebookLogin.LoginButton, result: FacebookLogin.LoginResult){

    switch result {
    case .failed(let error):
        print("FACEBOOK LOGIN FAILED: \(error)")
    case .cancelled:
        print("User cancelled login.")
    case .success(_, _, let accessToken):


        let customIdentity = AWSCustomIdentityProvider()
        let token = accessToken.authenticationToken
        customIdentity.addToken(value: token as NSString)

        let credentialsProvider = AWSCognitoCredentialsProvider(regionType: REGIONTYPE, identityPoolId: "XXXXXXXXXXXXXXXXXXXXXXX", identityProviderManager:customIdentity)

        credentialsProvider.clearKeychain()
        credentialsProvider.clearCredentials()

        let serviceConfiguration = AWSServiceConfiguration(region: REDIONTYPE, credentialsProvider: credentialsProvider)
        AWSServiceManager.default().defaultServiceConfiguration = serviceConfiguration;

        credentialsProvider.getIdentityId().continue( { (task: AWSTask!) -> AnyObject! in
            if (task.error != nil) {
                print("Error: " + (task.error?.localizedDescription)!)// gets called
            }
            else {
                print(task.result)//identityid
            }
            return nil
        })
    }
}

However I get the error:

Error Domain=com.amazonaws.AWSCognitoIdentityErrorDomain Code=8 "(null)" UserInfo={__type=NotAuthorizedException, message=Logins don't match. Please include at least one valid login for this identity or identity pool.}

Please let me know if you have any idea on how to resolve my issue. I've tried also following the docs and setting the logins directly "credentialsProvider.logins = {"graph.facebook.com": mytoken}

and that produces a different exception upon invoking a lambda method but DOES retrieve the identityID properly. However doing it according to the docs makes a warning that the method I'm using is deprecated.

The error I get:

UserInfo={NSLocalizedDescription=serialized object is neither a valid json Object nor NSData object: }

However that only happens sometimes. If I retry then i can potentially get the identity id but upon invoking a lambda method, I get the same error. I'm assuming it is a cognito issue.

UPDATE

If I use AWSCognitoLoginProviderKey.facebook.rawValue instead of graph.facebook.com in the first part, then it gives me the cognito ID and then I invoke the lambda method. I'll include the lambda method just incase that's the part I'm getting wrong but I'm decently sure that it's cognito that is preventing me from calling the lambda method:

import AWSLambda
import Foundation
struct AWSHelper{
let lambda = AWSLambda.default()
let APPLICATION_NAME = "MYAPPLICATION"
init(){

}

func getFunctionName(funcName: String) -> String{
    return "\(funcName)_\(APPLICATION_NAME)"
}

func login(facebookID: String, cognitoID:String, callback:@escaping (Bool) -> Void){
    let req = AWSLambdaInvocationRequest();
    req?.invocationType = AWSLambdaInvocationType.requestResponse
    req?.payload = ["cognitoID" : cognitoID, "facebookID" : facebookID]
    req?.functionName = getFunctionName(funcName: "MYFUNCNAME")

    lambda.invoke(req!) { (response: AWSLambdaInvocationResponse?,error: Error?) in
        print(error)
        let payload = response?.payload
        print(payload)
        callback(true)
    }

}
}

Update 2

I have found out that calling a refresh method like this:

credentialsProvider.credentials().continue({ (task: AWSTask!) -> Any? in
            print(task.result)
        })

Causes an error like this:

AWSiOSSDK v2.4.10 [Error] AWSCredentialsProvider.m line:577 | __44-[AWSCognitoCredentialsProvider credentials]_block_invoke.352 | Unable to refresh. Error is [Error Domain=com.amazonaws.AWSCognitoIdentityErrorDomain Code=10 "(null)" UserInfo={__type=ResourceNotFoundException, message=Identity 'us-east-1:0db18266-1baa-4c59-9110-f9041dc92ead' not found.}]

I believe the big string that looks like an identitypoolID is actually the identityID for the given user that I have, so cognito has distributed an ID but is not able to query it?

2

2 Answers

0
votes

the error:

Logins don't match. Please include at least one valid login for this identity or identity pool

Can also occur because you attempt to log in as another user without logging out, so the token in the logins dictionary is compared to the identityId for a different identity (and doesn't match). In this case the SDK usually recovers by retrying, clearing and reestablishing the identityId, and then it works.

But in your case since you are constructing your own logins dictionary, the issue is more likely that you have constructed a token that does not match. You can inspect tokens using https://jwt.io. (though I admit it works for google and cognito user pools, but not on facebook tokens (I don't know why this is)),

I think doesn't match means that the identityId records a different unique user than is specified in the token.

Are you sure the token is constructed correctly?

As you mentioned... the documentation.. well .. I find the documentation is not worth looking at, So I set up my projects so I can review working code and set breakpoints.

Here is a snippet of the code from Mobile Hub Helper's Facebook AWSSignInProvider, which shows what they use to get the token (token.tokenstring).

    - (AWSTask<NSString *> *)token {
    FBSDKAccessToken *token = [FBSDKAccessToken currentAccessToken];
    NSString *tokenString = token.tokenString;
    NSDate *idTokenExpirationDate = token.expirationDate;

    if (tokenString
        // If the cached token expires within 10 min, tries refreshing a token.
        && [idTokenExpirationDate compare:[NSDate dateWithTimeIntervalSinceNow:AWSFacebookSignInProviderTokenRefreshBuffer]] == NSOrderedDescending) {
        return [AWSTask taskWithResult:tokenString];
    }

    AWSTaskCompletionSource *taskCompletionSource = [AWSTaskCompletionSource taskCompletionSource];
    [FBSDKLoginManager renewSystemCredentials:^(ACAccountCredentialRenewResult result, NSError *error) {
        if (result == ACAccountCredentialRenewResultRenewed) {
            FBSDKAccessToken *token = [FBSDKAccessToken currentAccessToken];
            NSString *tokenString = token.tokenString;
            taskCompletionSource.result = tokenString;
        } else {
            taskCompletionSource.error = error;
        }
    }];
    return taskCompletionSource.task;
}

Also... It bears mentioning. The AWSIdentityManager, and it's associated AWSSignInProviders is a nice architecture for getting signed in with Facebook and Google. Even if you don't use the rest of Mobile Hub Helper. Why re-invent the wheel, they did a very good job on the Identity portion of aws-mobilehub-helper-ios

I have a version of that library posted on github that adds an AWSSignInProvider for Cognito User Pools as well. SignIn-awsmhh it requires some fixes in the aws-mobilehub-helper-ios to use cognito user pools they are here aws-mobilehub-helper-ios (so if you clone do a clone --recursive and you will be set up for debugging using breakpoints in the library).

0
votes

Few things that made it work I think.

I made the correct move by making my own identityprovidermanager and I think the main thing that was blocking me from executing a lambda method was actually the fact that I was using AWSLambda instead of AWSLambdaInvoker. After I switched it started making errors that made sense.