ruby_magic_link 0.0.1 → 1.0.1

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: b388ac403bc4f6041bc487bcd64d8a7fff1712a1e2691aa5477ab52652f3f5f9
4
- data.tar.gz: fc942055ed95c2ead29a6dae7cd8b9d332714a93fe9216590362d09fdbce3138
3
+ metadata.gz: 265ece5de19cf999679f6006cc8aa2ac2df56284c55e50acc44825d0ffa013fc
4
+ data.tar.gz: bf4848c3854c3fda2fa7858ddc79b7fdb39e36896548a580bc6fa51774d16160
5
5
  SHA512:
6
- metadata.gz: 38d0072644a3b821b222652c96841c4e5321d503e78e980844e6a02498c53ee4d6d1a9b72e4968c6f50f2e8f98bbd332458761c26608354e1a6aa12fafad5e5a
7
- data.tar.gz: e2e316ddc3cb71ae527400393a58285fca52101eb96e4aa322275d561760ba7fbd3ee2e2555415a3e2191bcde73781666a30899591373ec452634f041de8273d
6
+ metadata.gz: 95c41caf541fa90039f2231b994723d4d48ddba1aab9eb26bb4b2f09e0502997cf710d3e777fe89d423f5f572b3c66809c93604a0c66acfdb9bce2b81f91d137
7
+ data.tar.gz: c4eaa959ba5d81dda9db387f53131924f7089c00c58e11b7e402f23d5860bb8c8ad41460552dd393192d3f459264ec16271fb45fd74cc033c1ae0275131ee9b4
data/README.md CHANGED
@@ -1,3 +1,80 @@
1
+ ![CI](https://github.com/igorkorepanov/ruby_magic_link/actions/workflows/main.yml/badge.svg)
2
+
3
+ # RubyMagicLink
4
+
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.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ruby_magic_link'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ```bash
18
+ bundle install
19
+ ```
20
+
21
+ Or install it manually using:
22
+
23
+ ```bash
24
+ gem install ruby_magic_link
25
+ ```
26
+
27
+ ## Usage
28
+ ### Configuration
29
+
30
+ For Rails applications, create an initializer file in the `config/initializers` directory.
31
+
32
+ ```ruby
33
+ # config/initializers/ruby_magic_link.rb
34
+
35
+ RubyMagicLink.setup do |config|
36
+ config.secret_key = 'your_secret_key'
37
+ end
38
+ ```
39
+
40
+ Replace `your_secret_key` with a strong secret key. Keep this key confidential and do not expose it in your public repositories.
41
+
42
+ To generate a secret key, run the following rake task:
43
+
44
+ ```bash
45
+ bundle exec rake ruby_magic_link:generate_key
46
+ ```
47
+
48
+ ### Simple Example
49
+
50
+ Encode your data:
51
+
52
+ ```ruby
53
+ payload = {
54
+ user_id: 123_456,
55
+ action: :authenticate
56
+ }
57
+ encrypted_data = RubyMagicLink::Token.create(payload, expires_in: Time.now.to_i + 3600)
58
+ url = "https://example.com/magic_links?data=#{encrypted_data}"
59
+
60
+ # Send the generated URL in an email or through other communication channels.
61
+ ```
62
+
63
+ Decode your data:
64
+
65
+ ```ruby
66
+ class MagicLinksController < ApplicationController
67
+ def index
68
+ token = RubyMagicLink::Token.decode(params[:data])
69
+ if token.valid? && token.payload['action'] == 'authenticate'
70
+ sign_in(:user, token.payload['user_id'])
71
+ redirect_to home_path
72
+ end
73
+ end
74
+ end
75
+ ```
76
+
77
+
1
78
  ## License
2
79
 
3
80
  Copyright (c) 2024 Igor Korepanov
@@ -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
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rake'
4
+ require 'securerandom'
5
+
6
+ namespace :ruby_magic_link do
7
+ task :generate_key do
8
+ puts SecureRandom.hex(16)
9
+ end
10
+ end
@@ -6,47 +6,25 @@ require 'json'
6
6
 
7
7
  module RubyMagicLink
8
8
  module Token
9
- DELIMITER = '!'
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 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
12
  module_function
35
13
 
36
14
  def create(payload, expires_in: nil)
37
15
  data = { payload: payload }
38
- data[:expires_in] = expires_in if expires_in
16
+ data[:expires_in] = Time.now.to_i + expires_in if expires_in
39
17
  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))
18
+ encrypted_data = encrypt(JSON.generate(data), RubyMagicLink.config.secret_key, iv)
19
+ Base64.urlsafe_encode64(Base64.urlsafe_encode64(iv) + DELIMITER + encrypted_data)
42
20
  end
43
21
 
44
22
  def decode(data)
45
- TokenObject.new(data)
23
+ RubyMagicLink::TokenObject.new(data)
46
24
  end
47
25
 
48
26
  def decode_token(data)
49
- raw_iv, data = Base64.urlsafe_decode64(data).split(DELIMITER, 2)
27
+ raw_iv, data = Base64.urlsafe_decode64(data).split(DELIMITER, 2)
50
28
  JSON.parse(decrypt(data, RubyMagicLink.config.secret_key, Base64.urlsafe_decode64(raw_iv)))
51
29
  end
52
30
 
@@ -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 = '0.0.1'
4
+ VERSION = '1.0.1'
5
5
  end
@@ -1,4 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'ruby_magic_link/ruby_magic_link'
4
- require 'ruby_magic_link/token'
4
+ require 'ruby_magic_link/token'
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: 0.0.1
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-04 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
@@ -20,7 +20,9 @@ files:
20
20
  - README.md
21
21
  - lib/ruby_magic_link.rb
22
22
  - lib/ruby_magic_link/ruby_magic_link.rb
23
+ - lib/ruby_magic_link/tasks.rb
23
24
  - lib/ruby_magic_link/token.rb
25
+ - lib/ruby_magic_link/token_object.rb
24
26
  - lib/ruby_magic_link/version.rb
25
27
  homepage: https://github.com/igorkorepanov/ruby_magic_link
26
28
  licenses: