isomorfeus-transport 1.0.0.zeta24 → 2.0.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +21 -21
  3. data/README.md +27 -36
  4. data/lib/isomorfeus/transport/client_processor.rb +35 -35
  5. data/lib/isomorfeus/transport/config.rb +182 -166
  6. data/lib/isomorfeus/transport/hamster_session_store.rb +96 -0
  7. data/lib/isomorfeus/transport/handler/authentication_handler.rb +70 -70
  8. data/lib/isomorfeus/transport/imports.rb +9 -0
  9. data/lib/isomorfeus/transport/middlewares.rb +13 -13
  10. data/lib/isomorfeus/transport/rack_middleware.rb +59 -55
  11. data/lib/isomorfeus/transport/request_agent.rb +34 -34
  12. data/lib/isomorfeus/transport/response_agent.rb +23 -23
  13. data/lib/isomorfeus/transport/server_processor.rb +129 -129
  14. data/lib/isomorfeus/transport/server_socket_processor.rb +54 -54
  15. data/lib/isomorfeus/transport/ssr_login.rb +28 -28
  16. data/lib/isomorfeus/transport/version.rb +5 -5
  17. data/lib/isomorfeus/transport/{websocket.rb → websocket_client.rb} +123 -123
  18. data/lib/isomorfeus/transport.rb +200 -196
  19. data/lib/isomorfeus-transport.rb +70 -61
  20. data/lib/lucid_authentication/mixin.rb +122 -122
  21. data/lib/lucid_channel/base.rb +8 -9
  22. data/lib/lucid_channel/mixin.rb +105 -105
  23. data/lib/lucid_handler/base.rb +8 -9
  24. data/lib/lucid_handler/mixin.rb +27 -27
  25. data/node_modules/.package-lock.json +27 -0
  26. data/node_modules/ws/LICENSE +19 -0
  27. data/node_modules/ws/README.md +496 -0
  28. data/node_modules/ws/browser.js +8 -0
  29. data/node_modules/ws/index.js +13 -0
  30. data/node_modules/ws/lib/buffer-util.js +126 -0
  31. data/node_modules/ws/lib/constants.js +12 -0
  32. data/node_modules/ws/lib/event-target.js +266 -0
  33. data/node_modules/ws/lib/extension.js +203 -0
  34. data/node_modules/ws/lib/limiter.js +55 -0
  35. data/node_modules/ws/lib/permessage-deflate.js +511 -0
  36. data/node_modules/ws/lib/receiver.js +612 -0
  37. data/node_modules/ws/lib/sender.js +414 -0
  38. data/node_modules/ws/lib/stream.js +180 -0
  39. data/node_modules/ws/lib/subprotocol.js +62 -0
  40. data/node_modules/ws/lib/validation.js +124 -0
  41. data/node_modules/ws/lib/websocket-server.js +485 -0
  42. data/node_modules/ws/lib/websocket.js +1144 -0
  43. data/node_modules/ws/package.json +61 -0
  44. data/node_modules/ws/wrapper.mjs +8 -0
  45. data/package.json +6 -0
  46. metadata +76 -54
  47. data/lib/isomorfeus/transport/dbm_session_store.rb +0 -51
@@ -1,70 +1,70 @@
1
- module Isomorfeus
2
- module Transport
3
- module Handler
4
- class AuthenticationHandler < LucidHandler::Base
5
- TIMEOUT = 30
6
-
7
- on_request do |response_agent|
8
- # promise_send_path('Isomorfeus::Transport::Handler::AuthenticationHandler', 'login', user_class_name, user_identifier, user_password)
9
- response_agent.request.each_key do |login_or_logout|
10
- if login_or_logout == 'login'
11
- response_agent.agent_result = { error: 'Authentication failed' }
12
- tries = pub_sub_client.instance_variable_get(:@isomorfeus_authentication_tries)
13
- tries = 0 unless tries
14
- tries += 1
15
- sleep(5) if tries > 3 # TODO, this needs a better solution (store data in user/session)
16
- Isomorfeus.pub_sub_client.instance_variable_set(:@isomorfeus_authentication_tries, tries)
17
- response_agent.request['login'].each_key do |user_class_name|
18
- user = nil
19
- if Isomorfeus.valid_user_class_name?(user_class_name)
20
- user_class = Isomorfeus.cached_user_class(user_class_name)
21
- user_identifier = response_agent.request['login'][user_class_name].keys.first
22
- promise = user_class.promise_login(user: user_identifier, pass: response_agent.request['login'][user_class_name][user_identifier])
23
- unless promise.realized?
24
- start = Time.now
25
- until promise.realized?
26
- break if (Time.now - start) > TIMEOUT
27
- sleep 0.01
28
- end
29
- end
30
- user = promise.value
31
- end
32
- if user
33
- session_id = SecureRandom.uuid
34
- session_cookie = "session=#{session_id}; SameSite=Strict; HttpOnly; Path=/; Max-Age=2592000#{'; Secure' if Isomorfeus.production?}"
35
- session_cookie_accessor = SecureRandom.uuid
36
- Isomorfeus.pub_sub_client.instance_variable_set(:@isomorfeus_user, user)
37
- Isomorfeus.pub_sub_client.instance_variable_set(:@isomorfeus_authentication_tries, nil)
38
- Isomorfeus.pub_sub_client.instance_variable_set(:@isomorfeus_session_cookie, session_cookie)
39
- Isomorfeus.session_store.add(session_id: session_id, cookie: session_cookie, user: user, accessor: session_cookie_accessor)
40
- response_agent.agent_result = { success: 'ok', data: user.to_transport, session_cookie_accessor: session_cookie_accessor }
41
- end
42
- end
43
- elsif login_or_logout == 'ssr_login'
44
- response_agent.agent_result = { error: 'Authentication failed' }
45
- session_id = response_agent.request['ssr_login']
46
- user = Isomorfeus.session_store.get_user(session_id: session_id)
47
- if user
48
- Isomorfeus.pub_sub_client.instance_variable_set(:@isomorfeus_user, user)
49
- Isomorfeus.pub_sub_client.instance_variable_set(:@isomorfeus_authentication_tries, nil)
50
- Isomorfeus.pub_sub_client.instance_variable_set(:@isomorfeus_session_cookie, nil)
51
- response_agent.agent_result = { success: 'ok', data: user.to_transport }
52
- end
53
- elsif login_or_logout == 'logout'
54
- begin
55
- # bogus
56
- session_cookie = nil
57
- ensure
58
- Isomorfeus.pub_sub_client.instance_variable_set(:@isomorfeus_user, nil)
59
- Isomorfeus.pub_sub_client.instance_variable_set(:@isomorfeus_authentication_tries, nil)
60
- Isomorfeus.pub_sub_client.instance_variable_set(:@isomorfeus_session_cookie, nil)
61
- Isomorfeus.session_store.remove(cookie: session_cookie)
62
- response_agent.agent_result = { success: 'ok' }
63
- end
64
- end
65
- end
66
- end
67
- end
68
- end
69
- end
70
- end
1
+ module Isomorfeus
2
+ module Transport
3
+ module Handler
4
+ class AuthenticationHandler < LucidHandler::Base
5
+ TIMEOUT = 30
6
+
7
+ on_request do |response_agent|
8
+ # promise_send_path('Isomorfeus::Transport::Handler::AuthenticationHandler', 'login', user_class_name, user_identifier, user_password)
9
+ response_agent.request.each_key do |login_or_logout|
10
+ if login_or_logout == 'login'
11
+ response_agent.agent_result = { error: 'Authentication failed' }
12
+ tries = pub_sub_client.instance_variable_get(:@isomorfeus_authentication_tries)
13
+ tries = 0 unless tries
14
+ tries += 1
15
+ sleep(5) if tries > 3 # TODO, this needs a better solution (store data in user/session)
16
+ Isomorfeus.pub_sub_client.instance_variable_set(:@isomorfeus_authentication_tries, tries)
17
+ response_agent.request['login'].each_key do |user_class_name|
18
+ user = nil
19
+ if Isomorfeus.valid_user_class_name?(user_class_name)
20
+ user_class = Isomorfeus.cached_user_class(user_class_name)
21
+ user_identifier = response_agent.request['login'][user_class_name].keys.first
22
+ promise = user_class.promise_login(user: user_identifier, pass: response_agent.request['login'][user_class_name][user_identifier])
23
+ unless promise.realized?
24
+ start = Time.now
25
+ until promise.realized?
26
+ break if (Time.now - start) > TIMEOUT
27
+ sleep 0.01
28
+ end
29
+ end
30
+ user = promise.value
31
+ end
32
+ if user
33
+ session_id = SecureRandom.uuid
34
+ session_cookie = "session=#{session_id}; SameSite=Strict; HttpOnly; Path=/; Max-Age=2592000#{'; Secure' if Isomorfeus.production?}"
35
+ session_cookie_accessor = SecureRandom.uuid
36
+ Isomorfeus.pub_sub_client.instance_variable_set(:@isomorfeus_user, user)
37
+ Isomorfeus.pub_sub_client.instance_variable_set(:@isomorfeus_authentication_tries, nil)
38
+ Isomorfeus.pub_sub_client.instance_variable_set(:@isomorfeus_session_cookie, session_cookie)
39
+ Isomorfeus.session_store.add(session_id: session_id, cookie: session_cookie, user: user, accessor: session_cookie_accessor)
40
+ response_agent.agent_result = { success: 'ok', data: user.to_transport, session_cookie_accessor: session_cookie_accessor }
41
+ end
42
+ end
43
+ elsif login_or_logout == 'ssr_login'
44
+ response_agent.agent_result = { error: 'Authentication failed' }
45
+ session_id = response_agent.request['ssr_login']
46
+ user = Isomorfeus.session_store.get_user(session_id: session_id)
47
+ if user
48
+ Isomorfeus.pub_sub_client.instance_variable_set(:@isomorfeus_user, user)
49
+ Isomorfeus.pub_sub_client.instance_variable_set(:@isomorfeus_authentication_tries, nil)
50
+ Isomorfeus.pub_sub_client.instance_variable_set(:@isomorfeus_session_cookie, nil)
51
+ response_agent.agent_result = { success: 'ok', data: user.to_transport }
52
+ end
53
+ elsif login_or_logout == 'logout'
54
+ begin
55
+ # bogus
56
+ session_cookie = nil
57
+ ensure
58
+ Isomorfeus.pub_sub_client.instance_variable_set(:@isomorfeus_user, nil)
59
+ Isomorfeus.pub_sub_client.instance_variable_set(:@isomorfeus_authentication_tries, nil)
60
+ Isomorfeus.pub_sub_client.instance_variable_set(:@isomorfeus_session_cookie, nil)
61
+ Isomorfeus.session_store.remove(cookie: session_cookie)
62
+ response_agent.agent_result = { success: 'ok' }
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,9 @@
1
+ module Isomorfeus
2
+ module Transport
3
+ module Imports
4
+ def self.add
5
+ Isomorfeus.add_ssr_js_import('ws', 'WebSocket')
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,13 +1,13 @@
1
- module Isomorfeus
2
- module Transport
3
- module Middlewares
4
- def use_isomorfeus_middlewares
5
- STDOUT.puts "Isomorfeus is using the following middlewares:"
6
- Isomorfeus.middlewares.each do |isomorfeus_middleware|
7
- STDOUT.puts "#{isomorfeus_middleware}"
8
- use isomorfeus_middleware
9
- end
10
- end
11
- end
12
- end
13
- end
1
+ module Isomorfeus
2
+ module Transport
3
+ module Middlewares
4
+ def use_isomorfeus_middlewares
5
+ puts "Isomorfeus is using the following middlewares:"
6
+ Isomorfeus.middlewares.each do |isomorfeus_middleware|
7
+ puts "#{isomorfeus_middleware}"
8
+ use isomorfeus_middleware
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,55 +1,59 @@
1
- # frozen_string_literal: true
2
-
3
- module Isomorfeus
4
- module Transport
5
- class RackMiddleware
6
- WS_RESPONSE = [0, {}, []]
7
-
8
- def initialize(app)
9
- @app = app
10
- end
11
-
12
- def call(env)
13
- if env['PATH_INFO'] == Isomorfeus.api_websocket_path
14
- if env['rack.upgrade?'] == :websocket
15
- env['rack.upgrade'] = Isomorfeus::Transport::ServerSocketProcessor.new
16
- end
17
- WS_RESPONSE
18
- elsif env['PATH_INFO'] == Isomorfeus.cookie_eater_path
19
- cookie_accessor, new_path = env['QUERY_STRING'].split('=')
20
- cookie = Isomorfeus.session_store.take_cookie(accessor: cookie_accessor)
21
- if new_path.start_with?('/')
22
- if cookie
23
- [302, { 'Location' => new_path, 'Set-Cookie' => cookie }, ["Cookie eaten!"]]
24
- else
25
- [302, { 'Location' => new_path }, ["No Cookie!"]]
26
- end
27
- else
28
- [404, {}, ["Must specify relative path!"]]
29
- end
30
- else
31
- cookies = env['HTTP_COOKIE']
32
- if cookies
33
- cookies = cookies.split('; ')
34
- cookie = cookies.detect { |c| c.start_with?('session=') }
35
- if cookie
36
- session_id = cookie[8..-1]
37
- user = Isomorfeus.session_store.get_user(session_id: session_id)
38
- if user
39
- Thread.current[:isomorfeus_user] = user
40
- Thread.current[:isomorfeus_session_id] = session_id
41
- end
42
- end
43
- end
44
- begin
45
- result = @app.call(env)
46
- ensure
47
- Thread.current[:isomorfeus_user] = nil
48
- Thread.current[:isomorfeus_session_id] = nil
49
- end
50
- result
51
- end
52
- end
53
- end
54
- end
55
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Isomorfeus
4
+ module Transport
5
+ class RackMiddleware
6
+ WS_RESPONSE = [0, {}, []]
7
+
8
+ def initialize(app)
9
+ @app = app
10
+ end
11
+
12
+ def call(env)
13
+ if env['PATH_INFO'] == Isomorfeus.api_websocket_path
14
+ if env['rack.upgrade?'] == :websocket
15
+ env['rack.upgrade'] = Isomorfeus::Transport::ServerSocketProcessor.new
16
+ end
17
+ WS_RESPONSE
18
+ elsif env['PATH_INFO'] == Isomorfeus.cookie_eater_path
19
+ cookie_accessor, new_path = env['QUERY_STRING'].split('=')
20
+ cookie = Isomorfeus.session_store.take_cookie(accessor: cookie_accessor)
21
+ if new_path.start_with?('/')
22
+ if cookie
23
+ [302, { 'Location' => new_path, 'Set-Cookie' => cookie }, ["Cookie eaten!"]]
24
+ else
25
+ [302, { 'Location' => new_path }, ["No Cookie!"]]
26
+ end
27
+ else
28
+ [404, {}, ["Must specify relative path!"]]
29
+ end
30
+ else
31
+ cookies = env['HTTP_COOKIE']
32
+ if cookies
33
+ cookies = cookies.split('; ')
34
+ cookie = cookies.detect { |c| c.start_with?('session=') }
35
+ if cookie
36
+ session_id = cookie[8..-1]
37
+ user = Isomorfeus.session_store.get_user(session_id: session_id)
38
+ if user
39
+ Thread.current[:isomorfeus_user] = user
40
+ Thread.current[:isomorfeus_session_id] = session_id
41
+ end
42
+ end
43
+ end
44
+ begin
45
+ result = @app.call(env)
46
+ ensure
47
+ Thread.current[:isomorfeus_user] = nil
48
+ Thread.current[:isomorfeus_session_id] = nil
49
+ end
50
+ result
51
+ end
52
+ rescue Exception => e
53
+ # TODO
54
+ Isomorfeus.raise_error(error: e)
55
+ return [500, {}, '']
56
+ end
57
+ end
58
+ end
59
+ end
@@ -1,34 +1,34 @@
1
- module Isomorfeus
2
- module Transport
3
- class RequestAgent
4
- def self.agents
5
- @_agents ||= {}
6
- end
7
-
8
- def self.get(object_id)
9
- agents[object_id]
10
- end
11
-
12
- def self.get!(object_id)
13
- agents.delete(object_id.to_s)
14
- end
15
-
16
- attr_accessor :processed
17
- attr_accessor :result
18
- attr_accessor :response
19
- attr_accessor :full_response
20
- attr_accessor :sent
21
- attr_reader :id
22
- attr_reader :promise
23
- attr_reader :request
24
-
25
- def initialize(request = nil)
26
- @id = object_id.to_s
27
- self.class.agents[@id] = self
28
- @promise = Promise.new
29
- @request = request
30
- @sent = false
31
- end
32
- end
33
- end
34
- end
1
+ module Isomorfeus
2
+ module Transport
3
+ class RequestAgent
4
+ def self.agents
5
+ @_agents ||= {}
6
+ end
7
+
8
+ def self.get(object_id)
9
+ agents[object_id]
10
+ end
11
+
12
+ def self.get!(object_id)
13
+ agents.delete(object_id.to_s)
14
+ end
15
+
16
+ attr_accessor :processed
17
+ attr_accessor :result
18
+ attr_accessor :response
19
+ attr_accessor :full_response
20
+ attr_accessor :sent
21
+ attr_reader :id
22
+ attr_reader :promise
23
+ attr_reader :request
24
+
25
+ def initialize(request = nil)
26
+ @id = object_id.to_s
27
+ self.class.agents[@id] = self
28
+ @promise = Promise.new
29
+ @request = request
30
+ @sent = false
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,23 +1,23 @@
1
- module Isomorfeus
2
- module Transport
3
- class ResponseAgent
4
- attr_reader :agent_id
5
- attr_reader :request
6
- attr_accessor :agent_result
7
- attr_accessor :outer_result
8
- attr_accessor :error
9
-
10
- def initialize(agent_id, request)
11
- @agent_id = agent_id
12
- @request = request
13
- end
14
-
15
- def result
16
- return { response: { agent_ids: { @agent_id => @error }}} if @error
17
- response = { response: { agent_ids: { @agent_id => @agent_result }}}
18
- response.deep_merge!(@outer_result) if @outer_result
19
- return response
20
- end
21
- end
22
- end
23
- end
1
+ module Isomorfeus
2
+ module Transport
3
+ class ResponseAgent
4
+ attr_reader :agent_id
5
+ attr_reader :request
6
+ attr_accessor :agent_result
7
+ attr_accessor :outer_result
8
+ attr_accessor :error
9
+
10
+ def initialize(agent_id, request)
11
+ @agent_id = agent_id
12
+ @request = request
13
+ end
14
+
15
+ def result
16
+ return { response: { agent_ids: { @agent_id => @error }}} if @error
17
+ response = { response: { agent_ids: { @agent_id => @agent_result }}}
18
+ response.deep_merge!(@outer_result) if @outer_result
19
+ return response
20
+ end
21
+ end
22
+ end
23
+ end