mints 0.0.14 → 0.0.18

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3deabf302a32fac1452e45f7ab9753ec0a1ee7973535b0371b9c67a25cb60cc7
4
- data.tar.gz: 9589dacb58814ad17c5a2826f866b929f862d7ce7ce3fc745dcd3e191c2f19c9
3
+ metadata.gz: 0cff6a549992ee75536a6696575ef58435328068adcbdd3658fc6633ec0e4fd5
4
+ data.tar.gz: c2fc848075d8156ba44aadc01c45b0d01c6eeaf5a7a21201d38b51e99ad86d54
5
5
  SHA512:
6
- metadata.gz: d1bc7c183bc20b9dbe25f4f5d7770ee0492d0f2e773cdfb6934af7afe0056bf8f38f7277440a821e71f22a4abda51c11f7b8c2dd5f6279e22b9c68c7fe703d82
7
- data.tar.gz: 2f7ebc2b2f8977f5009bafc4e42348cc4a7494729fb1aa15ccf69a8e7937c9872120a2bc81711fcd22153fa5cb05e71c462dc0c67645f350c5e5021f1e7a71f9
6
+ metadata.gz: e865b46eb2536dd6f14ad39d0ebe691ec2db0cf98bd3c1861223dd3e9462deb555eee92d3ece5da498cbd4655dc6443774e6ca1ea94c0225f5a8db1977a7b237
7
+ data.tar.gz: f5203d5ba1b426812abd7f281146c84b58c2761e1be388b0ed004bd74ccd98000964615787d127a275848978fc029f444db2f8647f6a924df918814cf3f3c856
data/lib/client.rb CHANGED
@@ -24,6 +24,9 @@ module Mints
24
24
  base_url = @base_url if !base_url
25
25
  uri = ""
26
26
  if (options && options.class == Hash)
27
+ if (options[:jfilters] && options[:jfilters].class == Hash)
28
+ options[:jfilters] = Base64.encode64(JSON.generate(options[:jfilters]))
29
+ end
27
30
  uri = Addressable::URI.new
28
31
  uri.query_values = options
29
32
  end
@@ -69,6 +72,9 @@ module Mints
69
72
  elsif action === 'put' or action === 'patch' or action ==='update'
70
73
  action = 'put'
71
74
  response = self.send("#{@scope}_#{action}", "#{full_url}", data)
75
+ elsif action === 'delete' or action === 'destroy'
76
+ action = 'delete'
77
+ response = self.send("#{@scope}_#{action}", "#{full_url}", data)
72
78
  end
73
79
  if result_from_cache
74
80
  return parsed_response = JSON.parse(response)
@@ -90,7 +96,7 @@ module Mints
90
96
  name_len = name_spplited.size
91
97
  # the action always be the first element
92
98
  action = name_spplited.first
93
- raise 'NoActionError' unless ['get', 'create', 'post', 'update', 'put'].include?(action)
99
+ raise 'NoActionError' unless ['get', 'create', 'post', 'update', 'put', 'delete', 'destroy'].include?(action)
94
100
  # the object always be the last element
95
101
  object = separator == "__" ? name_spplited.last.gsub("_","-") : name_spplited.last
96
102
  # get intermediate url elements
@@ -185,6 +191,7 @@ module Mints
185
191
  puts url
186
192
  puts "Headers:"
187
193
  puts headers
194
+ puts "Method: get"
188
195
  end
189
196
  return headers ? HTTParty.get(url, :headers => headers) : HTTParty.get(url)
190
197
  end
@@ -198,6 +205,7 @@ module Mints
198
205
  puts headers
199
206
  puts "Data:"
200
207
  puts data
208
+ puts "Method: post"
201
209
  end
202
210
  return headers ? HTTParty.post(url, :headers=> headers, :body => data) : HTTParty.post(url, :body => data)
203
211
  end
