redis_token 0.0.1 → 0.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 +4 -4
- data/README.md +73 -6
- data/lib/redis_token/version.rb +1 -1
- data/lib/redis_token.rb +50 -7
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 10430bc88bb8202bf46a6d2e299dde3b96d3c4c7
|
4
|
+
data.tar.gz: 760cc37ab113e0a0e8470b68e17b121cd3460212
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b6a5430a3860e7b6822f02e9f12f422b5e230af08ccdbca81b08591aaba28cba1a5c0641aaad91991f2b0731d853af8fdc29301723c83ddb052f562acd826107
|
7
|
+
data.tar.gz: cb9b1cfa97a7525a240d74cefd4f19ea162a86b7a522a45d8dde25506178359addf862582446b74befccd49a1dba1dc3e02f16d2fdb7e6cfec76c8bef9b79a42
|
data/README.md
CHANGED
@@ -21,20 +21,19 @@ Or install it yourself as:
|
|
21
21
|
## Usage
|
22
22
|
|
23
23
|
```ruby
|
24
|
-
|
25
24
|
before_action :create_service
|
26
25
|
|
27
26
|
def auth
|
28
27
|
client = Client.find_by_email(params[:email])
|
29
28
|
|
30
29
|
if client.password == params[:password]
|
31
|
-
token = @redis_token.create(client.id, payload: { source: :native })
|
30
|
+
token = @redis_token.create("client.#{client.id}", payload: { source: :native })
|
32
31
|
json(access_token: token)
|
33
32
|
|
34
|
-
...
|
33
|
+
# ...
|
35
34
|
end
|
36
35
|
|
37
|
-
...
|
36
|
+
# ...
|
38
37
|
end
|
39
38
|
|
40
39
|
def secured_method
|
@@ -44,13 +43,13 @@ def secured_method
|
|
44
43
|
client = Client.find_by_id(value[:owner])
|
45
44
|
payload = value[:payload]
|
46
45
|
|
47
|
-
...
|
46
|
+
# ...
|
48
47
|
end
|
49
48
|
|
50
49
|
def client_tokens
|
51
50
|
@tokens = []
|
52
51
|
|
53
|
-
@redis_token.each(client.id) do |token, value|
|
52
|
+
@redis_token.each("client.#{client.id}") do |token, value|
|
54
53
|
@tokens << { token: token, value: value }
|
55
54
|
end
|
56
55
|
end
|
@@ -61,3 +60,71 @@ def create_service
|
|
61
60
|
@redis_token ||= RedisToken.new(prefix: 'myproject.tokens.', ttl: 30.days)
|
62
61
|
end
|
63
62
|
```
|
63
|
+
|
64
|
+
### RedisToken creation
|
65
|
+
|
66
|
+
Implicit Redis instance creation:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
# Redis.new with no arguments:
|
70
|
+
r = RedisToken.new(prefix: 'myproject.tokens.', ttl: 30.days)
|
71
|
+
|
72
|
+
# Redis.new(host: '192.168.1.1', port: 33421)
|
73
|
+
r = RedisToken.new(prefix: 'myproject.tokens.', host: '192.168.1.1', port: 33421)
|
74
|
+
```
|
75
|
+
|
76
|
+
Explicit Redis instance injection:
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
redis = Redis.new
|
80
|
+
r = RedisToken.new(redis, prefix: 'myproject.tokens.', ttl: 30.days)
|
81
|
+
r = RedisToken.new(redis, prefix: 'myproject2.tokens.')
|
82
|
+
```
|
83
|
+
|
84
|
+
### Create token
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
client_token = r.create("client.#{client.id}")
|
88
|
+
# => "eca431add3b1f0bbc6cfc73980b68708"
|
89
|
+
|
90
|
+
# Redefine default ttl:
|
91
|
+
user_token = r.create("u:#{current_user.id}", ttl: 15.hours)
|
92
|
+
# => "548a5d54eaf474c750bf83ed04bd242a"
|
93
|
+
|
94
|
+
# Create token with payload:
|
95
|
+
mobile_token = r.create("c.#{client.id}", payload: { source: :native })
|
96
|
+
# => "865249d6b87c4e6dd8f6b0796ace7fa0"
|
97
|
+
```
|
98
|
+
|
99
|
+
### Get token
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
value = r.get('865249d6b87c4e6dd8f6b0796ace7fa0')
|
103
|
+
# => {:owner=>"c.555", :at=>2017-08-18 15:53:15 +0300, :payload=>{:source=>:native}}
|
104
|
+
|
105
|
+
# Each get request slides ttl by default:
|
106
|
+
r.ttl('865249d6b87c4e6dd8f6b0796ace7fa0')
|
107
|
+
# => 1209493
|
108
|
+
r.get('865249d6b87c4e6dd8f6b0796ace7fa0')
|
109
|
+
# => {:owner=>"c.555", :at=>2017-08-18 15:53:15 +0300, :payload=>{:source=>:native}}
|
110
|
+
r.ttl('865249d6b87c4e6dd8f6b0796ace7fa0')
|
111
|
+
# => 1209598
|
112
|
+
|
113
|
+
# To prevent ttl sliding set slide_expire to false:
|
114
|
+
r.get('865249d6b87c4e6dd8f6b0796ace7fa0', slide_expire: false)
|
115
|
+
```
|
116
|
+
|
117
|
+
### Get all tokens owned by someone
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
5.times { r.create('u.555') }
|
121
|
+
|
122
|
+
r.each('u.555') { |t,v| p "#{t}: #{v}" }
|
123
|
+
# "5e8c661c11955b062d8c512c201734dd: {:owner=>\"u.555\", :at=>2017-08-18 16:02:10 +0300}"
|
124
|
+
# "5607a698763a6164975b9ffc06528513: {:owner=>\"u.555\", :at=>2017-08-18 16:02:10 +0300}"
|
125
|
+
# "7ef74cb761c1595dd33058e78a125720: {:owner=>\"u.555\", :at=>2017-08-18 16:02:10 +0300}"
|
126
|
+
# "621d2a10c34e92d7f0b4fc3b00be62af: {:owner=>\"u.555\", :at=>2017-08-18 16:02:10 +0300}"
|
127
|
+
# "17f7cb676e67d6b53c48e51f1c1beeb1: {:owner=>\"u.555\", :at=>2017-08-18 16:02:10 +0300}"
|
128
|
+
# => nil
|
129
|
+
|
130
|
+
```
|
data/lib/redis_token/version.rb
CHANGED
data/lib/redis_token.rb
CHANGED
@@ -13,6 +13,20 @@ class RedisToken
|
|
13
13
|
attr_accessor :prefix
|
14
14
|
attr_reader :created_value
|
15
15
|
|
16
|
+
# Create RedisToken instance
|
17
|
+
#
|
18
|
+
# Implicit redis instance creation (redis parameters can be passed in args):
|
19
|
+
# RedisToken.new(ttl: 5.days, prefix: 'project.tokens.', host: '127.0.0.1')
|
20
|
+
#
|
21
|
+
# Explicit redis instance injection:
|
22
|
+
# redis = Redis.new(host: '192.168.0.1', port: 33221)
|
23
|
+
# RedisToken.new(redis, ttl: 5.days, prefix: 'project.tokens.')
|
24
|
+
#
|
25
|
+
# @param [Hash] args
|
26
|
+
# @option args [String] :prefix redis keys prefix (e.g. 'myproject.tokens.')
|
27
|
+
# @option args [Integer] :ttl token time to live value (14 days by default)
|
28
|
+
#
|
29
|
+
# @return [RedisToken] a new RedisToken instance
|
16
30
|
def initialize(args = {}, opts = {})
|
17
31
|
@redis = if args.nil? || args.is_a?(Hash)
|
18
32
|
init_params(args)
|
@@ -21,10 +35,16 @@ class RedisToken
|
|
21
35
|
init_params(opts)
|
22
36
|
args
|
23
37
|
end
|
24
|
-
|
25
|
-
@default_ttl ||= DEFAULT_TTL
|
26
38
|
end
|
27
39
|
|
40
|
+
# Create a new token
|
41
|
+
#
|
42
|
+
# @param [String] owner owner of a token, e.g. 'client.1' or 'user-123'
|
43
|
+
# @param [Hash] args
|
44
|
+
# @option args :payload
|
45
|
+
# @option args [Integer] :ttl redefines the default ttl
|
46
|
+
#
|
47
|
+
# @return [String] a new token
|
28
48
|
def create(owner, args = {})
|
29
49
|
raise 'owner should be specified' unless owner
|
30
50
|
|
@@ -45,6 +65,14 @@ class RedisToken
|
|
45
65
|
token
|
46
66
|
end
|
47
67
|
|
68
|
+
# Get value of a token and slide ttl
|
69
|
+
#
|
70
|
+
# @param [String] token
|
71
|
+
# @param [Hash] args
|
72
|
+
# @option args [Integer] :ttl
|
73
|
+
# @option args [Boolean] :slide_expire (true) slide ttl of a token
|
74
|
+
#
|
75
|
+
# @return [Hash] value of a token
|
48
76
|
def get(token, args = {})
|
49
77
|
key = token_to_key(token)
|
50
78
|
value = redis_get(key)
|
@@ -61,6 +89,14 @@ class RedisToken
|
|
61
89
|
value
|
62
90
|
end
|
63
91
|
|
92
|
+
# Set new payload of a token
|
93
|
+
#
|
94
|
+
# @param [String] token
|
95
|
+
# @param [Hash] args
|
96
|
+
# @option args [Integer] :ttl set new time to live value
|
97
|
+
# @option args :payload new payload value
|
98
|
+
#
|
99
|
+
# @return [Boolean]
|
64
100
|
def set(token, args = {})
|
65
101
|
key = token_to_key(token)
|
66
102
|
value = redis_get(key)
|
@@ -77,6 +113,9 @@ class RedisToken
|
|
77
113
|
true
|
78
114
|
end
|
79
115
|
|
116
|
+
# Iterate all exist tokens of an owner
|
117
|
+
#
|
118
|
+
# @param [String] owner
|
80
119
|
def each(owner)
|
81
120
|
mask = "#{@prefix}#{owner}.*"
|
82
121
|
|
@@ -94,6 +133,11 @@ class RedisToken
|
|
94
133
|
end
|
95
134
|
end
|
96
135
|
|
136
|
+
# Delete a token
|
137
|
+
#
|
138
|
+
# @param [String] token
|
139
|
+
#
|
140
|
+
# @return [Boolean]
|
97
141
|
def del(token)
|
98
142
|
key = token_to_key(token)
|
99
143
|
value = redis_get(key)
|
@@ -107,6 +151,9 @@ class RedisToken
|
|
107
151
|
true
|
108
152
|
end
|
109
153
|
|
154
|
+
# Retrieve the remaining ttl of a token
|
155
|
+
#
|
156
|
+
# @return [Integer] ttl
|
110
157
|
def ttl(token)
|
111
158
|
@redis.ttl(token_to_key(token))
|
112
159
|
end
|
@@ -118,7 +165,7 @@ class RedisToken
|
|
118
165
|
end
|
119
166
|
|
120
167
|
def init_params(args)
|
121
|
-
@default_ttl = args[:ttl]
|
168
|
+
@default_ttl = args[:ttl] || DEFAULT_TTL
|
122
169
|
@prefix = args[:prefix]
|
123
170
|
end
|
124
171
|
|
@@ -139,8 +186,4 @@ class RedisToken
|
|
139
186
|
return unless value
|
140
187
|
Marshal.load(value)
|
141
188
|
end
|
142
|
-
|
143
|
-
def check_owner(owner)
|
144
|
-
raise 'owner should be specified' unless owner
|
145
|
-
end
|
146
189
|
end
|