Module: CognitoSyncService

Includes:
CognitoAttributesConverter
Defined in:
lib/cognito-sync-service.rb,
lib/cognito-sync-service/version.rb

Overview

require ‘cognito_provider.rb’

Constant Summary collapse

VERSION =
'1.1.0'

Instance Method Summary collapse

Methods included from CognitoAttributesConverter

#cognito_custom_attr_keys, #cognito_default_attr_keys, #cognito_key?, #cognito_key_name, #convert_from_cognito, #convert_to_cognito, #list_cognito_attr_keys, #list_cognito_custom_attr_keys, #list_cognito_default_attr_keys, #user_attributes

Instance Method Details

#c_find_by_access_token!(access_token) ⇒ Object

return user attributes by access token



109
110
111
112
# File 'lib/cognito-sync-service.rb', line 109

def c_find_by_access_token!(access_token)
  user = cognito_provider.get_user(access_token: access_token)
  convert_from_cognito(user)
end

#ca_create!(attrs, username, temporary_password = nil) ⇒ Object

username - can be email, phone_number or custom string depend on you cognito pool settings attrs - hash of user attributes which will be saved in cognito pool attrs = { email: ‘qwe@qwe,com’, phone_number: ‘+12.…..0’}



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cognito-sync-service.rb', line 14

def ca_create!(attrs, username, temporary_password = nil)
  user_attributes = {
    user_pool_id: web_pool_id,
    username: username,
    user_attributes: convert_to_cognito(attrs),
    temporary_password: temporary_password
  }.compact

  user = cognito_provider.admin_create_user(user_attributes).user

  convert_from_cognito(user)
end

#ca_delete!(username) ⇒ Object

user can be delete by email or phone_number depend on cognito pool settings



28
29
30
# File 'lib/cognito-sync-service.rb', line 28

def ca_delete!(username)
  cognito_provider.admin_delete_user(user_pool_id: web_pool_id, username: username)
end

#ca_disable!(username) ⇒ Object

user can be disable by email or phone_number depend on cognito pool settings



48
49
50
# File 'lib/cognito-sync-service.rb', line 48

def ca_disable!(username)
  cognito_provider.admin_disable_user(user_pool_id: web_pool_id, username: username)
end

#ca_enable!(username) ⇒ Object

user can be enable by email or phone_number depend on cognito pool settings



53
54
55
# File 'lib/cognito-sync-service.rb', line 53

def ca_enable!(username)
  cognito_provider.admin_enable_user(user_pool_id: web_pool_id, username: username)
end

#ca_find!(username) ⇒ Object

user can be find by email or phone_number depend on cognito pool settings



33
34
35
36
# File 'lib/cognito-sync-service.rb', line 33

def ca_find!(username)
  user = cognito_provider.admin_get_user(user_pool_id: web_pool_id, username: username)
  convert_from_cognito(user)
end

#ca_initiate_auth!(username, password) ⇒ Object

user can be authenticated by email or phone_number depend on cognito pool settings password can be constant or temporary “auth_flow” arg may differ depend on you authorization rules, we hardcode ADMIN_NO_SRP_AUTH like a common case In future this action be more flexible List of auth_flow args here - docs.aws.amazon.com/cli/latest/reference/cognito-idp/initiate-auth.html



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cognito-sync-service.rb', line 62

def ca_initiate_auth!(username, password)
  cognito_provider.admin_initiate_auth(
    user_pool_id: web_pool_id,
    client_id: web_client_id,
    auth_flow: 'ADMIN_NO_SRP_AUTH',
    auth_parameters: {
      USERNAME: username,
      PASSWORD: password
    }
  )
end

#ca_refresh_tokens!(refresh_token) ⇒ Object

user can refresh access token and id token by passing in a valid refresh token REFRESH_TOKEN_AUTH - Authentication flow for refreshing the access token and ID token by supplying a valid refresh token



76
77
78
79
80
81
82
83
84
85
# File 'lib/cognito-sync-service.rb', line 76

def ca_refresh_tokens!(refresh_token)
  cognito_provider.admin_initiate_auth(
    user_pool_id: web_pool_id,
    client_id: web_client_id,
    auth_flow: 'REFRESH_TOKEN_AUTH',
    auth_parameters: {
      REFRESH_TOKEN: refresh_token
    }
  )
end

#ca_respond_to_auth_challenge!(username, password, session) ⇒ Object

for now this method works only for password confirmation flow



88
89
90
91
92
93
94
95
96
# File 'lib/cognito-sync-service.rb', line 88

def ca_respond_to_auth_challenge!(username, password, session)
  cognito_provider.admin_respond_to_auth_challenge(
    user_pool_id: web_pool_id,
    client_id: web_client_id,
    challenge_name: 'NEW_PASSWORD_REQUIRED',
    session: session,
    challenge_responses: { USERNAME: username, NEW_PASSWORD: password }
  )
end

#ca_set_user_password!(username, password) ⇒ Object

works with any user after this method has been performed your account_status will be FORCE_CHANGE_PASSWORD



100
101
102
103
104
105
106
# File 'lib/cognito-sync-service.rb', line 100

def ca_set_user_password!(username, password)
  cognito_provider.admin_set_user_password(
    user_pool_id: web_pool_id,
    username: username,
    password: password
  ).to_h
end

#ca_update!(attrs, username) ⇒ Object

username - can be email, phone_number or custom string depend on cognito pool settings attrs - hash of user attributes which will be saved in cognito pool attrs = { email: ‘qwe@qwe,com’, phone_number: ‘+12.…..0’}



41
42
43
44
45
# File 'lib/cognito-sync-service.rb', line 41

def ca_update!(attrs, username)
  c_attributes = convert_to_cognito(attrs)
  cognito_provider.admin_update_user_attributes(user_pool_id: web_pool_id, username: username, user_attributes: c_attributes)
  ca_find!(username)
end