wampproto 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/.rubocop.yml +39 -0
  4. data/CHANGELOG.md +5 -0
  5. data/CODE_OF_CONDUCT.md +84 -0
  6. data/LICENSE +22 -0
  7. data/README.md +35 -0
  8. data/Rakefile +12 -0
  9. data/Steepfile +33 -0
  10. data/lib/wampproto/acceptor/authenticator.rb +14 -0
  11. data/lib/wampproto/acceptor/request.rb +58 -0
  12. data/lib/wampproto/acceptor/response.rb +19 -0
  13. data/lib/wampproto/acceptor.rb +183 -0
  14. data/lib/wampproto/auth/anonymous.rb +14 -0
  15. data/lib/wampproto/auth/base.rb +20 -0
  16. data/lib/wampproto/auth/cra.rb +84 -0
  17. data/lib/wampproto/auth/cryptosign.rb +86 -0
  18. data/lib/wampproto/auth/helpers.rb +25 -0
  19. data/lib/wampproto/auth/ticket.rb +27 -0
  20. data/lib/wampproto/auth.rb +14 -0
  21. data/lib/wampproto/broker.rb +155 -0
  22. data/lib/wampproto/dealer.rb +143 -0
  23. data/lib/wampproto/id_generator.rb +23 -0
  24. data/lib/wampproto/joiner.rb +78 -0
  25. data/lib/wampproto/message/abort.rb +32 -0
  26. data/lib/wampproto/message/authenticate.rb +25 -0
  27. data/lib/wampproto/message/base.rb +30 -0
  28. data/lib/wampproto/message/call.rb +33 -0
  29. data/lib/wampproto/message/challenge.rb +25 -0
  30. data/lib/wampproto/message/error.rb +27 -0
  31. data/lib/wampproto/message/event.rb +33 -0
  32. data/lib/wampproto/message/goodbye.rb +25 -0
  33. data/lib/wampproto/message/hello.rb +64 -0
  34. data/lib/wampproto/message/invocation.rb +33 -0
  35. data/lib/wampproto/message/publish.rb +33 -0
  36. data/lib/wampproto/message/published.rb +25 -0
  37. data/lib/wampproto/message/register.rb +26 -0
  38. data/lib/wampproto/message/registered.rb +25 -0
  39. data/lib/wampproto/message/result.rb +32 -0
  40. data/lib/wampproto/message/subscribe.rb +26 -0
  41. data/lib/wampproto/message/subscribed.rb +25 -0
  42. data/lib/wampproto/message/unregister.rb +25 -0
  43. data/lib/wampproto/message/unregistered.rb +24 -0
  44. data/lib/wampproto/message/unsubscribe.rb +25 -0
  45. data/lib/wampproto/message/unsubscribed.rb +24 -0
  46. data/lib/wampproto/message/welcome.rb +41 -0
  47. data/lib/wampproto/message/yield.rb +32 -0
  48. data/lib/wampproto/message.rb +108 -0
  49. data/lib/wampproto/message_with_recipient.rb +13 -0
  50. data/lib/wampproto/serializer/cbor.rb +18 -0
  51. data/lib/wampproto/serializer/json.rb +18 -0
  52. data/lib/wampproto/serializer/msgpack.rb +18 -0
  53. data/lib/wampproto/serializer.rb +5 -0
  54. data/lib/wampproto/session.rb +144 -0
  55. data/lib/wampproto/session_details.rb +24 -0
  56. data/lib/wampproto/validate.rb +52 -0
  57. data/lib/wampproto/version.rb +5 -0
  58. data/lib/wampproto.rb +32 -0
  59. data/sig/cbor.rbs +5 -0
  60. data/sig/ed25519/signing_key.rbs +7 -0
  61. data/sig/ed25519/verify_key.rbs +7 -0
  62. data/sig/message_pack.rbs +5 -0
  63. data/sig/wampproto/acceptor/authenticator.rbs +9 -0
  64. data/sig/wampproto/acceptor/request.rbs +46 -0
  65. data/sig/wampproto/acceptor/response.rbs +25 -0
  66. data/sig/wampproto/acceptor.rbs +86 -0
  67. data/sig/wampproto/auth/anonymous.rbs +13 -0
  68. data/sig/wampproto/auth/base.rbs +25 -0
  69. data/sig/wampproto/auth/cra.rbs +34 -0
  70. data/sig/wampproto/auth/cryptosign.rbs +38 -0
  71. data/sig/wampproto/auth/helpers.rbs +16 -0
  72. data/sig/wampproto/auth/ticket.rbs +21 -0
  73. data/sig/wampproto/broker.rbs +48 -0
  74. data/sig/wampproto/dealer.rbs +49 -0
  75. data/sig/wampproto/id_generator.rbs +17 -0
  76. data/sig/wampproto/joiner.rbs +43 -0
  77. data/sig/wampproto/message/abort.rbs +31 -0
  78. data/sig/wampproto/message/authenticate.rbs +20 -0
  79. data/sig/wampproto/message/base.rbs +14 -0
  80. data/sig/wampproto/message/call.rbs +34 -0
  81. data/sig/wampproto/message/challenge.rbs +20 -0
  82. data/sig/wampproto/message/error.rbs +28 -0
  83. data/sig/wampproto/message/event.rbs +34 -0
  84. data/sig/wampproto/message/goodbye.rbs +21 -0
  85. data/sig/wampproto/message/hello.rbs +39 -0
  86. data/sig/wampproto/message/invocation.rbs +34 -0
  87. data/sig/wampproto/message/publish.rbs +32 -0
  88. data/sig/wampproto/message/published.rbs +20 -0
  89. data/sig/wampproto/message/register.rbs +24 -0
  90. data/sig/wampproto/message/registered.rbs +20 -0
  91. data/sig/wampproto/message/result.rbs +32 -0
  92. data/sig/wampproto/message/subscribe.rbs +24 -0
  93. data/sig/wampproto/message/subscribed.rbs +20 -0
  94. data/sig/wampproto/message/unregister.rbs +20 -0
  95. data/sig/wampproto/message/unregistered.rbs +16 -0
  96. data/sig/wampproto/message/unsubscribe.rbs +20 -0
  97. data/sig/wampproto/message/unsubscribed.rbs +16 -0
  98. data/sig/wampproto/message/welcome.rbs +36 -0
  99. data/sig/wampproto/message/yield.rbs +30 -0
  100. data/sig/wampproto/message.rbs +59 -0
  101. data/sig/wampproto/message_with_recipient.rbs +15 -0
  102. data/sig/wampproto/serializer/cbor.rbs +10 -0
  103. data/sig/wampproto/serializer/json.rbs +11 -0
  104. data/sig/wampproto/serializer/msgpack.rbs +11 -0
  105. data/sig/wampproto/session.rbs +43 -0
  106. data/sig/wampproto/session_details.rbs +25 -0
  107. data/sig/wampproto/validate.rbs +18 -0
  108. data/sig/wampproto.rbs +17 -0
  109. data/wampproto.gemspec +39 -0
  110. metadata +196 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 77d767efae20a706cae98c3ded62404ff67b4d36cd949232a86874939f9009d8
