ruby_magic_link 1.0.0 → 1.0.1

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: 77385bf30e18ca3972349337694148c75264e91da47a21b630a76aaea350e1f8
4
- data.tar.gz: e4fbc8d84e7d998451d38f3d72841936b11026cead7f3600462d570beb8194d0
3
+ metadata.gz: 265ece5de19cf999679f6006cc8aa2ac2df56284c55e50acc44825d0ffa013fc
4
+ data.tar.gz: bf4848c3854c3fda2fa7858ddc79b7fdb39e36896548a580bc6fa51774d16160
5
5
  SHA512:
6
- metadata.gz: 5e2b691012719785f4d629afd8e7db3d31012947622f3fa03157858ffec3c56dfdde4db04cd15577115c0309ed8b95f66444f8aa0ee3f94a7a95ef62795391be
7
- data.tar.gz: d107925bad7e6f38af7723331bf4baf1aeaebca1104462a91c634a439462ac12cbcf284533ec29fc46c123dc6ab8809644e79f33c7f058048a8452aaffc019ba
6
+ metadata.gz: 95c41caf541fa90039f2231b994723d4d48ddba1aab9eb26bb4b2f09e0502997cf710d3e777fe89d423f5f572b3c66809c93604a0c66acfdb9bce2b81f91d137
7
+ data.tar.gz: c4eaa959ba5d81dda9db387f53131924f7089c00c58e11b7e402f23d5860bb8c8ad41460552dd393192d3f459264ec16271fb45fd74cc033c1ae0275131ee9b4
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ ![CI](https://github.com/igorkorepanov/ruby_magic_link/actions/workflows/main.yml/badge.svg)
2
+
1
3
  # RubyMagicLink
2
4
 
3
5
  RubyMagicLink: A gem crafted for the secure generation of tokens, ensuring the safe transmission of data within your Ruby application. Useful for creating magic links—single-use URLs empowering users to perform actions without a password.
@@ -25,7 +27,7 @@ gem install ruby_magic_link
25
27
  ## Usage
26
28
  ### Configuration
27
29
 
28
- For Rails applications, create an initializer file in the `config/initializers directory`.
30
+ For Rails applications, create an initializer file in the `config/initializers` directory.
29
31
 
30
32
  ```ruby
31
33
  # config/initializers/ruby_magic_link.rb
@@ -2,7 +2,7 @@
2
2
 
3
3
  module RubyMagicLink
4
4
  class Configuration
5
- attr_accessor :secret_key, :token_expiration
5
+ attr_accessor :secret_key
6
6
  end
7
7
 
8
8
  class << self
@@ -7,4 +7,4 @@ namespace :ruby_magic_link do
7
7
  task :generate_key do
8
8
  puts SecureRandom.hex(16)
9
9
  end
10
- end
10
+ end
@@ -9,44 +9,18 @@ module RubyMagicLink
9
9
  DELIMITER = '|'
10
10
  ALGORITHM = 'AES-256-CBC'
11
11
 
12
- class TokenObject
13
- def initialize(data)
14
- @data = data
15
- end
16
-
17
- def valid?
18
- !expired?
19
- end
20
-
21
- def expired?
22
- decoded_data['expires_in'] < Time.now.to_i
23
- end
24
-
25
- def payload
26
- decoded_data['payload']
27
- end
28
-
29
- private
30
-
31
- def decoded_data
32
- @decoded_data ||= RubyMagicLink::Token.decode_token(data)
33
- end
34
-
35
- attr_reader :data
36
- end
37
-
38
12
  module_function
39
13
 
40
14
  def create(payload, expires_in: nil)
41
15
  data = { payload: payload }
42
- data[:expires_in] = expires_in if expires_in
16
+ data[:expires_in] = Time.now.to_i + expires_in if expires_in
43
17
  iv = OpenSSL::Random.random_bytes(16)
44
- str = JSON.generate(data)
45
- Base64.urlsafe_encode64(Base64.urlsafe_encode64(iv) + DELIMITER + encrypt(str, RubyMagicLink.config.secret_key, iv))
18
+ encrypted_data = encrypt(JSON.generate(data), RubyMagicLink.config.secret_key, iv)
19
+ Base64.urlsafe_encode64(Base64.urlsafe_encode64(iv) + DELIMITER + encrypted_data)
46
20
  end
47
21
 
48
22
  def decode(data)
49
- TokenObject.new(data)
23
+ RubyMagicLink::TokenObject.new(data)
50
24
  end
51
25
 
52
26
  def decode_token(data)
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyMagicLink
4
+ class TokenObject
5
+ def initialize(data)
6
+ @data = data
7
+ end
8
+
9
+ def valid?
10
+ !expired?
11
+ end
12
+
13
+ def expired?
14
+ if decoded_data['expires_in']
15
+ decoded_data['expires_in'] < Time.now.to_i
16
+ else
17
+ false
18
+ end
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
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RubyMagicLink
4
- VERSION = '1.0.0'
4
+ VERSION = '1.0.1'
5
5
  end
@@ -2,4 +2,5 @@
2
2
 
3
3
  require 'ruby_magic_link/ruby_magic_link'
4
4
  require 'ruby_magic_link/token'
5
- require 'ruby_magic_link/tasks'
5
+ require 'ruby_magic_link/token_object'
6
+ require 'ruby_magic_link/tasks'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_magic_link
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Korepanov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-02-05 00:00:00.000000000 Z
11
+ date: 2024-02-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Magic links for ruby web applications
14
14
  email: noemail@example.com
@@ -22,6 +22,7 @@ files:
22
22
  - lib/ruby_magic_link/ruby_magic_link.rb
23
23
  - lib/ruby_magic_link/tasks.rb
24
24
  - lib/ruby_magic_link/token.rb
25
+ - lib/ruby_magic_link/token_object.rb
25
26
  - lib/ruby_magic_link/version.rb
26
27
  homepage: https://github.com/igorkorepanov/ruby_magic_link
27
28
  licenses: