AWS Mobile Blog

Announcing Twitter and Digits Support for Amazon Cognito

by Bob Kinney | on | Permalink | Comments |  Share

When Amazon Cognito was first launched, we included support for Facebook, Google, Login with Amazon, as well as unauthenticated identities.

In response to customer requests, we added OpenID Connect support as well as developer authenticated identities to enable developers to extend Amazon Cognito to support any aribitrary provider. Even though it was possible to add support for any provider, many customers continued to ask for native support for Twitter authentication without the overhead of a managed backend.

Today, we are excited to announce native support in Amazon Cognito for not only Twitter authentication, but also Digits authentication provided by Twitter’s Fabric platform. Amazon Cognito’s support for Digits enables developers to provide the simplest onboarding flow for their users. Users login with only their phone number to receive a code via SMS and never have to worry about entering a username/password.

This post will cover how you can configure Twitter/Digits login for Amazon Cognito to authenticate your users.

Configuring Your Identity Pool for Twitter/Digits

Twitter/Digits will appear as a tab in the Authentication providers section in the Amazon Cognito console when creating a new identity pool or editing an existing pool.

You will need to enter two pieces of information in the console to configure Twitter and Digits support:

  • Consumer Key
  • Consumer Secret

These values are available from the Fabric console after you have integrated Fabric into your application.

After you add these values in the identity pool configuration, you are ready to start authenticating users via Twitter and Digits.

Authenticating users with Twitter

Using the TwitterKit SDK login functionality, we need to simply capture the result of the completion handler and pass the appropriate fields to Amazon Cognito.

Twitter sessions contain two important values:

  • User token
  • User secret

Amazon Cognito expects these values to be passed in a single value in logins with the key api.twitter.com, concatenated with a single semicolon (;) delimeter.

iOS

Objective-C

[[Twitter sharedInstance] logInWithCompletion:^
            (TWTRSession *session, NSError *error) {
  if (session) {
      NSString value = [NSString stringWithFormat:@"%@;%@", session.authToken, session.authTokenSecret];
      // Note: This overrides any existing logins
      credentialsProvider.logins = @{@"api.twitter.com", value};
  } else {
      NSLog(@"error: %@", [error localizedDescription]);
  }
}];

Swift

Twitter.sharedInstance().logInWithCompletion {
                (session, error) -> Void in
    if (session != nil) {
        var value = session.authToken + ";" + session.authTokenSecret
        // Note: This overrides any existing logins
        credentialsProvider.logins = ["api.twitter.com": value]
    } else {
        println("error: (error.localizedDescription)")
    }
}

Android

loginButton = (TwitterLoginButton)
                              findViewById(R.id.login_button);
loginButton.setCallback(new Callback<TwitterSession>() {
   @Override
   public void success(Result<TwitterSession> result) {
       TwitterSession session = result.data;
       TwitterAuthToken authToken = session.getAuthToken();
       String value = authToken.token + ";" + authToken.secret;
       Map<String, String> logins = new HashMap<String, String>();
       logins.put("api.twitter.com", value);
       // Note: This overrides any existing logins
       credentialsProvider.setLogins(logins);
   }
 
   @Override
   public void failure(TwitterException exception) {
       // Do something on failure
   }
});

Authenticating users with Digits

Digits support is handled through a separate call in the Fabric SDK, but the session returned is essentially the same as with Twitter login. You just need to concatenate the values from the session using a single semicolon (;) delimiter and store in logins with the key of www.digits.com.

iOS

Objective-C

[[Digits sharedInstance] authenticateWithCompletion:^
            (DGTSession* session, NSError *error) {
    if (session) {
        NSString value = [NSString stringWithFormat:@"%@;%@", session.authToken, session.authTokenSecret];
      // Note: This overrides any existing logins
      credentialsProvider.logins = @{@"www.digits.com", value};
    }
}];

Swift

let digits = Digits.sharedInstance()
digits.authenticateWithCompletion { (session, error) in
    if (session != nil) {
        var value = session.authToken + ";" + session.authTokenSecret
        // Note: This overrides any existing logins
        credentialsProvider.logins = ["www.digits.com": value]
    }
}

Android

DigitsAuthButton digitsButton = (DigitsAuthButton) findViewById(R.id.auth_button);
digitsButton.setCallback(new AuthCallback() {
   @Override
   public void success(DigitsSession session,
                                    String phoneNumber) {
       TwitterAuthToken authToken = (TwitterAuthToken)session.getAuthToken();
       String value = authToken.token + ";" + authToken.secret;
       Map<String, String> logins = new HashMap<String, String>();
       logins.put("www.digits.com", value);
       // Note: This overrides any existing logins
       credentialsProvider.setLogins(logins);
   }
 
   @Override
   public void failure(DigitsException exception) {
       // Do something on failure
   }
});

Conclusions

As you can see, adding Twitter and Digits support is quite simple once you integrate the necessary SDKs in your application. We’ve updated our Anrdoid and iOS samples to include support for Twitter.

We encourage you to try out this new support and let us know what you think of this new feature by leaving a comment below, or by posting on our forums or StackOverflow if you encounter any issues.