Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Examples of Ruby on rails + aws congnito

I am building a rails 5 app that is deployed on heroku. I want to use AWS congnito to achieve single sign on, but there are not enough example to implement it. I am using devise for authentication. Now my goal is to put my all users on AWS cognito and authenticate them from my rails App.

This is the only resource i found on AWS congnito with rails, I am looking for some example application or a link to tools or ruby API document to achieve this.

Please Help.

Update On basis Of Bala Answer

require 'aws-sdk'

ENV['AWS_ACCESS_KEY_ID'] = 'XXXXXXXXXXXXXXXXX'
ENV['AWS_SECRET_ACCESS_KEY'] = 'XXXX+XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
region_name = 'us-east-1'
endpoint = 'cognito-idp.us-east-1.amazonaws.com'

client = Aws::CognitoIdentityProvider::Client.new(
  region: region_name
)


resp = client.admin_create_user({
  user_pool_id: "us-east-1_iD7xNHj0x", # required
  username: "Test", # required
  user_attributes: [
    {
      name: "email", # required
      value: "[email protected]",
    },
  ],
  validation_data: [
    {
      name: "Email", # required
      value: "AttributeValueType",
    },
  ],
  temporary_password: "PasswordType",
  force_alias_creation: false,
  message_action: "RESEND", # accepts RESEND, SUPPRESS
  desired_delivery_mediums: ["EMAIL"], # accepts SMS, EMAIL
})

Error stack trace

home/sachin/.rvm/gems/ruby-2.1.5@global/gems/aws-sdk-core-2.6.38/lib/seahorse/client/plugins/raise_response_errors.rb:15:in `call': User does not exist. (Aws::CognitoIdentityProvider::Errors::UserNotFoundException)
    from /home/sachin/.rvm/gems/ruby-2.1.5@global/gems/aws-sdk-core-2.6.38/lib/aws-sdk-core/plugins/idempotency_token.rb:18:in `call'
    from /home/sachin/.rvm/gems/ruby-2.1.5@global/gems/aws-sdk-core-2.6.38/lib/aws-sdk-core/plugins/param_converter.rb:20:in `call'
    from /home/sachin/.rvm/gems/ruby-2.1.5@global/gems/aws-sdk-core-2.6.38/lib/seahorse/client/plugins/response_target.rb:21:in `call'
    from /home/sachin/.rvm/gems/ruby-2.1.5@global/gems/aws-sdk-core-2.6.38/lib/seahorse/client/request.rb:70:in `send_request'
    from /home/sachin/.rvm/gems/ruby-2.1.5@global/gems/aws-sdk-core-2.6.38/lib/seahorse/client/base.rb:207:in `block (2 levels) in define_operation_methods'
    from aws_cognito.rb:20:in `<main>'

Update 2

resp = client.admin_initiate_auth({
  user_pool_id: "us-east-1_uKM", # required
  client_id: "3g766413826eul9kre28qne4f", # required
  auth_flow: "ADMIN_NO_SRP_AUTH",
  auth_parameters: {
    "EMAIL" => "[email protected]",
    "PASSWORD" => "Ibms#1234"
  }
})
like image 381
Sachin Singh Avatar asked Dec 19 '16 12:12

Sachin Singh


1 Answers

First of all, you need to create a user pool for your application

Use this link to create user pool through AWS console

You can find the ruby methods for sign_up, sign_in, change password and many other functions at http://docs.aws.amazon.com/sdkforruby/api/Aws/CognitoIdentityProvider/Client.html

EDIT

Now, you can sign up the user using sign_up

sign_in a user using admin_initiate_auth

if you need mobile number confirmation, email confirmation you need to configure the user pool that you are creating.

You can find the corresponding methods for confirming the mobile numbers using http://docs.aws.amazon.com/sdkforruby/api/Aws/CognitoIdentityProvider/Client.html#confirm_sign_up-instance_method

like image 162
Bala Karthik Avatar answered Oct 09 '22 16:10

Bala Karthik