cxf 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +12 -0
  3. data/README.md +395 -0
  4. data/lib/client.rb +447 -0
  5. data/lib/contact/config/config.rb +4 -0
  6. data/lib/contact/content/content.rb +4 -0
  7. data/lib/contact/ecommerce/ecommerce.rb +4 -0
  8. data/lib/contact.rb +284 -0
  9. data/lib/cxf/controllers/admin_base_controller.rb +17 -0
  10. data/lib/cxf/controllers/base_api_controller.rb +28 -0
  11. data/lib/cxf/controllers/base_controller.rb +54 -0
  12. data/lib/cxf/controllers/concerns/cxf_clients.rb +104 -0
  13. data/lib/cxf/controllers/concerns/read_config_file.rb +30 -0
  14. data/lib/cxf/controllers/contact_api_controller.rb +17 -0
  15. data/lib/cxf/controllers/public_api_controller.rb +14 -0
  16. data/lib/cxf/controllers/user_api_controller.rb +16 -0
  17. data/lib/cxf/helpers/contact_auth_helper.rb +86 -0
  18. data/lib/cxf/helpers/cxf_helper.rb +52 -0
  19. data/lib/cxf/helpers/proxy_controllers_methods.rb +144 -0
  20. data/lib/cxf/helpers/threads_helper.rb +109 -0
  21. data/lib/cxf/helpers/user_auth_helper.rb +74 -0
  22. data/lib/cxf.rb +15 -0
  23. data/lib/errors.rb +109 -0
  24. data/lib/generators/cxf_assets_controller.rb +7 -0
  25. data/lib/generators/cxf_config.yml.erb +27 -0
  26. data/lib/generators/cxf_contact_controller.rb +7 -0
  27. data/lib/generators/cxf_files_generator.rb +28 -0
  28. data/lib/generators/cxf_public_controller.rb +7 -0
  29. data/lib/generators/cxf_user_controller.rb +7 -0
  30. data/lib/pub/config/config.rb +6 -0
  31. data/lib/pub/content/assets.rb +16 -0
  32. data/lib/pub/content/content.rb +9 -0
  33. data/lib/pub/ecommerce/ecommerce.rb +6 -0
  34. data/lib/pub.rb +163 -0
  35. data/lib/user/config/attribute_groups.rb +79 -0
  36. data/lib/user/config/attributes.rb +88 -0
  37. data/lib/user/config/calendars.rb +91 -0
  38. data/lib/user/config/config.rb +23 -0
  39. data/lib/user/config/relationships.rb +141 -0
  40. data/lib/user/config/seeds.rb +55 -0
  41. data/lib/user/config/system_settings.rb +54 -0
  42. data/lib/user/config/tags.rb +61 -0
  43. data/lib/user/config/taxonomies.rb +120 -0
  44. data/lib/user/config/users.rb +77 -0
  45. data/lib/user/config/views.rb +68 -0
  46. data/lib/user/contacts/contacts.rb +22 -0
  47. data/lib/user/content/assets.rb +294 -0
  48. data/lib/user/content/block_templates.rb +72 -0
  49. data/lib/user/content/blocks.rb +109 -0
  50. data/lib/user/content/content.rb +174 -0
  51. data/lib/user/content/instances.rb +121 -0
  52. data/lib/user/content/print_versions.rb +129 -0
  53. data/lib/user/content/stories.rb +110 -0
  54. data/lib/user/content/story_templates.rb +97 -0
  55. data/lib/user/content/templates.rb +72 -0
  56. data/lib/user/crm/companies.rb +111 -0
  57. data/lib/user/crm/contacts.rb +294 -0
  58. data/lib/user/crm/crm.rb +9 -0
  59. data/lib/user/ecommerce/ecommerce.rb +29 -0
  60. data/lib/user/ecommerce/item_prices.rb +89 -0
  61. data/lib/user/ecommerce/locations.rb +171 -0
  62. data/lib/user/ecommerce/price_lists.rb +75 -0
  63. data/lib/user/ecommerce/product_templates.rb +106 -0
  64. data/lib/user/ecommerce/product_variations.rb +133 -0
  65. data/lib/user/ecommerce/product_versions.rb +107 -0
  66. data/lib/user/ecommerce/products.rb +156 -0
  67. data/lib/user/ecommerce/skus.rb +90 -0
  68. data/lib/user/ecommerce/taxes.rb +84 -0
  69. data/lib/user/ecommerce/variant_options.rb +71 -0
  70. data/lib/user/ecommerce/variant_values.rb +74 -0
  71. data/lib/user/ecommerce/vouchers.rb +90 -0
  72. data/lib/user/helpers/helpers.rb +116 -0
  73. data/lib/user/helpers/object_activities.rb +85 -0
  74. data/lib/user/helpers/object_folders.rb +84 -0
  75. data/lib/user/helpers/user_folders.rb +85 -0
  76. data/lib/user/marketing/marketing.rb +123 -0
  77. data/lib/user/profile/profile.rb +104 -0
  78. data/lib/user.rb +98 -0
  79. metadata +227 -0