4
+ data.tar.gz: e29a02be8412a67f77bc1c03419ce0166b856987407286d909ae7c710b7d4b5e
5
+ SHA512:
6
+ metadata.gz: 8ed18913724993b58517dddd405c3b292216929aa4d44ef02160edb67e4c4da03e935202a9830010fbf232b865df4890d71bbbd74018849e52225ef369e0139b
7
+ data.tar.gz: 89bdcf2351cc252ca100f40ce2b4117b21ab9ca63cb85013e95e8212ca4eb522001f0e6b7266eca2336f1dd3d4da4a0b4123bb700a9e9b963de312b5fb44debd
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format progress
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,39 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.3
3
+ NewCops: enable
4
+
5
+ require:
6
+ - rubocop-rake
7
+ - rubocop-rspec
8
+
9
+ Style/StringLiterals:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Style/StringLiteralsInInterpolation:
14
+ Enabled: true
15
+ EnforcedStyle: double_quotes
16
+
17
+ Layout/LineLength:
18
+ Max: 120
19
+
20
+ Metrics/BlockLength:
21
+ Enabled: false
22
+
23
+ Layout/BeginEndAlignment:
24
+ EnforcedStyleAlignWith: start_of_line
25
+
26
+ Layout/EndAlignment:
27
+ EnforcedStyleAlignWith: start_of_line
28
+
29
+ Layout/MultilineMethodCallIndentation:
30
+ EnforcedStyle: indented
31
+
32
+ Layout/CaseIndentation:
33
+ EnforcedStyle: end
34
+
35
+ RSpec/NestedGroups:
36
+ Enabled: false
37
+
38
+ RSpec/MultipleMemoizedHelpers:
39
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-03-26
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at rubyonrails3@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Simple Things Inc.
4
+ Copyright (c) 2024 XConnIO Ltd.
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Wampproto
2
+
3
+ -Sans-IO WAMP protocol implementation in Ruby
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ ## Usage
16
+
17
+ TODO: Write usage instructions here
18
+
19
+ ## Development
20
+
21
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
22
+
23
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
24
+
25
+ ## Contributing
26
+
27
+ Bug reports and pull requests are welcome on GitHub at https://github.com/xconnio/wampproto.rb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/xconnio/wampproto.rb/blob/main/CODE_OF_CONDUCT.md).
28
+
29
+ ## License
30
+
31
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
32
+
33
+ ## Code of Conduct
34
+
35
+ Everyone interacting in the Wampproto project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/xconnio/wampproto.rb/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/Steepfile ADDED
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ target :lib do
4
+ signature "sig"
5
+
6
+ check "lib" # Directory name
7
+ # check "Gemfile" # File name
8
+ # check "app/models/**/*.rb" # Glob
9
+ # ignore "lib/templates/*.rb"
10
+
11
+ library "json" # Standard libraries
12
+ library "base64" # Standard libraries
13
+ library "openssl" # Standard libraries
14
+ library "securerandom" # Standard libraries
15
+ library "time" # Standard libraries
16
+ library "forwardable" # Standard libraries
17
+
18
+ # configure_code_diagnostics(D::Ruby.default) # `default` diagnostics setting (applies by default)
19
+ # configure_code_diagnostics(D::Ruby.strict) # `strict` diagnostics setting
20
+ # configure_code_diagnostics(D::Ruby.lenient) # `lenient` diagnostics setting
21
+ # configure_code_diagnostics(D::Ruby.silent) # `silent` diagnostics setting
22
+ # configure_code_diagnostics do |hash| # You can setup everything yourself
23
+ # hash[D::Ruby::NoMethod] = :information
24
+ # end
25
+ end
26
+
27
+ # target :test do
28
+ # signature "sig", "sig-private"
29
+ #
30
+ # check "test"
31
+ #
32
+ # # library "pathname" # Standard libraries
33
+ # end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Wampproto
4
+ class Acceptor
5
+ # Interface to handle Authentications
6
+ class Authenticator
7
+ class << self
8
+ def authenticate(request)
9
+ raise NotImplementedError
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "forwardable"
4
+
5
+ module Wampproto
6
+ class Acceptor
7
+ # Request object sent for authentication
8
+ class Request
9
+ extend Forwardable
10
+
11
+ attr_reader :realm, :authid, :authextra, :hello
12
+
13
+ def initialize(hello)
14
+ @hello = hello
15
+ end
16
+
17
+ def authmethod
18
+ @hello.authmethods[0].to_s.intern
19
+ end
20
+
21
+ def public_key
22
+ @hello.authextra.fetch(:public_key)
23
+ end
24
+
25
+ def secret
26
+ nil
27
+ end
28
+
29
+ def ticket
30
+ nil
31
+ end
32
+
33
+ def_delegators :@hello, :authid, :realm, :authextra, :authmethods
34
+ end
35
+
36
+ # AuthenticateRequest
37
+ class AuthenticateRequest < Request
38
+ extend Forwardable
39
+ attr_reader :authenticate
40
+
41
+ def initialize(authenticate, hello_request)
42
+ @authenticate = authenticate
43
+ @hello_request = hello_request
44
+ super(hello_request.hello)
45
+ end
46
+
47
+ def ticket
48
+ @authenticate.signature
49
+ end
50
+
51
+ def secret
52
+ @authenticate.signature
53
+ end
54
+
55
+ def_delegators :@hello_request, :authmethod, :authid, :realm, :authextra, :authmethods
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Wampproto
4
+ class Acceptor
5
+ # Request object sent for authentication
6
+ class Response
7
+ attr_reader :authid, :authrole, :secret
8
+
9
+ def initialize(authid, authrole, secret = nil)
10
+ @authid = authid
11
+ @authrole = authrole
12
+ @secret = secret
13
+ end
14
+
15
+ alias ticket secret
16
+ alias public_key secret
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,183 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "acceptor/request"
4
+ require_relative "acceptor/response"
5
+ require_relative "acceptor/authenticator"
6
+
7
+ module Wampproto
8
+ # Accepts Request
9
+ class Acceptor # rubocop:disable Metrics/ClassLength:
10
+ STATE_NONE = 0
11
+ STATE_HELLO_RECEIVED = 1
12
+ STATE_CHALLENGE_SENT = 2
13
+ STATE_WELCOME_SENT = 3
14
+ STATE_ERROR = 4
15
+
16
+ ROUTER_ROLES = { dealer: {}, broker: {} }.freeze
17
+
18
+ attr_reader :serializer, :session_id, :authmethod, :authenticator
19
+ attr_accessor :state, :session_details
20
+
21
+ def initialize(serializer = Serializer::JSON, authenticator = Acceptor::Authenticator)
22
+ @serializer = serializer
23
+ @authenticator = authenticator
24
+ @state = STATE_NONE
25
+ @session_id = create_session_id
26
+ # @authmethod = authenticator.authmethod
27
+ # @session_details = SessionDetails
28
+ end
29
+
30
+ def accepted?
31
+ @state == STATE_WELCOME_SENT
32
+ end
33
+
34
+ def receive(data)
35
+ msg = serializer.deserialize(data)
36
+ to_send = receive_message(msg)
37
+
38
+ [serializer.serialize(to_send), to_send.instance_of?(Message::Welcome)]
39
+ end
40
+
41
+ def receive_message(msg)
42
+ raise ProtocolViolation, "session was established, not expecting any new messages" if state == STATE_WELCOME_SENT
43
+
44
+ case msg
45
+ when Message::Hello
46
+ handle_hello_message(msg)
47
+ when Message::Authenticate
48
+ handle_authenticate_message(msg)
49
+ else
50
+ self.state = STATE_ERROR
51
+ Message::Abort.new({ message: "Received Abort" }, "wamp.error.received_abort")
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ def handle_hello_message(msg) # rubocop:disable Metrics/MethodLength
58
+ raise ValueError, "unknown state" if state != STATE_NONE
59
+
60
+ @hello = msg
61
+ @authid = msg.authid
62
+ @authmethod = msg.authmethods[0] || "anonymous"
63
+
64
+ @request = Wampproto::Acceptor::Request.new(msg)
65
+ @response = authenticator.authenticate(@request)
66
+
67
+ case @authmethod
68
+ when "cryptosign"
69
+ handle_cryptosign_hello(msg)
70
+ when "ticket"
71
+ handle_ticket_hello(msg)
72
+ when "wampcra"
73
+ handle_wampcra_hello(msg)
74
+ when "anonymous"
75
+ handle_anonymous_hello(msg)
76
+ else
77
+ raise ValueError, "unknown method"
78
+ end
79
+ end
80
+
81
+ def handle_wampcra_hello(msg)
82
+ @challenge = Auth::Cra.create_challenge(session_id, msg.authid, @response.authrole, "dynamic")
83
+ self.state = STATE_CHALLENGE_SENT
84
+
85
+ Message::Challenge.new(@authmethod, { challenge: @challenge })
86
+ end
87
+
88
+ def handle_ticket_hello(_msg)
89
+ self.state = STATE_CHALLENGE_SENT
90
+ Message::Challenge.new(@authmethod, {})
91
+ end
92
+
93
+ def handle_cryptosign_hello(msg)
94
+ @public_key = msg.authextra[:pubkey]
95
+ raise ValueError, "authextra must contain pubkey for cryptosign" unless @public_key
96
+
97
+ @challenge = Auth::Cryptosign.create_challenge
98
+ self.state = STATE_CHALLENGE_SENT
99
+
100
+ Message::Challenge.new(@authmethod, { challenge: @challenge })
101
+ end
102
+
103
+ def handle_anonymous_hello(msg)
104
+ self.state = STATE_WELCOME_SENT
105
+ welcome = Wampproto::Message::Welcome.new(
106
+ session_id, {
107
+ roles: ROUTER_ROLES, authid: "anonymous", authmethod: "anonymous", authrole: "anonymous"
108
+ }
109
+ )
110
+ self.session_details = SessionDetails.new(session_id, msg.realm, welcome.authid, welcome.authrole)
111
+
112
+ welcome
113
+ end
114
+
115
+ def handle_authenticate_message(msg) # rubocop:disable Metrics/MethodLength
116
+ raise ValueError, "unknown state" if state != STATE_CHALLENGE_SENT
117
+
118
+ @auth_request = AuthenticateRequest.new(msg, @request)
119
+ @response = authenticator.authenticate(@auth_request)
120
+
121
+ case @authmethod
122
+ when "cryptosign"
123
+ handle_cryptosign_authenticate(msg)
124
+ when "ticket"
125
+ handle_ticket_authenticate(msg)
126
+ when "wampcra"
127
+ handle_wampcra_authenticate(msg)
128
+ else
129
+ Message::Abort.new({ message: "Invalid AuthMethod: #{@authmethod}" }, "wamp.error.protocol_violation")
130
+ end
131
+ end
132
+
133
+ def handle_wampcra_authenticate(msg)
134
+ return abort_message unless Wampproto::Auth::Cra.verify_challenge(msg.signature, @challenge, @response.secret)
135
+
136
+ self.state = STATE_WELCOME_SENT
137
+ welcome = Wampproto::Message::Welcome.new(
138
+ session_id, {
139
+ roles: ROUTER_ROLES, authid: @authid, authmethod: @authmethod, authrole: @response.authrole
140
+ }
141
+ )
142
+ self.session_details = SessionDetails.new(session_id, @hello.realm, welcome.authid, welcome.authrole)
143
+
144
+ welcome
145
+ end
146
+
147
+ def abort_message
148
+ Message::Abort.new({ message: "Unable to authenticate user" }, "wamp.error.invalid_credentials")
149
+ end
150
+
151
+ def handle_ticket_authenticate(msg)
152
+ return abort_message unless @response.secret == msg.signature
153
+
154
+ self.state = STATE_WELCOME_SENT
155
+ welcome = Wampproto::Message::Welcome.new(
156
+ session_id, {
157
+ roles: ROUTER_ROLES, authid: @authid, authmethod: @authmethod, authrole: @response.authrole
158
+ }
159
+ )
160
+ self.session_details = SessionDetails.new(session_id, @hello.realm, welcome.authid, welcome.authrole)
161
+
162
+ welcome
163
+ end
164
+
165
+ def handle_cryptosign_authenticate(msg)
166
+ return abort_message unless Auth::Cryptosign.verify_challenge(msg.signature, @challenge, @public_key)
167
+
168
+ self.state = STATE_WELCOME_SENT
169
+ welcome = Wampproto::Message::Welcome.new(
170
+ session_id, {
171
+ roles: ROUTER_ROLES, authid: @authid, authmethod: @authmethod, authrole: "user"
172
+ }
173
+ )
174
+ self.session_details = SessionDetails.new(session_id, @hello.realm, welcome.authid, welcome.authrole)
175
+
176
+ welcome
177
+ end
178
+
179
+ def create_session_id
180
+ rand(100_000..9_007_199_254_740_992)
181
+ end
182
+ end
183
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Wampproto
4
+ module Auth
5
+ # anonymous authentication
6
+ class Anonymous < Base
7
+ AUTH_METHOD = "anonymous"
8
+
9
+ def initialize(authid = "", authextra = {})
10
+ super(AUTH_METHOD, authid, authextra)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Wampproto
4
+ module Auth
5
+ # Auth Base
6
+ class Base
7
+ attr_reader :authmethod, :authid, :authextra
8
+
9
+ def initialize(method, authid, authextra = {})
10
+ @authmethod = Validate.string!("AuthMethod", method)
11
+ @authid = Validate.string!("AuthId", authid)
12
+ @authextra = Validate.hash!("AuthExtra", authextra)
13
+ end
14
+
15
+ def authenticate(_challenge)
16
+ raise NotImplementedError
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openssl"
4
+ require "base64"
5
+ require "time"
6
+
7
+ module Wampproto
8
+ module Auth
9
+ # generates wampcra authentication signature
10
+ class Cra < Base
11
+ include Helpers
12
+
13
+ attr_reader :secret
14
+
15
+ AUTH_METHOD = "wampcra"
16
+
17
+ # rubocop:disable Metrics/ParameterLists
18
+ def initialize(secret, authid, authextra = {}, salt = nil, keylen = 32, iterations = 100)
19
+ @secret = Validate.string!("Secret", secret)
20
+ @salt = Validate.string!("Salt", salt) if salt
21
+ @keylen = Validate.int!("Keylen", keylen) if salt
22
+ @iterations = Validate.int!("Iterations", iterations) if salt
23
+ super(AUTH_METHOD, authid, authextra)
24
+ end
25
+ # rubocop:enable Metrics/ParameterLists
26
+
27
+ def authenticate(challenge)
28
+ signature = create_signature(challenge)
29
+ Message::Authenticate.new(signature)
30
+ end
31
+
32
+ class << self
33
+ def create_challenge(session_id, authid, authrole, authprovider)
34
+ nounce = binary_to_hex(SecureRandom.random_bytes(16))
35
+ {
36
+ authid:,
37
+ authrole:,
38
+ authprovider:,
39
+ nounce:,
40
+ authmethod: AUTH_METHOD,
41
+ session_id:,
42
+ timestamp: Time.now.utc.iso8601(3)
43
+ }.to_json
44
+ end
45
+
46
+ # rubocop:disable Metrics/ParameterLists
47
+ def verify_challenge(signature, challenge, secret, salt = nil, keylen = 32, iterations = 100)
48
+ encoded_challenge = sign_challenge(secret, challenge, salt, keylen, iterations)
49
+ signature == encoded_challenge
50
+ end
51
+ # rubocop:enable Metrics/ParameterLists
52
+
53
+ def sign_challenge(secret, challenge, salt = nil, keylen = 32, iterations = 100)
54
+ unless salt.nil?
55
+ hmac = OpenSSL::HMAC.new(
56
+ create_derive_secret(secret, salt, keylen, iterations),
57
+ "SHA256"
58
+ )
59
+ end
60
+ hmac ||= OpenSSL::HMAC.new(secret, "SHA256")
61
+
62
+ hmac.update(challenge)
63
+
64
+ Base64.encode64(hmac.digest).rstrip
65
+ end
66
+
67
+ def create_derive_secret(secret, salt, length, iterations)
68
+ key = OpenSSL::KDF.pbkdf2_hmac(secret, salt:, iterations:, length:, hash: "SHA256")
69
+ binary_to_hex(key)
70
+ end
71
+ end
72
+
73
+ private
74
+
75
+ def create_signature(challenge)
76
+ extra = challenge.extra
77
+
78
+ self.class.sign_challenge(
79
+ secret, extra[:challenge], extra[:salt], extra[:keylen], extra[:iterations]
80
+ )
81
+ end
82
+ end
83
+ end
84
+ end