expirable-token 0.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 +7 -0
- data/lib/expirable_token.rb +55 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b6cef15be08faa34351c8c1b38d7e1b6fae496fc
|
4
|
+
data.tar.gz: 86795168e861621103fd7c3072885acdb945bc9e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8ec66acb5de8b3a45bcbb67df9f3c8cbd43cdc06e09d25bd067d300fbc0a212159e4bc371558f1f7f3775d67ede4caa12568df989d94169502f101351f6fd297
|
7
|
+
data.tar.gz: 28d0aec6fe826c150b8c1c7ca2427fdc6b831c5d37180b9e55e373f71a2ff5affd2b60a6b6d4288f18e2f266699373c080eb484300049a8ffaa08ce2deed3eb7
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'time'
|
2
|
+
require 'json'
|
3
|
+
require 'base64'
|
4
|
+
|
5
|
+
class ExpirableToken
|
6
|
+
|
7
|
+
attr_reader :extra
|
8
|
+
attr_reader :id
|
9
|
+
|
10
|
+
def initialize(id, extra, expire_on = nil)
|
11
|
+
@id = id
|
12
|
+
@extra = extra
|
13
|
+
@created_on = Time.now.to_i
|
14
|
+
if expire_on.nil?
|
15
|
+
@expire_on = @created_on + 86400
|
16
|
+
end
|
17
|
+
@diff = @expire_on - @created_on
|
18
|
+
end
|
19
|
+
|
20
|
+
def is_valid
|
21
|
+
today = Time.now.to_i
|
22
|
+
!@id.nil? && (@diff = (@expire_on - @created_on)) && (@expire_on > today)
|
23
|
+
end
|
24
|
+
|
25
|
+
def encode
|
26
|
+
Base64.encode64(
|
27
|
+
JSON.generate(
|
28
|
+
{
|
29
|
+
0 => @id,
|
30
|
+
1 => @extra,
|
31
|
+
2 => @diff,
|
32
|
+
3 => @created_on,
|
33
|
+
4 => @expire_on
|
34
|
+
}
|
35
|
+
)
|
36
|
+
).chomp.chomp('=').chomp('=')
|
37
|
+
end
|
38
|
+
|
39
|
+
def decode(token)
|
40
|
+
a = JSON.parse(Base64.decode64(token+"==\n"))
|
41
|
+
@id = a['0']
|
42
|
+
@extra = a['1']
|
43
|
+
@diff = a['2']
|
44
|
+
@created_on = a['3']
|
45
|
+
@expire_on = a['4']
|
46
|
+
is_valid
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.from_token(token)
|
50
|
+
instance = ExpirableToken.new(nil, nil)
|
51
|
+
instance.decode(token)
|
52
|
+
return instance
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: expirable-token
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joseluis Laso
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-22 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A generic class to convert simple information in a hard to understand
|
14
|
+
hash that can be used in a URL for instance
|
15
|
+
email: jlaso@joseluislaso.es
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/expirable_token.rb
|
21
|
+
homepage: https://rubygems.org/gems/expirable-token
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 2.6.8
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: A useful class to generate tokens auto-expirable
|
45
|
+
test_files: []
|