siwe 1.1.0 → 2.0.0

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: 9ce4aaa56993884786c2eb990729406a1ae4e0cb99d43f5dbcc6b3e0c8e0af67
4
- data.tar.gz: 4dbcb76592a3c0bd4eed2e53e3a739fe9461f185772fdb70fa06dfcd43f7adec
3
+ metadata.gz: 933ba5a3c973570550d45cd839212a57c6a66568e17a2b107be945fc5ee1c6f3
4
+ data.tar.gz: 6ea0957b133e576102f9038cee3a7c4459cba2d7bbbe11d4268ac8d464e5982b
5
5
  SHA512:
6
- metadata.gz: fb5e213eb8d8f50f6cf9a4c00be528bd0a5a9afdc806f95d2714534f629f3ea538bc79655330fc313b01ff85f7ac93550f2447c84baa1f5996eaf6617543630c
7
- data.tar.gz: ddf5168c793f6342f9f2e49ac8112dca937832fd7e6a42fb8362250cb6c8798ddb26bf444514064f25fa29ea09ad06d1827cdfc5a1ba696667448d04eb05eee6
6
+ metadata.gz: 9ecf2bac358261ae3cd49f8c5ef43ddc7724360705dddaf2704cf3ba427bbb2181bc20a5189cc66bd991144cc669a8fb9555db36780f439dfa6b0d007c941202
7
+ data.tar.gz: d63ad437299d1386f5cfce1ce44a4d76ecd7a8bcffd3e819f3d2b625196a3b4ced5128544a1413c76447b918e901fa78b63ed4268b6785c631ba18e3675c5327
data/.rubocop.yml CHANGED
@@ -32,5 +32,11 @@ Metrics/ClassLength:
32
32
  Style/OptionalBooleanParameter:
33
33
  Enabled: false
34
34
 
35
+ Style/RegexpLiteral:
36
+ Enabled: false
37
+
38
+ Lint/MixedRegexpCaptureTypes:
39
+ Enabled: false
40
+
35
41
  Layout/LineLength:
36
- Max: 120
42
+ Enabled: false
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- siwe (1.0.0)
4
+ siwe (2.0.0)
5
5
  eth (~> 0.5.1)
6
6
 
7
7
  GEM
@@ -12,7 +12,7 @@ GEM
12
12
  benchmark (0.2.0)
13
13
  diff-lcs (1.5.0)
14
14
  e2mmap (0.1.0)
15
- eth (0.5.1)
15
+ eth (0.5.2)
16
16
  keccak (~> 1.3)
17
17
  konstructor (~> 1.0)
18
18
  openssl (~> 2.2)
@@ -22,7 +22,7 @@ GEM
22
22
  ffi-compiler (1.0.1)
23
23
  ffi (>= 1.0.0)
24
24
  rake
25
- ipaddr (1.2.3)
25
+ ipaddr (1.2.4)
26
26
  jaro_winkler (1.5.4)
27
27
  keccak (1.3.0)
28
28
  konstructor (1.0.2)
@@ -30,7 +30,7 @@ GEM
30
30
  rexml
31
31
  kramdown-parser-gfm (1.1.0)
32
32
  kramdown (~> 2.0)
33
- mini_portile2 (2.7.1)
33
+ mini_portile2 (2.8.0)
34
34
  nokogiri (1.13.1-x86_64-linux)
35
35
  racc (~> 1.4)
36
36
  openssl (2.2.1)
data/LICENSE-APACHE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
File without changes
@@ -3,14 +3,49 @@
3
3
  module Siwe
4
4
  # Used when the message is already expired. (Expires At < Time.now)
5
5
  class ExpiredMessage < StandardError
6
- def initialize(msg = "Message expired.")
6
+ def initialize(msg = "Expired message.")
7
+ super
8
+ end
9
+ end
10
+
11
+ # Used when the domain is not a valid authority or is empty.
12
+ class InvalidDomain < StandardError
13
+ def initialize(msg = "Invalid domain.")
14
+ super
15
+ end
16
+ end
17
+
18
+ # Used when the domain doesn't match the domain provided for verification.
19
+ class DomainMismatch < StandardError
20
+ def initialize(msg = "Domain does not match provided domain for verification.")
21
+ super
22
+ end
23
+ end
24
+
25
+ # Used when the nonce doesn't match the nonce provided for verification.
26
+ class NonceMismatch < StandardError
27
+ def initialize(msg = "Nonce does not match provided nonce for verification.")
7
28
  super
8
29
  end
9
30
  end
10
31
 
11
32
  # Used when the address does not conform to EIP-55 or is invalid.
12
33
  class InvalidAddress < StandardError
13
- def initialize(msg = "Adress does not conform to EIP-55 or is invalid.")
34
+ def initialize(msg = "Address does not conform to EIP-55 or is invalid.")
35
+ super
36
+ end
37
+ end
38
+
39
+ # Used when the message is created with an invalid URI
40
+ class InvalidURI < StandardError
41
+ def initialize(msg = "URI does not conform to RFC 3986.")
42
+ super
43
+ end
44
+ end
45
+
46
+ # Used when the nonce is smaller then 8 characters or is not alphanumeric
47
+ class InvalidNonce < StandardError
48
+ def initialize(msg = "Nonce size smaller then 8 characters or is not alphanumeric.")
14
49
  super
15
50
  end
16
51
  end
@@ -22,10 +57,31 @@ module Siwe
22
57
  end
23
58
  end
24
59
 
60
+ # Used when the message contains a time format not compliant to ISO8601.
61
+ class InvalidTimeFormat < StandardError
62
+ def initialize(field, msg = "Invalid time format for: #{field}")
63
+ super
64
+ end
65
+ end
66
+
67
+ # Used when the message version is not 1.
68
+ class InvalidMessageVersion < StandardError
69
+ def initialize(msg = "Invalid message version.")
70
+ super
71
+ end
72
+ end
73
+
25
74
  # Used when the signature doesn't correspond to the address of the message.
26
75
  class InvalidSignature < StandardError
27
76
  def initialize(msg = "Signature doesn't match message.")
28
77
  super
29
78
  end
30
79
  end
80
+
81
+ # Used when the message doesn't match the RegExp.
82
+ class UnableToParseMessage < StandardError
83
+ def initialize(msg = "Unable to parse message.")
84
+ super
85
+ end
86
+ end
31
87
  end
data/lib/siwe/message.rb CHANGED
@@ -4,26 +4,27 @@ require "time"
4
4
  require "eth"
5
5
  require "json"
6
6
 
