google_authenticator_auth 1.0.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.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,25 @@
1
+ A simple class to work with Google Authenticator from ruby. Can
2
+ generate a secret key along with qrcode url/image and generate
3
+ one time passwords for a secret key.
4
+
5
+ See Also: http://code.google.com/p/google-authenticator/
6
+
7
+
8
+ Generating the Secret Key
9
+ ----------------------------------------------------
10
+ ga = GoogleAuthenticator.new
11
+ ga.secret_key # => "NINWS2QUIQD2LA2Z"
12
+ ga.qrcode_url('user@domain.com') # => "otpauth://totp/user@domain.com?secret=NINWS2QUIQD2LA2Z"
13
+ ga.qrcode_image_url('user@domain.com') # => "https://chart.googleapis.com/chart?chs=350x350&cht=qr&choe=UTF-8&chl=otpauth://totp/user@domain.com?secret=NINWS2QUIQD2LA2Z"
14
+
15
+
16
+ # Verifying a Key
17
+ ----------------------------------------------------
18
+ ga = GoogleAuthenticator.new('NINWS2QUIQD2LA2Z')
19
+ ga.key_valid?(key) # => true or false
20
+
21
+
22
+ # Generate Your Own Keys
23
+ ---------------------------------------------------
24
+ ga = GoogleAuthenticator.new('NINWS2QUIQD2LA2Z')
25
+ ga.get_keys # => [Previous, Current, Next]
@@ -0,0 +1,37 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'rake/gempackagetask'
5
+
6
+ require 'bundler'
7
+ Bundler::GemHelper.install_tasks
8
+
9
+ task :default => :test
10
+
11
+ desc 'Generate documentation for the google_authenticator_auth plugin.'
12
+ Rake::RDocTask.new(:rdoc) do |rdoc|
13
+ rdoc.rdoc_dir = 'rdoc'
14
+ rdoc.title = 'google_authenticator_auth'
15
+ rdoc.options << '--line-numbers' << '--inline-source'
16
+ rdoc.rdoc_files.include('README')
17
+ rdoc.rdoc_files.include('lib/**/*.rb')
18
+ end
19
+
20
+ find_file = lambda do |name|
21
+ file_name = lambda {|path| File.join(path, "#{name}.rb")}
22
+ root = $:.detect do |path|
23
+ File.exist?(file_name[path])
24
+ end
25
+ file_name[root] if root
26
+ end
27
+
28
+ TEST_LOADER = find_file['rake/rake_test_loader']
29
+ multiruby = lambda do |glob|
30
+ system 'multiruby', TEST_LOADER, *Dir.glob(glob)
31
+ end
32
+
33
+ Rake::TestTask.new(:test) do |test|
34
+ test.ruby_opts << "-W"
35
+ test.pattern = 'test/**/*_test.rb'
36
+ test.verbose = true
37
+ end
@@ -0,0 +1,85 @@
1
+ #!/usr/bin/env ruby
2
+ # A simple class to work with Google Authenticator from ruby
3
+ #
4
+ # can generate a secret key along with qrcode url/image and authenticate
5
+ # keys against a secret key
6
+ #
7
+ # See Also: http://code.google.com/p/google-authenticator/
8
+ #
9
+ require 'rubygems'
10
+ require 'base32'
11
+ require 'openssl'
12
+ require 'uri'
13
+
14
+ class GoogleAuthenticator
15
+
16
+ # Load class with the provided secret key. If no key is
17
+ # provided generate a new random secret key
18
+ def initialize(key=nil)
19
+ @secret_key = key.nil? ? GoogleAuthenticator.generate_secret_key : key
20
+ end
21
+
22
+ # Generate a unique secret key
23
+ def self.generate_secret_key
24
+ Base32.encode( (0...10).map{(rand(255)).chr}.join )
25
+ end
26
+
27
+ # Google Charts image URL (resulting image can be scanned by
28
+ # the Google Authenticator app to automaticly import secret key
29
+ def qrcode_image_url(label,wh=350)
30
+ "https://chart.googleapis.com/chart?chs=#{wh}x#{wh}&cht=qr&choe=UTF-8&chl=" + uri_parser.escape(qrcode_url(label))
31
+ end
32
+
33
+ # QRCode URL used to generate a QRCode that can be scanned into
34
+ # Google Authenticator (see qrcode_image_url)
35
+ def qrcode_url(label)
36
+ "otpauth://totp/#{label}?secret=#{@secret_key}"
37
+ end
38
+
39
+ # Current secret key
40
+ def secret_key
41
+ @secret_key
42
+ end
43
+
44
+ # Checks to see if the key is valid for the current secret key
45
+ def key_valid?(key)
46
+ get_keys.include?(key.to_i)
47
+ end
48
+
49
+ # Found at https://gist.github.com/987839
50
+ # Returns an array containing the previous, current, and next
51
+ # valid key for the current secret key
52
+ def get_keys
53
+ keys = []
54
+ int = 30
55
+ now = Time.now.to_i / int
56
+ key = Base32.decode @secret_key
57
+ sha = OpenSSL::Digest::Digest.new('sha1')
58
+
59
+ (-1..1).each do |x|
60
+ bytes = [ now + x ].pack('>q').reverse
61
+ hmac = OpenSSL::HMAC.digest(sha, key.to_s, bytes)
62
+ offset = nil
63
+ if RUBY_VERSION > '1.9'
64
+ offset = hmac[-1].ord & 0x0F
65
+ else
66
+ offset = hmac[-1] & 0x0F
67
+ end
68
+ hash = hmac[offset...offset + 4]
69
+
70
+ code = hash.reverse.unpack('L')[0]
71
+ code &= 0x7FFFFFFF
72
+ code %= 1000000
73
+
74
+ keys << code
75
+ end
76
+
77
+ keys
78
+ end
79
+
80
+ protected
81
+ def uri_parser
82
+ @uri_parser ||= URI.const_defined?(:Parser) ? URI::Parser.new : URI
83
+ end
84
+
85
+ end
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+ require File.expand_path(File.join(File.dirname(__FILE__),'test_helper'))
3
+
4
+ class GoogleAuthenticatorAuthTest < ActiveSupport::TestCase
5
+
6
+ test "should create a random key when started with no paramaters" do
7
+ ga = GoogleAuthenticator.new
8
+ assert_not_nil ga.secret_key
9
+ end
10
+
11
+ test "should use provided secret key" do
12
+ ga = GoogleAuthenticator.new('NINWS2QUIQD2LA2Z')
13
+ assert_equal ga.secret_key, 'NINWS2QUIQD2LA2Z'
14
+ end
15
+
16
+ test "should return three keys" do
17
+ ga = GoogleAuthenticator.new
18
+ assert_equal ga.get_keys.length, 3
19
+ end
20
+
21
+ test "should return a valid qrcode url" do
22
+ ga = GoogleAuthenticator.new('NINWS2QUIQD2LA2Z')
23
+ assert_equal ga.qrcode_image_url("user@domain.com"), "https://chart.googleapis.com/chart?chs=350x350&cht=qr&choe=UTF-8&chl=otpauth://totp/user@domain.com?secret=NINWS2QUIQD2LA2Z"
24
+ assert_equal ga.qrcode_url("user@domain.com"), "otpauth://totp/user@domain.com?secret=NINWS2QUIQD2LA2Z"
25
+ end
26
+
27
+ test "returned keys should be valid" do
28
+ ga = GoogleAuthenticator.new
29
+ ga.get_keys.each do |key|
30
+ assert ga.key_valid?(key)
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,7 @@
1
+
2
+ require 'rubygems'
3
+ gem "test-unit", ">= 2.0.5"
4
+ require 'active_support'
5
+ #require 'active_support/test_case'
6
+ require 'test/unit'
7
+ require File.join(File.dirname(__FILE__),'../lib/google_authenticator_auth.rb')
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_authenticator_auth
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - David Ricciardi
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-07-01 00:00:00 +00:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: builder
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: test-unit
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: activesupport
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :development
62
+ version_requirements: *id003
63
+ description: google authenticator auth
64
+ email: nricciar@gmail.com
65
+ executables: []
66
+
67
+ extensions: []
68
+
69
+ extra_rdoc_files:
70
+ - README
71
+ - MIT-LICENSE
72
+ files:
73
+ - lib/google_authenticator_auth.rb
74
+ - Rakefile
75
+ - README
76
+ - MIT-LICENSE
77
+ - test/google_authenticator_auth_test.rb
78
+ - test/test_helper.rb
79
+ has_rdoc: true
80
+ homepage: http://github.com/nricciar/google_authenticator_auth
81
+ licenses: []
82
+
83
+ post_install_message:
84
+ rdoc_options: []
85
+
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 3
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ hash: 3
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ requirements: []
107
+
108
+ rubyforge_project:
109
+ rubygems_version: 1.3.7
110
+ signing_key:
111
+ specification_version: 3
112
+ summary: A simple class to work with Google Authenticator from ruby
113
+ test_files:
114
+ - test/google_authenticator_auth_test.rb
115
+ - test/test_helper.rb