qr-to-otp 0.0.2 → 0.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: abbdcd6e1fcb18743ae93e712557c843dffad5babcd7ddb33839c28762cffcc9
4
- data.tar.gz: 00cddc816e064f25c885a241a933756450bab61b179bc9033af568b26a6b9141
3
+ metadata.gz: f99a898c604164374410017f05046fb317ab766e4d2c1718b88fa64146a016bd
4
+ data.tar.gz: 2aa11231831843a908b79ea968ee75438771af773569ad92a636a51537fdf39b
5
5
  SHA512:
6
- metadata.gz: a03af0f69f2bf557ecd62bfa3678d03f2fbcc6353daccc6be5cd7e9fd47b304a3e1c36a90207f8f6ca3118e0770e75902de47da86d67046945264c8e767569aa
7
- data.tar.gz: e3a9a92f0b057fb78bb2073ee715c0133fcb47673619d88789057f143b1e1c5f7bb1de6bc43228c6986a3aa2559ecc0d692c04b429e91bc17c8fa67039e8e70b
6
+ metadata.gz: 9062b38367c4cf5af6a841e660a6387bc10b3dc4ba4bfc34a63ede76c199dab15a56a758a8b67e30a33891951374fe2650e12093820dcb1f566594b5f6d665c1
7
+ data.tar.gz: 0d718e55032407305f4e93ccd2bb14202cca45ec4e837eacd65e1c8a1c73e24a53c4f35739a2d704698d01999f00874818a0464b9af7e117421a66a2e9f43fd7
@@ -1,5 +1,4 @@
1
1
  require 'openssl'
2
- require 'mac-address'
3
2
  require 'base32'
4
3
  require 'base64'
5
4
  require 'qrio'
@@ -1,107 +1,109 @@
1
1
  module QrToOtp
2
-
2
+
3
3
  class OTP
4
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
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
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
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
+ hash_mac_code = (data[0] << 24) +
45
+ (data[1] << 16) +
46
+ (data[2] << 8) +
47
+ data[3]
48
+ return (hash_mac_code % 10 ** @digits_length).to_s.rjust(@digits_length, '0')
49
+ else
50
+ return 'FAILURE'
51
+ end
52
+ end
33
53
 
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
54
  def auth_at(time)
55
55
  if time.class != Time
56
- time = Time.at(time.to_i)
57
- end
58
- get(unix_time(time))
56
+ time = Time.at(time.to_i)
57
+ end
58
+ get(unix_time(time))
59
59
  end
60
-
61
- private
60
+
61
+ private
62
62
 
63
63
  def unix_time(time = Time.now)
64
- (time.utc).to_i / @refresh_period
64
+ (time.utc).to_i / @refresh_period
65
65
  end
66
66
 
67
67
  def image_type()
68
68
  File.extname(@qr_image) == '.png' ? true : false
69
69
  end
70
70
 
71
+ #QR code to qr image conversion
71
72
  def qr_code_to_qr_image()
72
73
  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
74
+ image = "/tmp/image_#{Time.now.to_i}.png"
75
+ File.open("#{image}", 'wb') do |f|
76
+ f.write(Base64.decode64(@qr_image))
77
+ end
78
+ @qr_image = image
79
+ end
79
80
  end
80
81
 
82
+ #Convert QR image to otpauth URL
81
83
  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
84
+ qr_code_to_qr_image()
85
+ otpauth = Hash.new("")
86
+ otpauth_url = Qrio::Qr.load("#{@qr_image}").qr.text
87
+ base_name = File.basename("#{@qr_image}")
88
+ File.delete("/tmp/#{base_name}") if File.exist?("/tmp/#{base_name}")
89
+ otpauth[:url] = otpauth_url
90
+ otpauth[:device_id] = ''
91
+ otpauth[:secret] = otpauth_url[/secret=(.*?)&issuer/m, 1]
92
+ otpauth[:issuer] = otpauth_url[/issuer=(.*)/m, 1]
93
+ device_id = otpauth_url[/totp\/(.*)?/m, 1].split(':')
94
+ if device_id.length == 2
95
+ otpauth[:device_id] = device_id[1].split('?')[0]
96
+ end
97
+ return otpauth
96
98
  end
97
99
 
98
100
  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)
101
+ data_byte = []
102
+ until time_val == 0
103
+ data_byte << (0xFF & time_val).chr
104
+ time_val >>= 8
105
+ end
106
+ data_byte.reverse.join.rjust(@padding_length, 0.chr)
105
107
  end
106
108
  end
107
109
  end
@@ -1,3 +1,3 @@
1
1
  module QrToOtp
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qr-to-otp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tushar Metkar
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-17 00:00:00.000000000 Z
11
+ date: 2018-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -16,84 +16,84 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '10.0'
19
+ version: 12.3.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '10.0'
26
+ version: 12.3.1
27
27
  - !ruby/object:Gem::Dependency
28
- name: rspec
28
+ name: base32
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '3.0'
33
+ version: 0.3.2
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '3.0'
40
+ version: 0.3.2
41
41
  - !ruby/object:Gem::Dependency
42
- name: mac-address
42
+ name: qrio
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 1.6.3
47
+ version: 0.0.1
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 1.6.3
54
+ version: 0.0.1
55
55
  - !ruby/object:Gem::Dependency
56
- name: bundler
56
+ name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '1.12'
62
- type: :runtime
61
+ version: '3.0'
62
+ type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '1.12'
68
+ version: '3.0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: base32
70
+ name: macaddr
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 0.3.2
76
- type: :runtime
75
+ version: 1.7.0
76
+ type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 0.3.2
82
+ version: 1.7.0
83
83
  - !ruby/object:Gem::Dependency
84
- name: qrio
84
+ name: bundler
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: 0.0.1
90
- type: :runtime
89
+ version: '1.12'
90
+ type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: 0.0.1
96
+ version: '1.12'
97
97
  description: It converts base64 image or QR image to time based Google Authenticator
98
98
  one time password
99
99
  email: