cxf 0.0.5 → 0.0.6
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.
- checksums.yaml +4 -4
- data/lib/client.rb +20 -9
- data/lib/contact.rb +2 -0
- data/lib/cxf/controllers/concerns/cxf_clients.rb +4 -4
- data/lib/cxf/helpers/contact_auth_helper.rb +16 -15
- data/lib/cxf/helpers/proxy_controllers_methods.rb +20 -8
- data/lib/cxf/helpers/user_auth_helper.rb +13 -14
- data/lib/user.rb +2 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6f4d4438ad88e14b53649c99f16ade67766fe99131d5be687e0c45da71d7ffbc
|
4
|
+
data.tar.gz: b4c881f37d39e448ee993c87e26f1b3722a344d6a5f06f455b733867d24c91f7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b806f588681360912cbdf98932d6f061e64407158a898c56de54a9262461fe7d519688784b0af1b240745f7fa45b493878f04c6cb09400be79ca43f1d6bebd0
|
7
|
+
data.tar.gz: b09c53002f03c0ad50b8a93610b71c1ee8ba3fa474acb155c59f7175207f6cae7d1f85fc6c6734e23acc669af1e37312192eebeaaee27f1fe4bde48d942bcb88
|
data/lib/client.rb
CHANGED
@@ -13,7 +13,7 @@ module Cxf
|
|
13
13
|
include CxfHelper
|
14
14
|
|
15
15
|
attr_reader :host, :mode, :api_key, :scope, :base_url
|
16
|
-
attr_accessor :session_token, :refresh_token, :contact_token_id, :
|
16
|
+
attr_accessor :session_token, :refresh_token, :contact_token_id, :user_agent
|
17
17
|
|
18
18
|
def initialize(
|
19
19
|
host,
|
@@ -30,9 +30,7 @@ module Cxf
|
|
30
30
|
@host = host
|
31
31
|
@api_key = api_key
|
32
32
|
@session_token = session_token
|
33
|
-
@session_token_expires_at = nil
|
34
33
|
@refresh_token = refresh_token
|
35
|
-
@refresh_token_expires_at = nil
|
36
34
|
@contact_token_id = contact_token_id
|
37
35
|
@visit_id = visit_id
|
38
36
|
@debug = debug
|
@@ -380,7 +378,7 @@ module Cxf
|
|
380
378
|
|
381
379
|
if !is_success and !ignore_http_errors
|
382
380
|
title = "Request failed with status #{http_status}"
|
383
|
-
detail = response&.parsed_response["message"] ||response&.response&.message || 'Unknown error'
|
381
|
+
detail = response&.parsed_response["message"] || response&.response&.message || 'Unknown error'
|
384
382
|
|
385
383
|
if @debug
|
386
384
|
puts "Error detected: #{http_status}"
|
@@ -447,12 +445,25 @@ module Cxf
|
|
447
445
|
return unless response&.headers
|
448
446
|
|
449
447
|
# Return if the response does not have headers Access-Token and Refresh-Token
|
450
|
-
return unless response.headers.key?('
|
448
|
+
return unless response.headers.key?('Set-Cookie')
|
449
|
+
parsed_cookie = parse_set_cookie(response.headers['Set-Cookie'])
|
450
|
+
return unless parsed_cookie['Access-Token'] || parsed_cookie['Refresh-Token']
|
451
451
|
|
452
|
-
@session_token =
|
453
|
-
@refresh_token =
|
454
|
-
|
455
|
-
|
452
|
+
@session_token = parsed_cookie['Access-Token'] if parsed_cookie['Access-Token']
|
453
|
+
@refresh_token = parsed_cookie['Refresh-Token'] if parsed_cookie['Refresh-Token']
|
454
|
+
end
|
455
|
+
|
456
|
+
def parse_set_cookie(set_cookie)
|
457
|
+
set_cookie = set_cookie.split(', ')
|
458
|
+
cookies_hash = {}
|
459
|
+
|
460
|
+
set_cookie.each do |cookie|
|
461
|
+
key, value = cookie.split('=')
|
462
|
+
value = value.split(';')[0]
|
463
|
+
cookies_hash[key] = value
|
464
|
+
end
|
465
|
+
|
466
|
+
cookies_hash
|
456
467
|
end
|
457
468
|
end
|
458
469
|
end
|
data/lib/contact.rb
CHANGED
@@ -93,6 +93,8 @@ module Cxf
|
|
93
93
|
password: password
|
94
94
|
}
|
95
95
|
response = @client.raw('post', '/contacts/login', nil, data_transform(data))
|
96
|
+
|
97
|
+
return response unless response.is_a? Hash
|
96
98
|
if response.key? 'data' and response['data'].key? 'access_token'
|
97
99
|
@client.session_token = response['data']['access_token']
|
98
100
|
@client.refresh_token = response['data']['refresh_token']
|
@@ -62,8 +62,8 @@ module CxfClients
|
|
62
62
|
# Initialize the contact client and set the contact token
|
63
63
|
def set_cxf_contact_client
|
64
64
|
# Initialize cxf contact client
|
65
|
-
contact_session_token = cookies[
|
66
|
-
contact_refresh_token = cookies[
|
65
|
+
contact_session_token = cookies["cxf_contact_session_token"]
|
66
|
+
contact_refresh_token = cookies["cxf_contact_refresh_token"]
|
67
67
|
contact_token_id = cookies[:cxf_contact_id] || nil
|
68
68
|
user_agent = request.user_agent
|
69
69
|
@cxf_contact = Cxf::Contact.new(
|
@@ -82,8 +82,8 @@ module CxfClients
|
|
82
82
|
# Initialize the user client
|
83
83
|
def set_cxf_user_client
|
84
84
|
# Initialize cxf user client
|
85
|
-
user_session_token = cookies[
|
86
|
-
user_refresh_token = cookies[
|
85
|
+
user_session_token = cookies["cxf_user_session_token"]
|
86
|
+
user_refresh_token = cookies["cxf_user_refresh_token"]
|
87
87
|
user_agent = request.user_agent
|
88
88
|
@cxf_user = Cxf::User.new(
|
89
89
|
@host,
|
@@ -7,15 +7,18 @@ module ContactAuthHelper
|
|
7
7
|
def cxf_contact_login(email, password)
|
8
8
|
# Login in cxf
|
9
9
|
response = @cxf_contact.login(email, password)
|
10
|
+
|
10
11
|
# Get session token from response
|
12
|
+
return response unless response.is_a? Hash
|
11
13
|
if response.key? 'data'
|
12
|
-
session_token = response['data']['
|
14
|
+
session_token = response['data']['access_token']
|
13
15
|
refresh_token = response['data']['refresh_token']
|
14
16
|
id_token = response['data']['contact_token'] || response['data']['id_token'] || nil
|
15
17
|
end
|
18
|
+
|
16
19
|
# Set a permanent cookie with the session token
|
17
|
-
cookies.permanent[
|
18
|
-
cookies.permanent[
|
20
|
+
cookies.permanent["cxf_contact_session_token"] = { value: session_token, secure: true, httponly: true }
|
21
|
+
cookies.permanent["cxf_contact_refresh_token"] = { value: refresh_token, secure: true, httponly: true }
|
19
22
|
# cookies.permanent[:cxf_contact_id] = { value: id_token, secure: true, httponly: true }
|
20
23
|
@contact_token = id_token
|
21
24
|
end
|
@@ -33,8 +36,8 @@ module ContactAuthHelper
|
|
33
36
|
refresh_token = response['data']['refresh_token']
|
34
37
|
# id_token = response['data']['contact']['contact_token'] ? response['data']['contact']['contact_token'] : response['data']['contact']['id_token']
|
35
38
|
# Set a permanent cookie with the session token
|
36
|
-
cookies.permanent[
|
37
|
-
cookies.permanent[
|
39
|
+
cookies.permanent["cxf_contact_session_token"] = { value: session_token, secure: true, httponly: true }
|
40
|
+
cookies.permanent["cxf_contact_refresh_token"] = { value: refresh_token, secure: true, httponly: true }
|
38
41
|
# cookies.permanent[:cxf_contact_id] = { value: id_token, secure: true, httponly: true }
|
39
42
|
# @contact_token = id_token
|
40
43
|
redirect_to response['data']['redirect_url'] || '/' if redirect_in_error
|
@@ -51,8 +54,8 @@ module ContactAuthHelper
|
|
51
54
|
@cxf_contact.logout
|
52
55
|
# Delete session token and keep the contact token id
|
53
56
|
# Never delete the cxf_contact_id cookie to avoid the creation of ghosts
|
54
|
-
cookies.delete(
|
55
|
-
cookies.delete(
|
57
|
+
cookies.delete("cxf_contact_session_token")
|
58
|
+
cookies.delete("cxf_contact_refresh_token")
|
56
59
|
@contact_token = nil
|
57
60
|
end
|
58
61
|
|
@@ -66,8 +69,8 @@ module ContactAuthHelper
|
|
66
69
|
rescue => e
|
67
70
|
# Handle the client Unauthorized error
|
68
71
|
# if cxf response is negative delete the session cookie
|
69
|
-
cookies.delete(
|
70
|
-
cookies.delete(
|
72
|
+
cookies.delete("cxf_contact_session_token")
|
73
|
+
cookies.delete("cxf_contact_refresh_token")
|
71
74
|
status = false
|
72
75
|
end
|
73
76
|
|
@@ -75,12 +78,10 @@ module ContactAuthHelper
|
|
75
78
|
end
|
76
79
|
|
77
80
|
def update_contact_tokens
|
78
|
-
access_token = @
|
79
|
-
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
|
81
|
+
access_token = @cxf_contact.get_client.session_token
|
82
|
+
refresh_token = @cxf_contact.get_client.refresh_token
|
82
83
|
|
83
|
-
cookies[
|
84
|
-
cookies[
|
84
|
+
cookies["cxf_contact_session_token"] = { value: access_token, secure: true, httponly: true} if access_token
|
85
|
+
cookies["cxf_contact_refresh_token"] = { value: refresh_token, secure: true, httponly: true } if refresh_token
|
85
86
|
end
|
86
87
|
end
|
@@ -29,8 +29,8 @@ module ProxyControllersMethods
|
|
29
29
|
}
|
30
30
|
|
31
31
|
if %w[contact user].include? controller_type
|
32
|
-
session_token = cookies["cxf_#{controller_type}_session_token"
|
33
|
-
refresh_token = cookies["cxf_#{controller_type}_refresh_token"
|
32
|
+
session_token = cookies["cxf_#{controller_type}_session_token"]
|
33
|
+
refresh_token = cookies["cxf_#{controller_type}_refresh_token"]
|
34
34
|
headers['Access-Token'] = session_token
|
35
35
|
headers['Refresh-Token'] = refresh_token
|
36
36
|
end
|
@@ -124,12 +124,9 @@ module ProxyControllersMethods
|
|
124
124
|
# set Cookies from response headers
|
125
125
|
if %w[contact user].include? controller_type
|
126
126
|
config.on_response do |_status_code, response|
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
# add expires to cookies
|
131
|
-
cookies["cxf_#{controller_type}_session_token".to_sym] = { value: response.header['Access-Token'], secure: true, httponly: true, expires: Time.at(session_token_expires_at) } if response.header['Access-Token']
|
132
|
-
cookies["cxf_#{controller_type}_refresh_token".to_sym] = { value: response.header['Refresh-Token'], secure: true, httponly: true, expires: Time.at(refresh_token_expires_at) } if response.header['Refresh-Token']
|
127
|
+
parsed_cookie = parse_set_cookie(response.header['Set-Cookie'])
|
128
|
+
cookies["cxf_#{controller_type}_session_token"] = { value: parsed_cookie['Access-Token'], secure: true, httponly: true } if parsed_cookie['Access-Token']
|
129
|
+
cookies["cxf_#{controller_type}_refresh_token"] = { value: parsed_cookie['Refresh-Token'], secure: true, httponly: true } if parsed_cookie['Refresh-Token']
|
133
130
|
end
|
134
131
|
end
|
135
132
|
|
@@ -140,4 +137,19 @@ module ProxyControllersMethods
|
|
140
137
|
end
|
141
138
|
end
|
142
139
|
end
|
140
|
+
|
141
|
+
private
|
142
|
+
|
143
|
+
def parse_set_cookie(set_cookie)
|
144
|
+
set_cookie = set_cookie.split(', ')
|
145
|
+
cookies_hash = {}
|
146
|
+
|
147
|
+
set_cookie.each do |cookie|
|
148
|
+
key, value = cookie.split('=')
|
149
|
+
value = value.split(';')[0]
|
150
|
+
cookies_hash[key] = value
|
151
|
+
end
|
152
|
+
|
153
|
+
cookies_hash
|
154
|
+
end
|
143
155
|
end
|
@@ -8,8 +8,8 @@ module UserAuthHelper
|
|
8
8
|
rescue => e
|
9
9
|
# Handle the client Unauthorized error
|
10
10
|
# if cxf response is negative delete the session cookie
|
11
|
-
cookies.delete(
|
12
|
-
cookies.delete(
|
11
|
+
cookies.delete("cxf_user_session_token")
|
12
|
+
cookies.delete("cxf_user_refresh_token")
|
13
13
|
response = nil
|
14
14
|
end
|
15
15
|
|
@@ -23,16 +23,17 @@ module UserAuthHelper
|
|
23
23
|
# Login in cxf
|
24
24
|
response = @cxf_user.login(email, password)
|
25
25
|
# Get session token from response
|
26
|
+
return response unless response.is_a? Hash
|
26
27
|
if response.key? 'data'
|
27
28
|
session_token = response['data']['access_token']
|
28
29
|
refresh_token = response['data']['refresh_token']
|
29
|
-
session_token_expires_at = Time.parse(response['data']['access_token_expires_at'])
|
30
|
-
refresh_token_expires_at = Time.parse(response['data']['refresh_token_expires_at'])
|
30
|
+
# session_token_expires_at = Time.parse(response['data']['access_token_expires_at'])
|
31
|
+
# refresh_token_expires_at = Time.parse(response['data']['refresh_token_expires_at'])
|
31
32
|
end
|
32
33
|
|
33
34
|
# Set a permanent cookie with the session token
|
34
|
-
cookies[
|
35
|
-
cookies[
|
35
|
+
cookies["cxf_user_session_token"] = { value: session_token, secure: true, httponly: true }
|
36
|
+
cookies["cxf_user_refresh_token"] = { value: refresh_token, secure: true, httponly: true}
|
36
37
|
end
|
37
38
|
|
38
39
|
##
|
@@ -43,8 +44,8 @@ module UserAuthHelper
|
|
43
44
|
response = @cxf_user.magic_link_login(hash)
|
44
45
|
if response['data']
|
45
46
|
# Set a cookie with the session token
|
46
|
-
cookies[
|
47
|
-
cookies[
|
47
|
+
cookies["cxf_user_session_token"] = { value: response['data']['access_token'], secure: true, httponly: true }
|
48
|
+
cookies["cxf_user_refresh_token"] = { value: response['data']['refresh_token'], secure: true, httponly: true }
|
48
49
|
redirect_to response['data']['redirect_url'] || '/'
|
49
50
|
else
|
50
51
|
redirect_to '/'
|
@@ -58,17 +59,15 @@ module UserAuthHelper
|
|
58
59
|
# Logout from cxf
|
59
60
|
# @cxf_user.logout
|
60
61
|
# Delete local cookie
|
61
|
-
cookies.delete(
|
62
|
-
cookies.delete(
|
62
|
+
cookies.delete("cxf_user_session_token")
|
63
|
+
cookies.delete("cxf_user_refresh_token")
|
63
64
|
end
|
64
65
|
|
65
66
|
def update_user_tokens
|
66
67
|
access_token = @cxf_user.get_client.session_token
|
67
68
|
refresh_token = @cxf_user.get_client.refresh_token
|
68
|
-
access_token_expires_at = @cxf_user.get_client.session_token_expires_at
|
69
|
-
refresh_token_expires_at = @cxf_user.get_client.refresh_token_expires_at
|
70
69
|
|
71
|
-
cookies[
|
72
|
-
cookies[
|
70
|
+
cookies["cxf_user_session_token"] = { value: access_token, secure: true, httponly: true} if access_token
|
71
|
+
cookies["cxf_user_refresh_token"] = { value: refresh_token, secure: true, httponly: true} if refresh_token
|
73
72
|
end
|
74
73
|
end
|
data/lib/user.rb
CHANGED
@@ -69,7 +69,8 @@ module Cxf
|
|
69
69
|
def login(email, password)
|
70
70
|
data = { email: email, password: password }
|
71
71
|
response = @client.raw('post', '/users/login', nil, data.to_json, '/api/v1', { no_content_type: true })
|
72
|
-
|
72
|
+
|
73
|
+
return response unless response.is_a? Hash
|
73
74
|
if response.key? 'data' and response['data'].key? 'access_token'
|
74
75
|
@client.session_token = response['data']['access_token']
|
75
76
|
@client.refresh_token = response['data']['refresh_token']
|