ruby_magic_link 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b388ac403bc4f6041bc487bcd64d8a7fff1712a1e2691aa5477ab52652f3f5f9
4
+ data.tar.gz: fc942055ed95c2ead29a6dae7cd8b9d332714a93fe9216590362d09fdbce3138
5
+ SHA512:
6
+ metadata.gz: 38d0072644a3b821b222652c96841c4e5321d503e78e980844e6a02498c53ee4d6d1a9b72e4968c6f50f2e8f98bbd332458761c26608354e1a6aa12fafad5e5a
7
+ data.tar.gz: e2e316ddc3cb71ae527400393a58285fca52101eb96e4aa322275d561760ba7fbd3ee2e2555415a3e2191bcde73781666a30899591373ec452634f041de8273d
data/LICENSE.txt ADDED
@@ -0,0 +1,9 @@
1
+ Copyright (c) 2024 Igor Korepanov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,11 @@
1
+ ## License
2
+
3
+ Copyright (c) 2024 Igor Korepanov
4
+
5
+ MIT License
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
+
9
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
+
11
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyMagicLink
4
+ class Configuration
5
+ attr_accessor :secret_key, :token_expiration
6
+ end
7
+
8
+ class << self
9
+ attr_accessor :config
10
+
11
+ def setup
12
+ self.config ||= Configuration.new
13
+ yield(config)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'openssl'
4
+ require 'base64'
5
+ require 'json'
6
+
7
+ module RubyMagicLink
8
+ module Token
9
+ DELIMITER = '!'
10
+ ALGORITHM = 'AES-256-CBC'
11
+
12
+ class TokenObject
13
+ def initialize(data)
14
+ @data = data
15
+ end
16
+
17
+ def expired?
18
+ decoded_data['expires_in'] < Time.now.to_i
19
+ end
20
+
21
+ def payload
22
+ decoded_data['payload']
23
+ end
24
+
25
+ private
26
+
27
+ def decoded_data
28
+ @decoded_data ||= RubyMagicLink::Token.decode_token(data)
29
+ end
30
+
31
+ attr_reader :data
32
+ end
33
+
34
+ module_function
35
+
36
+ def create(payload, expires_in: nil)
37
+ data = { payload: payload }
38
+ data[:expires_in] = expires_in if expires_in
39
+ iv = OpenSSL::Random.random_bytes(16)
40
+ str = JSON.generate(data)
41
+ Base64.urlsafe_encode64(Base64.urlsafe_encode64(iv) + DELIMITER + encrypt(str, RubyMagicLink.config.secret_key, iv))
42
+ end
43
+
44
+ def decode(data)
45
+ TokenObject.new(data)
46
+ end
47
+
48
+ def decode_token(data)
49
+ raw_iv, data = Base64.urlsafe_decode64(data).split(DELIMITER, 2)
50
+ JSON.parse(decrypt(data, RubyMagicLink.config.secret_key, Base64.urlsafe_decode64(raw_iv)))
51
+ end
52
+
53
+ def encrypt(data, key, iv)
54
+ cipher = OpenSSL::Cipher.new(ALGORITHM)
55
+ cipher.encrypt
56
+ cipher.key = key
57
+ cipher.iv = iv
58
+ cipher.update(data) + cipher.final
59
+ end
60
+
61
+ def decrypt(encrypted_data, key, iv)
62
+ cipher = OpenSSL::Cipher.new(ALGORITHM)
63
+ cipher.decrypt
64
+ cipher.key = key
65
+ cipher.iv = iv
66
+ cipher.update(encrypted_data) + cipher.final
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyMagicLink
4
+ VERSION = '0.0.1'
5
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ruby_magic_link/ruby_magic_link'
4
+ require 'ruby_magic_link/token'
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_magic_link
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Igor Korepanov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-02-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Magic links for ruby web applications
14
+ email: noemail@example.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE.txt
20
+ - README.md
21
+ - lib/ruby_magic_link.rb
22
+ - lib/ruby_magic_link/ruby_magic_link.rb
23
+ - lib/ruby_magic_link/token.rb
24
+ - lib/ruby_magic_link/version.rb
25
+ homepage: https://github.com/igorkorepanov/ruby_magic_link
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '2.7'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubygems_version: 3.2.22
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Magic links for ruby web applications
48
+ test_files: []