@@ -211,10 +219,25 @@ module Mints
211
219
  puts headers
212
220
  puts "Data:"
213
221
  puts data
222
+ puts "Method: put"
214
223
  end
215
224
  return headers ? HTTParty.put(url, :headers=> headers, :body => data) : HTTParty.put(url, :body => data)
216
225
  end
217
226
 
227
+ # Simple HTTP DELETE
228
+ def http_delete(url, headers = nil, data = nil)
229
+ if @debug
230
+ puts "Url:"
231
+ puts url
232
+ puts "Headers:"
233
+ puts headers
234
+ puts "Data:"
235
+ puts data
236
+ puts "Method: delete"
237
+ end
238
+ return headers ? HTTParty.delete(url, :headers=> headers, :body => data) : HTTParty.delete(url, :body => data)
239
+ end
240
+
218
241
  # Start contact context
219
242
  def contact_get(url)
220
243
  headers = {
@@ -243,7 +266,7 @@ module Mints
243
266
  "ContactToken" => @contact_token_id
244
267
  }
245
268
  headers["Authorization"] = "Bearer #{@session_token}" if @session_token
246
- return self.http_post(url, headers, data)
269
+ return self.http_put(url, headers, data)
247
270
  end
248
271
 
249
272
  # Start User context
@@ -274,6 +297,15 @@ module Mints
274
297
  headers["Authorization"] = "Bearer #{@session_token}" if @session_token
275
298
  return self.http_put(url, headers, data)
276
299
  end
300
+
301
+ def user_delete(url, data)
302
+ headers = {
303
+ "ApiKey" => @api_key,
304
+ "Accept" => "application/json"
305
+ }
306
+ headers["Authorization"] = "Bearer #{@session_token}" if @session_token
307
+ return self.http_delete(url, headers, data)
308
+ end
277
309
  # End User Context
278
310
 
279
311
  def public_get(url, headers = nil)
data/lib/contact.rb CHANGED
@@ -15,6 +15,13 @@ module Mints
15
15
  # === Login.
16
16
  # Starts a contact session
17
17
  #
18
+ # ==== Parameters:
19
+ # * +email+ - [_String_] The email that will be logged.
20
+ # * +password+ - [_String_] The password of the email.
21
+ #
22
+ # ==== Example
23
+ # @mints_contact.login("brown.abigail@dubuque.com", "helloword")
24
+ #
18
25
  def login(email, password)
19
26
  data = {
20
27
  email: email,
@@ -27,9 +34,62 @@ module Mints
27
34
  return response
28
35
  end
29
36
 
37
+ ##
38
+ # === Magic Link Login.
39
+ # Starts a contact session with a token received in the contact email. The token will be received by send_magic_link method.
40
+ #
41
+ # ==== Parameters:
42
+ # * +token+ - [_String_] The email that will be logged.
43
+ #
44
+ # ==== Example
45
+ # @mints_contact.magic_link_login(
46
+ # "d8618c6d-a165-41cb-b3ec-d053cbf30059:zm54HtRdfHED8dpILZpjyqjPIceiaXNLfOklqM92fveBS0nDtyPYBlI4CPlPe3zq"
47
+ # )
48
+ #
49
+ def magic_link_login(token)
50
+ response = @client.raw("get", "/contacts/magic-link-login/#{token}", nil, '/api/v1')
51
+ if response.key? "session_token"
52
+ @client.session_token = response["session_token"]
53
+ end
54
+ return response
55
+ end
56
+
57
+ ##
58
+ # === Send Magic Link
59
+ # Send magic link to contact by email. That magic link will be used in magic_link_login method.
60
+ #
61
+ # ==== Parameters:
62
+ # * +email+ - [_String_] Contact's email.
63
+ # * +template_slug+ - [_String_] Email template's slug to be used in the email.
64
+ # * +redirectUrl+ - [_String_] Url to be redirected in the implemented page.
65
+ # * +lifeTime+ - [_Integer_] Maximum time of use in minutes.
66
+ # * +maxVisits+ - [_Integer_] The maximum number of uses of a token.
67
+ #
68
+ # ==== First Example
69
+ # @mints_contact.send_magic_link("brown.abigail@dubuque.com", "")
70
+ #
71
+ # ==== Second Example
72
+ # @mints_contact.send_magic_link("brown.abigail@dubuque.com", "", "", 1440, 3)
73
+ #
74
+ def send_magic_link(email, template_slug, redirectUrl = '', lifeTime = 1440, maxVisits = nil)
75
+ data = {
76
+ email: email,
77
+ lifeTime: lifeTime,
78
+ maxVisits: maxVisits,
79
+ redirectUrl: redirectUrl,
80
+ templateId: template_slug
81
+ }
82
+ response = @client.raw("post", "/contacts/magic-link", nil, { data: data }, '/api/v1')
83
+ return response
84
+ end
85
+
30
86
  ##
31
87
  # === Logout.
32
- # Ends a contact session
88
+ # Ends a contact session previously logged.
89
+ #
90
+ # ==== Example
91
+ # @mints_contact.login('brown.abigail@dubuque.com', 'helloword')
92
+ # @mints_contact.logout
33
93
  #
34
94
  def logout
35
95
  response = @client.raw("post", "/contacts/logout") if session_token?
@@ -41,7 +101,15 @@ module Mints
41
101
 
42
102
  ##
43
103
  # === Change Password.
44
- # Change password
104
+ # Change password without email. To change the password a contact must be logged.
105
+ #
106
+ # ==== Parameters:
107
+ # * +data+ - [] A new password allocated in a data key.
108
+ #
109
+ # ==== Example
110
+ # @mints_contact.login('brown.abigail@dubuque.com', 'helloword')
111
+ # data = { "data": { "password": "123456" } }
112
+ # @mints_contact.change_password(data)
45
113
  #
46
114
  def change_password(data)
47
115
  return @client.raw("post", "/contacts/change-password", nil, data)
@@ -49,7 +117,14 @@ module Mints
49
117
 
50
118
  ##
51
119
  # === Recover Password.
52
- # Recover password
120
+ # Send a email that contains a token to a contact. That token will be used in reset_password to establish a new password.
121
+ #
122
+ # ==== Parameters:
123
+ # * +data+ - [] It's a data key where will be hosted the destination email.
124
+ #
125
+ # ==== Example
126
+ # data = { "data": { "email": "brown.abigail@dubuque.com" } }
127
+ # @mints_contact.recover_password(data)
53
128
  #
54
129
  def recover_password(data)
55
130
  return @client.raw("post", "/contacts/recover-password", nil, data)
@@ -57,7 +132,19 @@ module Mints
57
132
 
58
133
  ##
59
134
  # === Reset Password.
60
- # Reset password
135
+ # Reset password using a token. The token is obtained by recover_password method.
136
+ #
137
+ # ==== Parameters:
138
+ # * +data+ - [] It's a set of data which contains all the information to reset a contact password.
139
+ #
140
+ # ==== Example
141
+ # data = { "data": {
142
+ # "email": "brown.abigail@dubuque.com",
143
+ # "password": "helloword",
144
+ # "password_confirmation": "helloword",
145
+ # "token": "644aa3aa0831d782cc42e42b11aedea9a2234389af4f429a8d96651295ecfa09"
146
+ # } }
147
+ # @mints_contact.reset_password(data)
61
148
  #
62
149
  def reset_password(data)
63
150
  return @client.raw("post", "/contacts/reset-password", nil, data)
@@ -73,7 +160,10 @@ module Mints
73
160
 
74
161
  ##
75
162
  # === Me.
76
- # Get contact logged info
163
+ # Get contact logged info.
164
+ #
165
+ # ==== Example
166
+ # @mints_contact.me
77
167
  #
78
168
  def me
79
169
  return @client.raw("get", "/contacts/me")
@@ -81,7 +171,10 @@ module Mints
81
171
 
82
172
  ##
83
173
  # === Status.
84
- # Get contact logged status
174
+ # Get contact logged status.
175
+ #
176
+ # ==== Example
177
+ # @mints_contact.status
85
178
  #
86
179
  def status
87
180
  return @client.raw("get", "/contacts/status")
@@ -89,12 +182,43 @@ module Mints
89
182
 
90
183
  ##
91
184
  # === Update.
92
- # Update logged contact attributes
185
+ # Update logged contact attributes.
186
+ #
187
+ # ==== Parameters:
188
+ # * +data+ - [] It's the data to update with a session active.
189
+ #
190
+ # ==== Example
191
+ # @mints_contact.login("brown.abigail@dubuque.com", "helloword")
192
+ # data = { "data": {
193
+ # "given_name": "Alonso",
194
+ # "last_name": "Garcia"
195
+ # } }
196
+ # @mints_contact.update(data)
93
197
  #
94
198
  def update(data)
95
199
  return @client.raw("put", "/contacts/update", nil, data)
96
200
  end
97
201
 
202
+ ##
203
+ # === Register.
204
+ # Register a contact.
205
+ #
206
+ # ==== Parameters:
207
+ # * +data+ - [] It's the register data.
208
+ #
209
+ # ==== Example
210
+ # data = { "data": {
211
+ # "email": "carlos@mints.cloud",
212
+ # "given_name": "Carlos",
213
+ # "last_name": "Fernandez",
214
+ # "password": "123456"
215
+ # } }
216
+ # @mints_contact.register(data);
217
+ #
218
+ def register(data)
219
+ return @client.raw("post", "/contacts/register", nil, data)
220
+ end
221
+
98
222
  private
99
223
 
100
224
  def session_token?
@@ -0,0 +1,3 @@
1
+ require 'reverse_proxy/client'
2
+ class MintsAssetsController < Mints::PublicAPIController
3
+ end
@@ -6,8 +6,10 @@ class MintsFilesGenerator < Rails::Generators::Base
6
6
  copy_file 'mints_user_controller.rb', './app/controllers/api/mints_user_controller.rb'
7
7
  copy_file 'mints_contact_controller.rb', './app/controllers/api/v1/mints_contact_controller.rb'
8
8
  copy_file 'mints_public_controller.rb', './app/controllers/api/v1/mints_public_controller.rb'
9
+ copy_file 'mints_assets_controller.rb', './app/controllers/mints_assets_controller.rb'
9
10
  route <<-eos
10
11
  # Mints auto-generated routes (proxy to send request to mints.cloud)
12
+ match '/public-assets/*path' => 'mints_assets#index', via: [:get, :post, :put, :patch, :delete]
11
13
  namespace :api, defaults: { format: :json } do
12
14
  match '/user/v1/*path' => 'mints_user#index', via: [:get, :post, :put, :patch, :delete]
13
15
  match '/contact/v1/*path' => 'mints_contact#index', via: [:get, :post, :put, :patch, :delete]
@@ -2,16 +2,16 @@ module Mints
2
2
  class AdminBaseController < ActionController::Base
3
3
  before_action :set_mints_user_client
4
4
 
5
- # def mints_user_signed_in?
6
- # # Check status in mints
7
- # response = @mints_user.status
8
- # status = response['success'] ? response['success'] : false
9
- # unless status
10
- # # if mints response is negative delete the session cookie
11
- # cookies.delete(:mints_user_session_token)
12
- # end
13
- # return status
14
- # end
5
+ def mints_user_signed_in?
6
+ # Check status in mints
7
+ response = @mints_user.me
8
+ status = response['data'] ? true : false
9
+ unless status
10
+ # if mints response is negative delete the session cookie
11
+ #cookies.delete(:mints_user_session_token)
12
+ end
13
+ return status
14
+ end
15
15
 
16
16
  ##
17
17
  # === Mints user Login.
@@ -22,7 +22,22 @@ module Mints
22
22
  # Get session token from response
23
23
  session_token = response['api_token']
24
24
  # Set a permanent cookie with the session token
25
- cookies.permanent[:mints_user_session_token] = session_token
25
+ cookies[:mints_user_session_token] = { value: session_token, secure: true, httponly: true, expires: 1.day }
26
+ end
27
+
28
+ ##
29
+ # === Mints user Login.
30
+ # Starts a user session in mints.cloud and set a session cookie
31
+ def mints_user_magic_link_login(hash)
32
+ # Login in mints
33
+ response = @mints_user.magic_link_login(hash)
34
+ if response['data'] && response['data']['redirect_url']
35
+ # Set a cookie with the session token
36
+ cookies[:mints_user_session_token] = { value: response['data']['api_token'], expires: 1.day, secure: true, httponly: true }
37
+ redirect_to response['data']['redirect_url']
38
+ else
39
+ redirect_to '/'
40
+ end
26
41
  end
27
42
 
28
43
  ##
@@ -51,7 +66,8 @@ module Mints
51
66
  raise 'MintsBadCredentialsError'
52
67
  end
53
68
  # Initialize mints user client
54
- @mints_user = Mints::User.new(@host, @api_key, nil, @debug)
69
+ session_token = cookies[:mints_user_session_token] ? cookies[:mints_user_session_token] : nil
70
+ @mints_user = Mints::User.new(@host, @api_key, session_token, @debug)
55
71
  end
56
72
  end
57
73
  end
@@ -1,6 +1,6 @@
1
1
  module Mints
2
2
  class BaseApiController < ActionController::Base
3
- before_action :set_mints_pub_client
3
+ before_action :set_mints_clients
4
4
 
5
5
  ##
6
6
  # === Mints Contact Login.
@@ -10,11 +10,31 @@ module Mints
10
10
  response = @mints_contact.login(email, password)
11
11
  # Get session token from response
12
12
  session_token = response['session_token']
13
- id_token = response['contact']['id_token']
13
+ id_token = response['contact']['contact_token']
14
14
  # Set a permanent cookie with the session token
15
- cookies.permanent[:mints_contact_session_token] = session_token
16
- cookies.permanent[:mints_contact_id] = id_token
15
+ cookies.permanent[:mints_contact_session_token] = { value: session_token, secure: true, httponly: true }
16
+ cookies.permanent[:mints_contact_id] = { value: id_token, secure: true, httponly: true }
17
17
  @contact_token = id_token
18
+ end
19
+
20
+ ##
21
+ # === Mints cotnact Login.
22
+ # Starts a cotnact session in mints.cloud and set a session cookie
23
+ def mints_contact_magic_link_login(hash)
24
+ # Login in mints
25
+ response = @mints_contact.magic_link_login(hash)
26
+ if response['data']
27
+ # Get session token from response
28
+ session_token = response['data']['session_token']
29
+ id_token = response['data']['contact']['contact_token']
30
+ # Set a permanent cookie with the session token
31
+ cookies.permanent[:mints_contact_session_token] = { value: session_token, secure: true, httponly: true }
32
+ cookies.permanent[:mints_contact_id] = { value: id_token, secure: true, httponly: true }
33
+ @contact_token = id_token
34
+ redirect_to response['data']['redirect_url'] ? response['data']['redirect_url'] : '/'
35
+ else
36
+ redirect_to '/'
37
+ end
18
38
  end
19
39
 
20
40
  ##
@@ -28,26 +48,96 @@ module Mints
28
48
  cookies.delete(:mints_contact_id)
29
49
  @contact_token = nil
30
50
  end
51
+
52
+ ##
53
+ # === Mints user Login.
54
+ # Starts a user session in mints.cloud and set a session cookie
55
+ def mints_user_login(email, password)
56
+ # Login in mints
57
+ response = @mints_user.login(email, password)
58
+ # Get session token from response
59
+ session_token = response['api_token']
60
+ # Set a permanent cookie with the session token
61
+ cookies[:mints_user_session_token] = { value: session_token, secure: true, httponly: true, expires: 1.day }
62
+ end
63
+
64
+ ##
65
+ # === Mints user Login.
66
+ # Starts a user session in mints.cloud and set a session cookie
67
+ def mints_user_magic_link_login(hash)
68
+ # Login in mints
69
+ response = @mints_user.magic_link_login(hash)
70
+ if response['data']
71
+ # Set a cookie with the session token
72
+ cookies[:mints_user_session_token] = { value: response['data']['api_token'], secure: true, httponly: true, expires: 1.day }
73
+ redirect_to response['data']['redirect_url'] ? response['data']['redirect_url'] : '/'
74
+ else
75
+ redirect_to '/'
76
+ end
77
+ end
78
+
79
+ ##
80
+ # === Mints user Logout.
81
+ # Destroy session from mints.cloud and delete local session cookie
82
+ def mints_user_logout
83
+ # Logout from mints
84
+ # @mints_user.logout
85
+ # Delete local cookie
86
+ cookies.delete(:mints_user_session_token)
87
+ end
31
88
 
32
89
  private
33
90
 
34
91
  ##
35
- # === Set mints pub.
36
- # Initialize the public client and set the contact token
37
- def set_mints_pub_client
92
+ # === Set mints clients (pub, user and contact)
93
+ # Initialize all clients from mitns
94
+ def set_mints_clients
38
95
  if File.exists?("#{Rails.root}/mints_config.yml.erb")
39
96
  template = ERB.new File.new("#{Rails.root}/mints_config.yml.erb").read
40
97
  config = YAML.load template.result(binding)
41
98
  @host = config["mints"]["host"]
42
99
  @api_key = config["mints"]["api_key"]
43
100
  @debug = config["sdk"]["debug"] ? config["sdk"]["debug"] : false
101
+
102
+ #public client
103
+ set_mints_pub_client
104
+ #contact client
105
+ set_mints_contact_client
106
+ #user client
107
+ set_mints_user_client
44
108
  else
45
109
  raise 'MintsBadCredentialsError'
46
110
  end
111
+ end
112
+
113
+ ##
114
+ # === Set mints pub.
115
+ # Initialize the public client and set the contact token
116
+ def set_mints_pub_client
117
+
47
118
  # Initialize mints pub client, credentials taken from mints_config.yml.erb file
48
119
  @mints_pub = Mints::Pub.new(@host, @api_key, nil, @debug)
49
120
  # Set contact token from cookie
50
121
  @mints_pub.client.session_token = @contact_token
51
122
  end
123
+
124
+ ##
125
+ # === Set mints contact client.
126
+ # Initialize the public client and set the contact token
127
+ def set_mints_contact_client
128
+ # Initialize mints clontact client
129
+ session_token = cookies[:mints_contact_session_token] ? cookies[:mints_contact_session_token] : nil
130
+ contact_token_id = cookies[:mints_contact_id] ? cookies[:mints_contact_id] : nil
131
+ @mints_contact = Mints::Contact.new(@host, @api_key, session_token, contact_token_id, @debug)
132
+ end
133
+
134
+ ##
135
+ # === Set Mints user client.
136
+ # Initialize the public client and set the user token
137
+ def set_mints_user_client
138
+ # Initialize mints user client
139
+ session_token = cookies[:mints_user_session_token] ? cookies[:mints_user_session_token] : nil
140
+ @mints_user = Mints::User.new(@host, @api_key, session_token, @debug)
141
+ end
52
142
  end
53
143
  end