ruby_magic_link 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 77385bf30e18ca3972349337694148c75264e91da47a21b630a76aaea350e1f8
4
- data.tar.gz: e4fbc8d84e7d998451d38f3d72841936b11026cead7f3600462d570beb8194d0
3
+ metadata.gz: 205f84fd453224203038a00c21e6af97a9854776dc878e0cad7e05f80e3b3045
4
+ data.tar.gz: 6058775a727aac913328f699f650540ae5d4bdc16c280ce1346f9ea959c2a29e
5
5
  SHA512:
6
- metadata.gz: 5e2b691012719785f4d629afd8e7db3d31012947622f3fa03157858ffec3c56dfdde4db04cd15577115c0309ed8b95f66444f8aa0ee3f94a7a95ef62795391be
7
- data.tar.gz: d107925bad7e6f38af7723331bf4baf1aeaebca1104462a91c634a439462ac12cbcf284533ec29fc46c123dc6ab8809644e79f33c7f058048a8452aaffc019ba
6
+ metadata.gz: 4a371c23f1f5dda6c2f155249a4bdc9a54d1abb0abee56f3c680539533f377d1ec8ea7f2bccb29e63cf935adc60e08e76bd90160043190651118d90c4c18ee6a
7
+ data.tar.gz: 643eca2c3d8ffd8c03fc66525899edd2cc9331bfc3813072d225cfcb4e4a4d2888893b382477f336a1a7fea6ad283ab0be81a161dfcc4e9bcfb468a806e26ce5
data/README.md CHANGED
@@ -1,3 +1,6 @@
1
+ [![Gem Version](https://badge.fury.io/rb/ruby_magic_link.svg)](https://badge.fury.io/rb/ruby_magic_link)
2
+ ![CI](https://github.com/igorkorepanov/ruby_magic_link/actions/workflows/main.yml/badge.svg)
3
+
1
4
  # RubyMagicLink
2
5
 
3
6
  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 +28,7 @@ gem install ruby_magic_link
25
28
  ## Usage
26
29
  ### Configuration
27
30
 
28
- For Rails applications, create an initializer file in the `config/initializers directory`.
31
+ For Rails applications, create an initializer file in the `config/initializers` directory.
29
32
 
30
33
  ```ruby
31
34
  # 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,22 @@ 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
+ if expires_in
17
+ raise(StandardError, '`expires_at` must be an Integer') unless expires_in.is_a? Integer
18
+
19
+ data[:expires_in] = Time.now.to_i + expires_in
20
+ end
43
21
  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))
22
+ encrypted_data = encrypt(JSON.generate(data), RubyMagicLink.config.secret_key, iv)
23
+ Base64.urlsafe_encode64(Base64.urlsafe_encode64(iv) + DELIMITER + encrypted_data)
46
24
  end
47
25
 
48
26
  def decode(data)
49
- TokenObject.new(data)
27
+ RubyMagicLink::TokenObject.new(data)
50
28
  end
51
29
 
52
30
  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.2'
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,17 +1,17 @@
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.2
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-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Magic links for ruby web applications
14
- email: noemail@example.com
14
+ email: korepanovigor87@gmail.com
15
15
  executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
@@ -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: