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