jonsaw / amazon-cognito-identity-dart

Unofficial Amazon Cognito Identity Provider Dart SDK, to easily add user sign-up and sign-in to your mobile and web apps with AWS.
MIT License
204 stars 93 forks source link

Added optional authenticator parameter which enables us to use social logins like Google or Facebook. #36

Open BerndWessels opened 5 years ago

BerndWessels commented 5 years ago

Rather than logging in with a Cognito User we want to be able to also login with Google or Facebook.

This can be achieved by logging in with a registered federated identity provider like Google:

    final GoogleSignIn googleSignIn = GoogleSignIn(scopes: ['email']);
    final GoogleSignInAccount googleUser = await googleSignIn.signIn();
    GoogleSignInAuthentication googleAuth = await googleUser.authentication;

Then using the providers identity token to get the Cognito credentials:

    final userPool = new CognitoUserPool('ap-southeast-2_xxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxxxxxxx');
    final credentials = new CognitoCredentials('ap-southeast-2:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', userPool);
    await credentials.getAwsCredentials(googleAuth.idToken, 'accounts.google.com');

Note that getAwsCredentials now allows to specify the authenticator as an optional parameter like 'accounts.google.com'.

And now we can make all our requests as usual:

      const endpoint = 'https://xxxxxxxxxx.execute-api.ap-southeast-2.amazonaws.com/default';
      final awsSigV4Client = new AwsSigV4Client(credentials.accessKeyId, credentials.secretAccessKey, endpoint, sessionToken: credentials.sessionToken, region: 'ap-southeast-2');
      final signedRequest = new SigV4Request(awsSigV4Client,
          method: 'POST',
          path: '/flutter',
          headers: new Map<String, String>.from({'header-1': 'one', 'header-2': 'two'}),
          queryParams: new Map<String, String>.from({'tracking': 'x123'}),
          body: new Map<String, dynamic>.from({'color': 'blue'}));
      http.Response response;
      response = await http.post(signedRequest.url, headers: signedRequest.headers, body: signedRequest.body);

This was requested here too

ghost commented 5 years ago

@BerndWessels : Thanks for the support of Google/Facebook login.

Moreover, we are using custom identity server authentication with AWS. As described here https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_oidc_manual.html

Should your implementation work with this also? Or It requires further customization?

furaiev commented 4 years ago

Hi all here, I've copied this project in a separate package (because this one isn't supported anymore) https://pub.dev/packages/amazon_cognito_identity_dart_2 Welcome to contribute.