7
- SIWE_DOMAIN = "^(?<domain>([^?#]*)) wants you to sign in with your Ethereum account:\\n"
8
- SIWE_ADDRESS = "(?<address>0x[a-zA-Z0-9]{40})\\n\\n"
9
- SIWE_STATEMENT = "((?<statement>[^\\n]+)\\n)?\\n"
10
- SIWE_URI = "(([^:?#]+):)?(([^?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))"
11
- SIWE_URI_LINE = "URI: (?<uri>#{SIWE_URI}?)\\n"
12
- SIWE_VERSION = "Version: (?<version>1)\\n"
13
- SIWE_CHAIN_ID = "Chain ID: (?<chain_id>[0-9]+)\\n"
14
- SIWE_NONCE = "Nonce: (?<nonce>[a-zA-Z0-9]{8,})\\n"
15
- SIWE_DATETIME = "([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9])"\
16
- ":([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))"
17
- SIWE_ISSUED_AT = "Issued At: (?<issued_at>#{SIWE_DATETIME})"
18
- SIWE_EXPIRATION_TIME = "(\\nExpiration Time: (?<expiration_time>#{SIWE_DATETIME}))?"
19
- SIWE_NOT_BEFORE = "(\\nNot Before: (?<not_before>#{SIWE_DATETIME}))?"
20
- SIWE_REQUEST_ID = "(\\nRequest ID: (?<request_id>[-._~!$&'()*+,;=:@%a-zA-Z0-9]*))?"
21
- SIWE_RESOURCES = "(\\nResources:(?<resources>(\\n- #{SIWE_URI}?)+))?$"
22
-
23
- SIWE_MESSAGE = "#{SIWE_DOMAIN}#{SIWE_ADDRESS}#{SIWE_STATEMENT}#{SIWE_URI_LINE}#{SIWE_VERSION}#{SIWE_CHAIN_ID}"\
24
- "#{SIWE_NONCE}#{SIWE_ISSUED_AT}#{SIWE_EXPIRATION_TIME}#{SIWE_NOT_BEFORE}#{SIWE_REQUEST_ID}"\
25
- "#{SIWE_RESOURCES}"
26
-
7
+ DOMAIN = %r{(?<domain>[^/?#]+)}.freeze
8
+ SIWE_DOMAIN = %r{^#{DOMAIN.source} wants you to sign in with your Ethereum account:}.freeze
9
+
10
+ SIWE_ADDRESS = %r{\n(?<address>0x[a-zA-Z0-9]{40})\n\n}.freeze
11
+ SIWE_STATEMENT = %r{((?<statement>[^\n]+)\n)?}.freeze
12
+ RFC3986 = %r{(([^:?#]+):)?(([^?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?}.freeze
13
+ SIWE_URI_LINE = %r{\nURI: (?<uri>#{RFC3986.source}?)}.freeze
14
+ SIWE_VERSION = %r{\nVersion: (?<version>1)}.freeze
15
+ SIWE_CHAIN_ID = %r{\nChain ID: (?<chain_id>[0-9]+)}.freeze
16
+ SIWE_NONCE = %r{\nNonce: (?<nonce>[a-zA-Z0-9]{8,})}.freeze
17
+ SIWE_DATETIME = %r{([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|([+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))}.freeze
18
+ SIWE_ISSUED_AT = %r{\nIssued At: (?<issued_at>#{SIWE_DATETIME.source})}.freeze
19
+ SIWE_EXPIRATION_TIME = %r{(\nExpiration Time: (?<expiration_time>#{SIWE_DATETIME.source}))?}.freeze
20
+ SIWE_NOT_BEFORE = %r{(\nNot Before: (?<not_before>#{SIWE_DATETIME.source}))?}.freeze
21
+ SIWE_REQUEST_ID = %r{(\nRequest ID: (?<request_id>[-._~!$&'()*+,;=:@%a-zA-Z0-9]*))?}.freeze
22
+ SIWE_RESOURCES = %r{(\nResources:(?<resources>(\n- #{RFC3986.source}?)+))?$}.freeze
23
+
24
+ SIWE_MESSAGE = Regexp.new(SIWE_DOMAIN.source + SIWE_ADDRESS.source + SIWE_STATEMENT.source + SIWE_URI_LINE.source +
25
+ SIWE_VERSION.source + SIWE_CHAIN_ID.source + SIWE_NONCE.source + SIWE_ISSUED_AT.source +
26
+ SIWE_EXPIRATION_TIME.source + SIWE_NOT_BEFORE.source + SIWE_REQUEST_ID.source +
27
+ SIWE_RESOURCES.source)
27
28
  module Siwe
28
29
  # Class that defines the EIP-4361 message fields and some utility methods to
29
30
  # generate/validate the messages
@@ -76,47 +77,41 @@ module Siwe
76
77
 
77
78
  def initialize(domain, address, uri, version, options = {})
78
79
  @domain = domain
79
- begin
80
- @address = Eth::Address.new(address).to_s
81
- rescue StandardError
82
- raise Siwe::InvalidAddress
83
- end
84
- raise Siwe::InvalidAddress unless @address.eql? address
85
-
80
+ @address = address
86
81
  @uri = uri
87
82
  @version = version
88
83
  @statement = options.fetch :statement, ""
89
84
  @issued_at = options.fetch :issued_at, Time.now.utc.iso8601
90
85
  @nonce = options.fetch :nonce, Siwe::Util.generate_nonce
91
- @chain_id = options.fetch :chain_id, "1"
86
+ @chain_id = options.fetch :chain_id, 1
92
87
  @expiration_time = options.fetch :expiration_time, ""
93
88
  @not_before = options.fetch :not_before, ""
94
89
  @request_id = options.fetch :request_id, ""
95
90
  @resources = options.fetch :resources, []
91
+ validate
96
92
  end
97
93
 
98
94
  def self.from_message(msg)
99
- if (message = msg.match SIWE_MESSAGE)
100
- new(
101
- message[:domain],
102
- Eth::Address.new(message[:address]).to_s,
103
- message[:uri],
104
- message[:version],
105
- {
106
- statement: message[:statement] || "",
107
- issued_at: message[:issued_at],
108
- nonce: message[:nonce],
109
- chain_id: message[:chain_id],
110
- expiration_time: message[:expiration_time] || "",
111
- not_before: message[:not_before] || "",
112
- request_id: message[:request_id] || "",
113
- resources: message[:resources]&.split("\n- ")&.drop(1) || []
114
- }
115
- )
116
-
117
- else
118
- throw "Invalid message input."
119
- end
95
+ message = msg.match SIWE_MESSAGE
96
+
97
+ raise Siwe::UnableToParseMessage unless message.to_s == msg
98
+
99
+ new(
100
+ message[:domain],
101
+ message[:address],
102
+ message[:uri],
103
+ message[:version],
104
+ {
105
+ statement: message[:statement],
106
+ issued_at: message[:issued_at],
107
+ nonce: message[:nonce],
108
+ chain_id: message[:chain_id].to_i,
109
+ expiration_time: message[:expiration_time],
110
+ not_before: message[:not_before],
111
+ request_id: message[:request_id],
112
+ resources: message[:resources]&.split("\n- ")&.drop(1)
113
+ }
114
+ )
120
115
  end
121
116
 
122
117
  def to_json_string
@@ -156,11 +151,61 @@ module Siwe
156
151
  )
157
152
  end
158
153
 
159
- def validate(signature)
160
- raise Siwe::ExpiredMessage if !@expiration_time.empty? && Time.now.utc > Time.parse(@expiration_time)
161
- raise Siwe::NotValidMessage if !@not_before.empty? && Time.now.utc < Time.parse(@not_before)
154
+ def validate
155
+ # check domain
156
+ raise Siwe::InvalidDomain unless @domain.match %r{[^/?#]*} || @domain.empty?
157
+
158
+ # check address EIP-55
159
+ raise Siwe::InvalidAddress unless Eth::Address.new(@address).to_s.eql? @address
160
+
161
+ # check uri
162
+ raise Siwe::InvalidURI unless URI.parse(@uri)
163
+
164
+ # check version
165
+ raise Siwe::InvalidMessageVersion unless @version == "1"
166
+
167
+ # check if the nonce is alphanumeric and bigger then 8 characters
168
+ raise Siwe::InvalidNonce unless @nonce.match(%r{[a-zA-Z0-9]{8,}})
169
+
170
+ # check issued_at format
171
+ begin
172
+ Time.iso8601(@issued_at)
173
+ rescue ArgumentError
174
+ raise Siwe::InvalidTimeFormat, "issued_at"
175
+ end
176
+
177
+ # check exp_time
178
+ begin
179
+ Time.iso8601(@expiration_time) unless @expiration_time.nil? || @expiration_time.empty?
180
+ rescue ArgumentError
181
+ raise Siwe::InvalidTimeFormat, "expiration_time"
182
+ end
183
+
184
+ # check not_before
185
+ begin
186
+ Time.iso8601(@not_before) unless @not_before.nil? || @not_before.empty?
187
+ rescue ArgumentError
188
+ raise Siwe::InvalidTimeFormat, "not_before"
189
+ end
190
+
191
+ # check resources
192
+ raise Siwe::InvalidURI unless @resources.nil? || @resources.empty? || @resources.each { |uri| URI.parse(uri) }
193
+ end
194
+
195
+ def verify(signature, domain, time, nonce)
196
+ raise Siwe::DomainMismatch unless domain.nil? || domain.eql?(@domain)
197
+
198
+ raise Siwe::NonceMismatch unless nonce.nil? || nonce.eql?(@nonce)
199
+
200
+ check_time = time.nil? ? Time.now.utc : Time.iso8601(time)
201
+
202
+ raise Siwe::ExpiredMessage if (!@expiration_time.nil? && !@expiration_time.empty?) && check_time > Time.iso8601(@expiration_time)
203
+
204
+ raise Siwe::NotValidMessage if (!@not_before.nil? && !@not_before.empty?) && check_time < Time.iso8601(@not_before)
205
+
206
+ raise Siwe::InvalidSignature if signature.nil? && signature.empty?
162
207
 
163
- raise Siwe::InvalidSignature if signature.empty?
208
+ raise Siwe::InvalidAddress unless @address.eql?(Eth::Address.new(@address).to_s)
164
209
 
165
210
  begin
166
211
  pub_key = Eth::Signature.personal_recover prepare_message, signature
@@ -181,7 +226,7 @@ module Siwe
181
226
 
182
227
  header = [greeting, address]
183
228
 
184
- if @statement.empty?
229
+ if @statement.nil? || @statement.empty?
185
230
  header.push "\n"
186
231
  else
187
232
  header.push statement
@@ -200,7 +245,6 @@ module Siwe
200
245
  expiration_time = "Expiration Time: #{@expiration_time}"
201
246
  not_before = "Not Before: #{@not_before}"
202
247
  request_id = "Request ID: #{@request_id}"
203
- resources = "Resources:\n#{@resources.map { |x| "- #{x}" }.join "\n"}"
204
248
 
205
249
  body.push expiration_time unless @expiration_time.to_s.strip.empty?
206
250
 
@@ -208,7 +252,7 @@ module Siwe
208
252
 
209
253
  body.push request_id unless @request_id.to_s.strip.empty?
210
254
 
211
- body.push resources unless @resources.empty?
255
+ body.push "Resources:\n#{@resources.map { |x| "- #{x}" }.join "\n"}" unless @resources.nil? || @resources.empty?
212
256
 
213
257
  body = body.join "\n"
214
258
 
data/lib/siwe/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Siwe
4
- VERSION = "1.1.0"
4
+ VERSION = "2.0.0"
5
5
  end
data/lib/siwe.rb CHANGED
@@ -7,9 +7,17 @@ module Siwe
7
7
  autoload :Message, "siwe/message"
8
8
  autoload :Util, "siwe/util"
9
9
  autoload :ExpiredMessage, "siwe/exceptions"
10
+ autoload :InvalidDomain, "siwe/exceptions"
11
+ autoload :DomainMismatch, "siwe/exceptions"
12
+ autoload :NonceMismatch, "siwe/exceptions"
13
+ autoload :InvalidAddress, "siwe/exceptions"
14
+ autoload :InvalidURI, "siwe/exceptions"
15
+ autoload :InvalidNonce, "siwe/exceptions"
10
16
  autoload :NotValidMessage, "siwe/exceptions"
17
+ autoload :InvalidTimeFormat, "siwe/exceptions"
18
+ autoload :InvalidMessageVersion, "siwe/exceptions"
11
19
  autoload :InvalidSignature, "siwe/exceptions"
12
- autoload :InvalidAddress, "siwe/exceptions"
20
+ autoload :UnableToParseMessage, "siwe/exceptions"
13
21
 
14
22
  class Error < StandardError; end
15
23
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: siwe
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Spruce Systems Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-07 00:00:00.000000000 Z
11
+ date: 2022-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: eth
@@ -94,8 +94,8 @@ files:
94
94
  - CODE_OF_CONDUCT.md
95
95
  - Gemfile
96
96
  - Gemfile.lock
97
- - LICENSE
98
- - LICENSE.txt
97
+ - LICENSE-APACHE
98
+ - LICENSE-MIT
99
99
  - README.md
100
100
  - Rakefile
101
101
  - bin/console
data/LICENSE.txt DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2021 Gregório Granado Magalhães
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.