data/lib/contact.rb ADDED
@@ -0,0 +1,284 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './client'
4
+ require_relative './cxf/helpers/cxf_helper'
5
+ require_relative './cxf/helpers/threads_helper'
6
+ require_relative './contact/content/content'
7
+ require_relative './contact/config/config'
8
+ require_relative './contact/ecommerce/ecommerce'
9
+
10
+ include ActionController::Cookies
11
+
12
+ module Cxf
13
+ class Contact
14
+ include ContactConfig
15
+ include ContactContent
16
+ include ContactEcommerce
17
+ include CxfHelper
18
+ include ThreadsHelper
19
+
20
+ attr_reader :client
21
+
22
+ ##
23
+ # === Initialize.
24
+ # Class constructor.
25
+ #
26
+ # ==== Parameters
27
+ # host:: (String) -- It's the visitor IP.
28
+ # api_key:: (String) -- Cxf instance api key.
29
+ # contact_token_id:: (Integer) -- Cookie 'cxf_contact_id' value (cxf_contact_token).
30
+ #
31
+ # ==== Return
32
+ # Returns a Contact object
33
+ def initialize(
34
+ host,
35
+ api_key,
36
+ session_token = nil,
37
+ refresh_token = nil,
38
+ contact_token_id = nil,
39
+ debug = false,
40
+ timeouts = {}
41
+ )
42
+ @contact_v1_url = '/api/contact/v1'
43
+ @client = Cxf::Client.new(
44
+ host,
45
+ api_key,
46
+ 'contact',
47
+ session_token,
48
+ refresh_token,
49
+ contact_token_id,
50
+ nil,
51
+ debug,
52
+ timeouts
53
+ )
54
+ end
55
+
56
+ ### V1/CONTACTS ###
57
+
58
+ ##
59
+ # === Register.
60
+ # Register a contact.
61
+ #
62
+ # ==== Parameters
63
+ # data:: (Hash) -- It's the register data.
64
+ #
65
+ # ==== Example
66
+ # data = {
67
+ # email: 'email@example.com',
68
+ # given_name: 'Given Name',
69
+ # last_name: 'Last Name',
70
+ # password: 'password'
71
+ # }
72
+ # @cxf_contact.register(data);
73
+ def register(data)
74
+ @client.raw('post', '/contacts/register', nil, data_transform(data))
75
+ end
76
+
77
+ ##
78
+ # === Login.
79
+ # Starts a contact session.
80
+ #
81
+ # ==== Parameters
82
+ # email:: (String) -- The email that will be logged.
83
+ # password:: (String) -- The password of the email.
84
+ #
85
+ # ==== Example
86
+ # @cxf_contact.login('email@example.com', 'password')
87
+ def login(email, password)
88
+ data = {
89
+ email: email,
90
+ password: password
91
+ }
92
+ response = @client.raw('post', '/contacts/login', nil, data_transform(data))
93
+ if response.key? 'data' and response['data'].key? 'access_token'
94
+ @client.session_token = response['data']['access_token']
95
+ @client.refresh_token = response['data']['refresh_token']
96
+ end
97
+ response
98
+ end
99
+
100
+ ##
101
+ # === Recover Password.
102
+ # Send a email that contains a token to a contact. That token will be used in reset_password to establish a new password.
103
+ #
104
+ # ==== Parameters
105
+ # data:: (Hash) -- It's a data key where will be hosted the destination email.
106
+ #
107
+ # ==== Example
108
+ # data = { email: 'email@example.com' }
109
+ # @cxf_contact.recover_password(data)
110
+ def recover_password(data)
111
+ @client.raw('post', '/contacts/recover-password', nil, data_transform(data))
112
+ end
113
+
114
+ ##
115
+ # === Reset Password.
116
+ # Reset password using a token. The token is obtained by recover_password method.
117
+ #
118
+ # ==== Parameters
119
+ # data:: (Hash) -- It's a set of data which contains all the information to reset a contact password.
120
+ #
121
+ # ==== Example
122
+ # data = {
123
+ # email: 'email@example.com',
124
+ # password: 'password',
125
+ # password_confirmation: 'password',
126
+ # token: '644aa3aa0831d782cc42e42b11aedea9a2234389af4f429a8d96651295ecfa09'
127
+ # }
128
+ # @cxf_contact.reset_password(data)
129
+ def reset_password(data)
130
+ @client.raw('post', '/contacts/reset-password', nil, data_transform(data))
131
+ end
132
+
133
+ ##
134
+ # === OAuth Login.
135
+ # Login a contact using oauth.
136
+ def oauth_login(data)
137
+ @client.raw('post', '/contacts/oauth-login', nil, data)
138
+ end
139
+
140
+ ##
141
+ # === Magic Link Login.
142
+ # Starts a contact session with a token received in the contact email. The token will be received by send_magic_link method.
143
+ #
144
+ # ==== Parameters
145
+ # token:: (String) -- The email token that will be used to log in.
146
+ #
147
+ # ==== Example
148
+ # @cxf_contact.magic_link_login(
149
+ # 'd8618c6d-a165-41cb-b3ec-d053cbf30059:zm54HtRdfHED8dpILZpjyqjPIceiaXNLfOklqM92fveBS0nDtyPYBlI4CPlPe3zq'
150
+ # )
151
+ def magic_link_login(token)
152
+ response = @client.raw('get', "/contacts/magic-link-login/#{token}", nil, '/api/v1')
153
+ @client.session_token = response['session_token'] if response.key? 'session_token'
154
+
155
+ response
156
+ end
157
+
158
+ ##
159
+ # === Send Magic Link.
160
+ # Send magic link to contact by email. That magic link will be used in magic_link_login method.
161
+ #
162
+ # ==== Parameters
163
+ # email_or_phone:: (String) -- Contact's email.
164
+ # template_slug:: (String) -- Email template's slug to be used in the email.
165
+ # redirectUrl:: (String) -- Url to be redirected in the implemented page.
166
+ # lifeTime:: (Integer) -- Maximum time of use in minutes.
167
+ # maxVisits:: (Integer) -- The maximum number of uses of a token.
168
+ #
169
+ # ==== First Example
170
+ # @cxf_contact.send_magic_link('email@example.com', 'template_slug')
171
+ #
172
+ # ==== Second Example
173
+ # @cxf_contact.send_magic_link('+526561234567', 'template_slug', '', 1440, 3, 'whatsapp')
174
+ def send_magic_link(email_or_phone, template_slug, redirect_url = '', life_time = 1440, max_visits = nil, driver = 'email')
175
+ data = {
176
+ driver: driver,
177
+ lifeTime: life_time,
178
+ maxVisits: max_visits,
179
+ redirectUrl: redirect_url,
180
+ templateId: template_slug
181
+ }
182
+ if %w[sms whatsapp].include? driver
183
+ data['phone'] = email_or_phone
184
+ else
185
+ data['email'] = email_or_phone
186
+ end
187
+ @client.raw('post', '/contacts/magic-link', nil, data_transform(data), '/api/v1')
188
+ end
189
+
190
+ ### CONTACT/V1 ###
191
+
192
+ ##
193
+ # === Me.
194
+ # Get contact logged info.
195
+ #
196
+ # ==== Parameters
197
+ # # options:: (Hash) -- List of {Resource collection Options}[#class-Cxf::Pub-label-Resource+collections+options+] shown above can be used as parameter.
198
+ #
199
+ # ==== First Example
200
+ # @data = @cxf_contact.me
201
+ #
202
+ # ==== Second Example
203
+ # options = {
204
+ # attributes: true,
205
+ # taxonomies: true
206
+ # }
207
+ # @data = @cxf_contact.me(options)
208
+ def me(options = nil)
209
+ @client.raw('get', '/me', options, nil, @contact_v1_url)
210
+ end
211
+
212
+ ##
213
+ # === Status.
214
+ # Get contact logged status.
215
+ #
216
+ # ==== Example
217
+ # @data = @cxf_contact.status
218
+ def status
219
+ @client.raw('get', '/status', nil, nil, @contact_v1_url)
220
+ end
221
+
222
+ ##
223
+ # === Update.
224
+ # Update logged contact attributes.
225
+ #
226
+ # ==== Parameters
227
+ # data:: (Hash) -- It's the data to update with a session active.
228
+ #
229
+ # ==== Example
230
+ # data = {
231
+ # given_name: 'Given Name',
232
+ # last_name: 'Last Name'
233
+ # }
234
+ # @data = @cxf_contact.update(data)
235
+ def update(data)
236
+ @client.raw('put', '/update', nil, data_transform(data), @contact_v1_url)
237
+ end
238
+
239
+ ##
240
+ # === Logout.
241
+ # Ends a contact session previously logged.
242
+ #
243
+ # ==== Example
244
+ # @data = @cxf_contact.logout
245
+ def logout
246
+ if session_token?
247
+ response = @client.raw('post', '/logout', nil, nil, @contact_v1_url)
248
+ @client.session_token = nil if response['success']
249
+
250
+ response
251
+ end
252
+ end
253
+
254
+ ##
255
+ # === Change Password.
256
+ # Change password without email. To change the password a contact must be logged.
257
+ #
258
+ # ==== Parameters
259
+ # data:: (Hash) -- A new password allocated in a data key.
260
+ #
261
+ # ==== Example
262
+ # data = { password: 'new_password' }
263
+ # @data = @cxf_contact.change_password(data)
264
+ def change_password(data)
265
+ @client.raw('post', '/change-password', nil, data_transform(data), @contact_v1_url)
266
+ end
267
+
268
+ def get_client
269
+ @client
270
+ end
271
+
272
+ private
273
+
274
+ include CxfHelper
275
+
276
+ def session_token?
277
+ unless @client.session_token
278
+ Cxf::DynamicError.new(@client, 'Unauthorized', 'Attach contact session token', 401, nil)
279
+ end
280
+
281
+ true
282
+ end
283
+ end
284
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './concerns/cxf_clients'
4
+ require_relative '../helpers/user_auth_helper'
5
+
6
+ module Cxf
7
+ class AdminBaseController < ActionController::Base
8
+
9
+ include CxfClients
10
+ include UserAuthHelper
11
+
12
+ # Override default values for cxf clients concern
13
+ def define_cxf_clients
14
+ %w[user service_account]
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './concerns/cxf_clients'
4
+ require_relative '../helpers/user_auth_helper'
5
+ require_relative '../helpers/contact_auth_helper'
6
+
7
+ module Cxf
8
+ class BaseApiController < ActionController::Base
9
+ # Concerns
10
+ include CxfClients
11
+
12
+ # Helpers
13
+ include ContactAuthHelper
14
+ include UserAuthHelper
15
+
16
+ def define_cxf_clients
17
+ %w[contact pub]
18
+ end
19
+
20
+ ##
21
+ # === Cxf contact Login.
22
+ # This method works to override the base to add the redirect parameter
23
+ # The main method is located in contact_auth_helper.rb
24
+ def cxf_contact_magic_link_login(hash)
25
+ super(hash, true)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './concerns/cxf_clients'
4
+ require_relative '../helpers/contact_auth_helper'
5
+
6
+ module Cxf
7
+ class BaseController < ActionController::Base
8
+ # Concerns
9
+ include CxfClients
10
+
11
+ # Helpers
12
+ include ContactAuthHelper
13
+
14
+ before_action :register_visit
15
+
16
+ # Override default values for cxf clients concern
17
+ def define_cxf_clients
18
+ %w[contact pub]
19
+ end
20
+
21
+ private
22
+
23
+ ##
24
+ # === Register visit.
25
+ # Call register visit method from the public client and set/renew the cookie cxf_contact_id
26
+ def register_visit
27
+ if @debug
28
+ puts "REQUEST IN REGISTER VISIT: #{request}"
29
+ puts "BODY REQUEST: #{request.body}"
30
+ puts "AUTH REQUEST: #{request.authorization}"
31
+ puts "LENGTH REQUEST: #{request.content_length}"
32
+ puts "FORM DATA REQUEST: #{request.form_data?}"
33
+ puts "FULLPATH REQUEST: #{request.fullpath}"
34
+ puts "HEADERS REQUEST: #{request.headers}"
35
+ puts "IP REQUEST: #{request.ip}"
36
+ puts "REQUEST IP ADDRESS: #{request['ip_address']}"
37
+ puts "REQUEST REMOTE IP: #{request['remote_ip']}"
38
+ end
39
+
40
+ response = @cxf_pub.register_visit(request)
41
+
42
+ puts "RESPONSE IN REGISTER VISIT: #{response}" if @debug
43
+
44
+ @contact_token = response['contact_token'] || response['user_token']
45
+ @visit_id = response['visit_id']
46
+
47
+ puts "VISIT ID: #{@visit_id}" if @debug
48
+
49
+ cookies.permanent[:cxf_contact_id] = { value: @contact_token, secure: true, httponly: true }
50
+ cookies.permanent[:cxf_visit_id] = { value: @visit_id, secure: true, httponly: true }
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "./read_config_file"
4
+
5
+ module CxfClients
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ include ReadConfigFile
10
+ before_action :set_cxf_clients
11
+ end
12
+
13
+ # Define the clients that will have
14
+ # Override in the controller if you dont wanna all clients
15
+ def define_cxf_clients
16
+ %w[contact user pub service_account]
17
+ end
18
+
19
+ private
20
+
21
+ ##
22
+ # === Set cxf clients (pub, user and contact)
23
+ # Initialize all clients from cxf
24
+ def set_cxf_clients
25
+ valid_clients = %w[contact user pub service_account]
26
+
27
+ clients = define_cxf_clients&.select { |client| valid_clients.include? client } || []
28
+
29
+ if @debug
30
+ puts "Clients to initialize:", clients
31
+ puts "Host:", @host
32
+ end
33
+
34
+ if clients.kind_of?(Array) and @host
35
+ clients.each do |client|
36
+ send("set_cxf_#{client}_client")
37
+ end
38
+ end
39
+ end
40
+
41
+ ##
42
+ # === Set cxf pub.
43
+ # Initialize the public client and set the contact token
44
+ def set_cxf_pub_client
45
+ # Initialize cxf pub client, credentials taken from cxf_config.yml.erb file
46
+ visit_id = cookies[:cxf_visit_id]
47
+ contact_token_id = cookies[:cxf_contact_id]
48
+
49
+ @cxf_pub = Cxf::Pub.new(
50
+ @host,
51
+ @api_key,
52
+ contact_token_id,
53
+ visit_id,
54
+ @debug
55
+ )
56
+ end
57
+
58
+ ##
59
+ # === Set cxf contact client.
60
+ # Initialize the contact client and set the contact token
61
+ def set_cxf_contact_client
62
+ # Initialize cxf contact client
63
+ contact_session_token = cookies[:cxf_contact_session_token]
64
+ contact_refresh_token = cookies[:cxf_contact_refresh_token]
65
+ contact_token_id = cookies[:cxf_contact_id]
66
+ @cxf_contact = Cxf::Contact.new(
67
+ @host,
68
+ @api_key,
69
+ contact_session_token,
70
+ contact_refresh_token,
71
+ contact_token_id,
72
+ @debug
73
+ )
74
+ end
75
+
76
+ ##
77
+ # === Set Cxf user client.
78
+ # Initialize the user client
79
+ def set_cxf_user_client
80
+ # Initialize cxf user client
81
+ user_session_token = cookies[:cxf_user_session_token]
82
+ user_refresh_token = cookies[:cxf_user_refresh_token]
83
+ @cxf_user = Cxf::User.new(
84
+ @host,
85
+ @api_key,
86
+ user_session_token,
87
+ user_refresh_token,
88
+ @debug
89
+ )
90
+ end
91
+
92
+ ##
93
+ # === Set Cxf service account
94
+ # Initialize the service account client
95
+ def set_cxf_service_account_client
96
+ # Initialize service account client
97
+ @cxf_service_account = Cxf::User.new(
98
+ @host,
99
+ @api_key,
100
+ @api_key,
101
+ @debug
102
+ )
103
+ end
104
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ReadConfigFile
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ before_action :set_config_variables
8
+ end
9
+
10
+ def set_config_variables
11
+ if File.exists?("#{Rails.root}/cxf_config.yml.erb")
12
+ template = ERB.new File.new("#{Rails.root}/cxf_config.yml.erb").read
13
+ config = YAML.safe_load template.result(binding)
14
+
15
+ @host = config.dig('cxf', 'host')
16
+ @api_key = config.dig('cxf', 'api_key')
17
+ @debug = !!config.dig('cxf', 'debug')
18
+ @redis_config = config.dig('cxf', 'redis_cache')
19
+ @use_cache = config.dig('cxf', 'redis_cache', 'use_cache')
20
+
21
+ if @use_cache
22
+ @redis_server = Redis.new(
23
+ host: config.dig('cxf', 'redis_cache', 'redis_host'),
24
+ port: config.dig('cxf', 'redis_cache', 'redis_port') || 6379,
25
+ db: config.dig('cxf', 'redis_cache', 'redis_db') || 1
26
+ )
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './concerns/read_config_file'
4
+ require_relative '../helpers/proxy_controllers_methods'
5
+
6
+ module Cxf
7
+ class ContactAPIController < ActionController::API
8
+ include AbstractController::Helpers
9
+ include ReverseProxy::Controller
10
+ include ReadConfigFile
11
+ include ProxyControllersMethods
12
+
13
+ def index
14
+ super('contact')
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './concerns/read_config_file'
4
+ require_relative '../helpers/proxy_controllers_methods'
5
+
6
+ module Cxf
7
+ class PublicAPIController < ActionController::API
8
+ include ReverseProxy::Controller
9
+ include AbstractController::Helpers
10
+ include ReadConfigFile
11
+ include ProxyControllersMethods
12
+
13
+ end
14
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './concerns/read_config_file'
4
+ require_relative '../helpers/proxy_controllers_methods'
5
+
6
+ module Cxf
7
+ class UserAPIController < ActionController::API
8
+ include AbstractController::Helpers
9
+ include ReadConfigFile
10
+ include ProxyControllersMethods
11
+
12
+ def index
13
+ super('user')
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ContactAuthHelper
4
+ ##
5
+ # === Cxf Contact Login.
6
+ # Starts a contact session in cxf.cloud and set a session cookie
7
+ def cxf_contact_login(email, password)
8
+ # Login in cxf
9
+ response = @cxf_contact.login(email, password)
10
+ # Get session token from response
11
+ if response.key? 'data'
12
+ session_token = response['data']['session_token']
13
+ refresh_token = response['data']['refresh_token']
14
+ end
15
+ # id_token = response['contact']['contact_token'] ? response['contact']['contact_token'] : response['contact']['id_token']
16
+ # Set a permanent cookie with the session token
17
+ cookies.permanent[:cxf_contact_session_token] = { value: session_token, secure: true, httponly: true }
18
+ cookies.permanent[:cxf_contact_refresh_token] = { value: refresh_token, secure: true, httponly: true }
19
+ # cookies.permanent[:cxf_contact_id] = { value: id_token, secure: true, httponly: true }
20
+ @contact_token = id_token
21
+ end
22
+
23
+ ##
24
+ # === Cxf contact Login.
25
+ # Starts a contact session in cxf.cloud and set a session cookie
26
+ def cxf_contact_magic_link_login(hash, redirect_in_error = false)
27
+ # Login in cxf
28
+ response = @cxf_contact.magic_link_login(hash)
29
+
30
+ if response['data']
31
+ # Get session token from response
32
+ session_token = response['data']['session_token']
33
+ refresh_token = response['data']['refresh_token']
34
+ # id_token = response['data']['contact']['contact_token'] ? response['data']['contact']['contact_token'] : response['data']['contact']['id_token']
35
+ # Set a permanent cookie with the session token
36
+ cookies.permanent[:cxf_contact_session_token] = { value: session_token, secure: true, httponly: true }
37
+ cookies.permanent[:cxf_contact_refresh_token] = { value: refresh_token, secure: true, httponly: true }
38
+ # cookies.permanent[:cxf_contact_id] = { value: id_token, secure: true, httponly: true }
39
+ # @contact_token = id_token
40
+ redirect_to response['data']['redirect_url'] || '/' if redirect_in_error
41
+ else
42
+ redirect_to '/' if redirect_in_error
43
+ end
44
+ end
45
+
46
+ ##
47
+ # === Cxf Contact Logout.
48
+ # Destroy session from cxf.cloud and delete local session cookie
49
+ def cxf_contact_logout
50
+ # Logout from cxf
51
+ @cxf_contact.logout
52
+ # Delete session token and keep the contact token id
53
+ # Never delete the cxf_contact_id cookie to avoid the creation of ghosts
54
+ cookies.delete(:cxf_contact_session_token)
55
+ cookies.delete(:cxf_contact_refresh_token)
56
+ @contact_token = nil
57
+ end
58
+
59
+ def cxf_contact_signed_in?
60
+
61
+ begin
62
+ # Check status in cxf
63
+ # Check status in cxf
64
+ response = @cxf_contact.status
65
+ status = response['success'] || false
66
+ rescue => e
67
+ # Handle the client Unauthorized error
68
+ # if cxf response is negative delete the session cookie
69
+ cookies.delete(:cxf_contact_session_token)
70
+ cookies.delete(:cxf_contact_refresh_token)
71
+ status = false
72
+ end
73
+
74
+ status
75
+ end
76
+
77
+ def update_contact_tokens
78
+ access_token = @cxf_user.get_client.session_token
79
+ refresh_token = @cxf_user.get_client.refresh_token
80
+ access_token_expires_at = @cxf_user.get_client.session_token_expires_at
81
+ refresh_token_expires_at = @cxf_user.get_client.refresh_token_expires_at
82
+
83
+ cookies[:cxf_contact_session_token] = { value: access_token, secure: true, httponly: true, expires: Time.at(access_token_expires_at) } if access_token && access_token_expires_at
84
+ cookies[:cxf_contact_refresh_token] = { value: refresh_token, secure: true, httponly: true, expires: Time.at(refresh_token_expires_at) } if refresh_token && refresh_token_expires_at
85
+ end
86
+ end