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 +4 -4
- data/README.md +75 -0
- data/lib/ruby_magic_link/tasks.rb +10 -0
- data/lib/ruby_magic_link/token.rb +6 -2
- data/lib/ruby_magic_link/version.rb +1 -1
- data/lib/ruby_magic_link.rb +2 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 77385bf30e18ca3972349337694148c75264e91da47a21b630a76aaea350e1f8
|
4
|
+
data.tar.gz: e4fbc8d84e7d998451d38f3d72841936b11026cead7f3600462d570beb8194d0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
@@ -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 =
|
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
|
|
data/lib/ruby_magic_link.rb
CHANGED
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
|
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-
|
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
|