qr-to-otp 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/qr-to-otp.rb +11 -0
- data/lib/qr-to-otp/otp.rb +107 -0
- data/lib/qr-to-otp/version.rb +3 -0
- metadata +12 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: abbdcd6e1fcb18743ae93e712557c843dffad5babcd7ddb33839c28762cffcc9
|
4
|
+
data.tar.gz: 00cddc816e064f25c885a241a933756450bab61b179bc9033af568b26a6b9141
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a03af0f69f2bf557ecd62bfa3678d03f2fbcc6353daccc6be5cd7e9fd47b304a3e1c36a90207f8f6ca3118e0770e75902de47da86d67046945264c8e767569aa
|
7
|
+
data.tar.gz: e3a9a92f0b057fb78bb2073ee715c0133fcb47673619d88789057f143b1e1c5f7bb1de6bc43228c6986a3aa2559ecc0d692c04b429e91bc17c8fa67039e8e70b
|
data/lib/qr-to-otp.rb
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
module QrToOtp
|
2
|
+
|
3
|
+
class OTP
|
4
|
+
|
5
|
+
attr_reader :qr_image, :device_auth_failure, :otpauth
|
6
|
+
REFRESH_PERIOD = 30
|
7
|
+
DIGIT_LENGTH = 6
|
8
|
+
DIGEST_TYPE = "SHA1"
|
9
|
+
PADDING_LENGTH = 8
|
10
|
+
|
11
|
+
# @param [String] image either base64 encoded string or QR image in png format.
|
12
|
+
# @option Various options are accepted as REFRESH_PERIOD, DIGIT_LENGTH, PADDING_LENGTH, DIGEST_TYPE
|
13
|
+
# @returns Auth Code instantiation
|
14
|
+
def initialize(image, options = {})
|
15
|
+
@qr_image = image
|
16
|
+
@refresh_period = options[:refresh_period] || REFRESH_PERIOD
|
17
|
+
@digits_length = options[:digits] || DIGIT_LENGTH
|
18
|
+
@padding_length = options[:padding_length] || PADDING_LENGTH
|
19
|
+
@digest_type = options[:digest_type] || DIGEST_TYPE
|
20
|
+
@image_path =
|
21
|
+
@device_auth_failure = false
|
22
|
+
@device_identity = nil
|
23
|
+
@otpauth = qr_image_to_otpauth()
|
24
|
+
if options[:device_identity]
|
25
|
+
@device_identity = options[:device_identity]
|
26
|
+
if @otpauth[:device_id] != @device_identity
|
27
|
+
@device_auth_failure = true
|
28
|
+
@otpauth = {}
|
29
|
+
return "FAILURE"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def get(time = unix_time())
|
35
|
+
if (!@device_identity) or (@device_identity and !@device_auth_failure)
|
36
|
+
hash_mac = OpenSSL::HMAC.digest(
|
37
|
+
OpenSSL::Digest.new(@digest_type),
|
38
|
+
Base32.decode(@otpauth[:secret]),
|
39
|
+
time_to_string(time)
|
40
|
+
)
|
41
|
+
data_offset = hash_mac.bytes.last & 0xf
|
42
|
+
data = hash_mac.bytes[data_offset..data_offset + 3]
|
43
|
+
data[0] = data[0] & 0x7f
|
44
|
+
has_mac_code = (data[0] << 24) +
|
45
|
+
(data[1] << 16) +
|
46
|
+
(data[2] << 8) +
|
47
|
+
data[3]
|
48
|
+
return (has_mac_code % 10 ** @digits_length).to_s.rjust(@digits_length, '0')
|
49
|
+
else
|
50
|
+
return 'FAILURE'
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def auth_at(time)
|
55
|
+
if time.class != Time
|
56
|
+
time = Time.at(time.to_i)
|
57
|
+
end
|
58
|
+
get(unix_time(time))
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def unix_time(time = Time.now)
|
64
|
+
(time.utc).to_i / @refresh_period
|
65
|
+
end
|
66
|
+
|
67
|
+
def image_type()
|
68
|
+
File.extname(@qr_image) == '.png' ? true : false
|
69
|
+
end
|
70
|
+
|
71
|
+
def qr_code_to_qr_image()
|
72
|
+
if !image_type
|
73
|
+
image = "/tmp/image_#{Time.now.to_i}.png"
|
74
|
+
File.open("#{image}", 'wb') do |f|
|
75
|
+
f.write(Base64.decode64(@qr_image))
|
76
|
+
end
|
77
|
+
@qr_image = image
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def qr_image_to_otpauth()
|
82
|
+
qr_code_to_qr_image()
|
83
|
+
otpauth = Hash.new("")
|
84
|
+
otpauth_url = Qrio::Qr.load("#{@qr_image}").qr.text
|
85
|
+
base_name = File.basename("#{@qr_image}")
|
86
|
+
File.delete("/tmp/#{base_name}") if File.exist?("/tmp/#{base_name}")
|
87
|
+
otpauth[:url] = otpauth_url
|
88
|
+
otpauth[:device_id] = ''
|
89
|
+
otpauth[:secret] = otpauth_url[/secret=(.*?)&issuer/m, 1]
|
90
|
+
otpauth[:issuer] = otpauth_url[/issuer=(.*)/m, 1]
|
91
|
+
device_id = otpauth_url[/totp\/(.*)?/m, 1].split(':')
|
92
|
+
if device_id.length == 2
|
93
|
+
otpauth[:device_id] = device_id[1].split('?')[0]
|
94
|
+
end
|
95
|
+
return otpauth
|
96
|
+
end
|
97
|
+
|
98
|
+
def time_to_string(time_val)
|
99
|
+
data_byte = []
|
100
|
+
until time_val == 0
|
101
|
+
data_byte << (0xFF & time_val).chr
|
102
|
+
time_val >>= 8
|
103
|
+
end
|
104
|
+
data_byte.reverse.join.rjust(@padding_length, 0.chr)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: qr-to-otp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tushar Metkar
|
@@ -17,7 +17,7 @@ dependencies:
|
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '10.0'
|
20
|
-
type: :
|
20
|
+
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
@@ -31,7 +31,7 @@ dependencies:
|
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '3.0'
|
34
|
-
type: :
|
34
|
+
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
@@ -45,7 +45,7 @@ dependencies:
|
|
45
45
|
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: 1.6.3
|
48
|
-
type: :
|
48
|
+
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
@@ -59,7 +59,7 @@ dependencies:
|
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '1.12'
|
62
|
-
type: :
|
62
|
+
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
@@ -73,7 +73,7 @@ dependencies:
|
|
73
73
|
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: 0.3.2
|
76
|
-
type: :
|
76
|
+
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
@@ -87,7 +87,7 @@ dependencies:
|
|
87
87
|
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: 0.0.1
|
90
|
-
type: :
|
90
|
+
type: :runtime
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
@@ -101,7 +101,10 @@ email:
|
|
101
101
|
executables: []
|
102
102
|
extensions: []
|
103
103
|
extra_rdoc_files: []
|
104
|
-
files:
|
104
|
+
files:
|
105
|
+
- lib/qr-to-otp.rb
|
106
|
+
- lib/qr-to-otp/otp.rb
|
107
|
+
- lib/qr-to-otp/version.rb
|
105
108
|
homepage: https://github.com/tusharmetkar/qr-to-otp.git
|
106
109
|
licenses: []
|
107
110
|
metadata: {}
|
@@ -121,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
121
124
|
version: '0'
|
122
125
|
requirements: []
|
123
126
|
rubyforge_project:
|
124
|
-
rubygems_version: 2.
|
127
|
+
rubygems_version: 2.7.7
|
125
128
|
signing_key:
|
126
129
|
specification_version: 4
|
127
130
|
summary: Ruby library for QR image to otp conversion
|