CognitoSyncService
Status
Content
Installation
Add this line to your application's Gemfile:
gem 'cognito-sync-service', '~> 1.0'
And then execute:
$ bundle
Or install it yourself as:
$ gem install cognito-sync-service
Usage
AWS Cognito let use list of methods or this SDK methods for ruby.
In common case you need CRUD functionality, synchronization, between your DB and Cognito pools and handling authorization.
With this gem you can implement:
- CRUD functionality from you application
(WIP)
Synchronize you database users with user_pools(WIP)
Handling authorization
Step 1
Add required lib to you module or class
require 'aws-sdk-cognitoidentityprovider'
Create you cognito provider method in this module for example
for rails:
</code>
def cognito_provider ::Aws::CognitoIdentityProvider::Client.new( access_key_id: Rails.application.credentials.dig(:access_key_id), secret_access_key: Rails.application.credentials.dig(:secret_access_key), region: Rails.application.credentials.dig(Rails.env.to_sym, :region) ) end
> or with `ENV` variables
```ruby
def cognito_provider
::Aws::CognitoIdentityProvider::Client.new(
access_key_id: ENV['access_key_id'],
secret_access_key: ENV['secret_access_key'],
region: ENV['region']
)
end
or come up with any other way to implement the method cognito_prodiver
def cognito_provider # your code end
Step 2
Define two methods that will return
pool_id
andcliend_pool_id
withweb_pool_id
andweb_client_id
names
def web_pool_id
@web_pool_id ||= ENV['aws']['pool_id']
end
def web_client_id
@web_client_id ||= ENV['aws']['client_id']
end
or come up with any other way to implement the method cognito_prodiver
def web_pool_id
'us-east-1_DaexV9pOc' # Pool Id example
end
def web_client_id
'6d1rss9carten3pkl0134658g5p' # Client id example
end
Step 3
Above methods should be
extend
~~not included~~ to you classIn other words, the methods should be available to the
class
but not to the instance.
# example with User class
User.cognito_provider # => #<Aws::CognitoIdentityProvider::Client>
User.web_poool_id # => 'us-east-1_DaexV9pOc'
User.web_client_id # => '6d1rss9carten3pkl0134658g5p'
Step 4
Add required lib to you class
require 'cognito-sync-service'
extend cognito sync service in you class
extend CognitoSyncService
Primitive example
require 'aws-sdk-cognitoidentityprovider'
require 'cognito-sync-service'
class User
extend CognitoSyncService
def self.cognito_provider
::Aws::CognitoIdentityProvider::Client.new( access_key_id: "access_key_id", secret_access_key: "secret_access_key", region: "region")
end
def self.web_pool_id
@web_pool_id ||= "pool_id"
end
def self.web_client_id
@web_client_id ||= "client_id"
end
end
User.ca_create!({phone_number: "+1111111111"}, "+1111111111")
# => {
# "username"=>"fd09f027-bebf-4f43-abf9-248130145107f",
# "user_create_date"=>2077-77-11 16:27:14 +0300,
# "user_last_modified_date"=>2077-77-26 16:27:14 +0300,
# "enabled"=>true,
# "user_status"=>"FORCE_CHANGE_PASSWORD",
# "phone_number"=>"+1111111111"
#}
List of methods
naming note:
methods naming was inspired by cognito naming but we added a prefix for a slight difference and changed the names to more understandable for ruby developers
ca
_action => cognito_admin
_action
c
_action => cognito
_action
- #ca_create!
- #ca_update!
- #ca_delete!
- #ca_enable!
- #ca_disable!
- #ca_find!
- #ca_initiate_auth!
- #ca_refresh_tokens!
- #ca_respond_to_auth_challenge!
- #ca_set_user_password!
- #c_find_by_access_token!
Development
See DEVELOPMENT.md.
Contributing
See CONTRIBUTING.md.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the CognitoSyncService project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.