twofactor 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 ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MjAzZmNmYmRiZWVkZjI1OTEwMTE3OTAzMzM3ODg2ZDZhNmY4ZTQ4ZA==
5
+ data.tar.gz: !binary |-
6
+ OTkyYWI0ZGM0MDU4OWY2OWE1N2Y1MWE4ODVlNjdkYzY3N2Q0ODdhMQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ Y2UxNjE1YjM0ZTlhZjJhYTg0OGEwOTU2NjJjZWZkOGE1MGVmOTJmNzM5ODhk
10
+ M2IwNDc1YmU4NmM5MTBiNTc3MmVhYmU2MjE5MTgxNzUxYjA5M2RhODA1Y2Zj
11
+ NzQwZTQ1NGUxZmE3NmVmMDBlZDllYzcxZTU0NzAwYzgyYjY0NmY=
12
+ data.tar.gz: !binary |-
13
+ OGM0ZjU4ZGY2Y2I0MGI3NTYwYTI5ZjZlMTM2ZWZkNTU4MDQxNjFhODVkZTZk
14
+ NjUyYTg5MDQ4OTEyOWU4N2E3NTIwMmNhNDY3MWRlZTQ2N2ZiNzBjYzdhODc1
15
+ MzYxM2ExMmQzMGM4MGQ2ZTQ1MGM3ODE1MmVhN2FjNGZlZjJjYjg=
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 YOURNAME
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/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Twofactor'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
22
+ require 'rake/testtask'
23
+
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << 'lib'
26
+ t.libs << 'test'
27
+ t.pattern = 'test/**/*_test.rb'
28
+ t.verbose = false
29
+ end
30
+
31
+
32
+ task default: :test
@@ -0,0 +1,63 @@
1
+ module Twofactor
2
+ class InstallGenerator < Rails::Generators::Base
3
+ source_root File.dirname(__FILE__)
4
+
5
+ #First arg : Model Name which needs Two-factor auth
6
+ #Second arg : Reference field that needs to be used(This field will appear in the client's Google Authenticator app. Defaults to 'email')
7
+ #Third arg : Template type to use for twofactor_register default page (takes one of erb / haml / slim)
8
+ #Fourth arg : Controller that could be configured with TwoFactor actions( Defaults to Controller with modelname pluralized )
9
+ #Fifth arg : Table name corresponding to the model. ( Defaults to Rails' choice. )
10
+ def initialize(*runtime_args)
11
+ super(*runtime_args)
12
+ @args = runtime_args
13
+ end
14
+
15
+ class InvalidParameterError < StandardError
16
+ def initialize(msg = "Provide a valid model name & reference_field field.")
17
+ super(msg)
18
+ end
19
+ end
20
+ class InvalidTemplateTypeError < StandardError
21
+ def initialize(msg = "Provide a valid template type. slim (or) haml (or) erb.")
22
+ super(msg)
23
+ end
24
+ end
25
+ def install_twofactor
26
+ args = @args.first
27
+ options = {}
28
+ options[:model_name] = args.first
29
+ options[:reference_field] = args.second
30
+ options[:template_type] = args.third
31
+ options[:controller] = args.fourth
32
+ options[:table_name] = args.fifth
33
+ model = options[:model_name]
34
+ @reference_field = options[:reference_field]
35
+
36
+ if(model.nil? || model.blank? || @reference_field.nil? || @reference_field.blank?)
37
+ raise InvalidParameterError
38
+ else
39
+ @model_name = model.capitalize
40
+ end
41
+
42
+ raise InvalidTemplateTypeError unless ['haml','erb','slim'].include? options[:template_type]
43
+
44
+ controller_name = options[:controller].present? ? options[:controller] : "#{@model_name.pluralize}Controller"
45
+ table_name = options[:table_name].present? ? options[:table_name] : @model_name.tableize
46
+
47
+ template 'templates/twofactor_config.rb', 'config/initializers/twofactor_config.rb'
48
+
49
+ template 'templates/twofactor_migration.rb', File.join('db', 'migrate', "#{Time.now.strftime('%Y%m%d%H%M%S')}_add_twofactor_fields_to_#{table_name}.rb")
50
+ template_location = controller_name == 'ApplicationController' ? 'twofactor' : controller_name.gsub('Controller','').tableize
51
+ copy_file "templates/twofactor_register.html.#{options[:template_type]}", "app/views/#{template_location}/twofactor_register.html.#{options[:template_type]}"
52
+ twofactor_actions = "\n public\n"\
53
+ " def twofactor_register\n"\
54
+ " model_object = (Rails.application.class::TWOFACTOR_MODEL_NAME).constantize.find_by_id(params[:id])\n"\
55
+ " qrcode = Twofactor::TwoStep.enable_twofactor_auth(model_object)\n"\
56
+ " render :template => \"#{template_location}/twofactor_register\", :locals => {:qrcode => qrcode}\n"\
57
+ " end\n\n"
58
+ inject_into_file "app/controllers/#{controller_name.underscore}.rb", twofactor_actions, :before => /^end/
59
+ c_name = controller_name.gsub("Controller","").underscore
60
+ route "get '#{c_name}/twofactor_register/:id' => '#{c_name}#twofactor_register', as: 'twofactor_register'"
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,20 @@
1
+ #Model to be used for twofacter authentication
2
+ <%= "#{Rails.application.class}::TWOFACTOR_MODEL_NAME = '#{@model_name}'" %>
3
+
4
+ #Reference Field to be displayed on authenticator mobile app (Defaults to CL argument provided during gem installation)
5
+ <%= "#{Rails.application.class}::TWOFACTOR_REFERENCE_FIELD = '#{@reference_field}'" %>
6
+
7
+ #Attribute in the model that holds the secret key generated by the gem
8
+ <%= "#{Rails.application.class}::TWOFACTOR_SECRET_ATTRIBUTE = 'twofactor_secret'" %>
9
+
10
+ #Attribute in the model that holds 2factor enabled/disabled flag
11
+ <%= "#{Rails.application.class}::TWOFACTOR_FLAG_ATTRIBUTE = 'twofactor_enabled'" %>
12
+
13
+ #QR-Code size
14
+ <%= "#{Rails.application.class}::TWOFACTOR_QRCODE_SIZE = 6" %>
15
+
16
+ #QR-Code quality
17
+ <%= "#{Rails.application.class}::TWOFACTOR_QRCODE_QUALITY = 'm'" %>
18
+
19
+ #Override if you'd like to replace Your application's Name with your choice (This will appear in user's authenticator app for his reference)
20
+ <%= "#{Rails.application.class}::TWOFACTOR_PROVIDER_NAME = ''" %>
@@ -0,0 +1,6 @@
1
+ class AddTwofactorFieldsTo<%= @model_name.pluralize %> < ActiveRecord::Migration
2
+ def change
3
+ add_column <%= ":#{@model_name.tableize}" %>, :twofactor_enabled, :boolean, :default => false
4
+ add_column <%= ":#{@model_name.tableize}" %>, :twofactor_secret, :string
5
+ end
6
+ end
@@ -0,0 +1,21 @@
1
+ <style type='text/css'>
2
+ table {
3
+ border-width: 0;
4
+ border-style: none;
5
+ border-color: #0000ff;
6
+ border-collapse: collapse;
7
+ }
8
+
9
+ td {
10
+ border-left: solid 10px #000;
11
+ padding: 0;
12
+ margin: 0;
13
+ width: 0px;
14
+ height: 10px;
15
+ }
16
+
17
+ td.black { border-color: #000; }
18
+ td.white { border-color: #fff; }
19
+ </style>
20
+ <%= raw qrcode.as_html %>
21
+ <%= link_to "Back", :back %>
@@ -0,0 +1,20 @@
1
+ :css
2
+ table {
3
+ border-width: 0;
4
+ border-style: none;
5
+ border-color: #0000ff;
6
+ border-collapse: collapse;
7
+ }
8
+
9
+ td {
10
+ border-left: solid 10px #000;
11
+ padding: 0;
12
+ margin: 0;
13
+ width: 0px;
14
+ height: 10px;
15
+ }
16
+
17
+ td.black { border-color: #000; }
18
+ td.white { border-color: #fff; }
19
+ = raw qrcode.as_html
20
+ = link_to "Back", :back
@@ -0,0 +1,20 @@
1
+ css:
2
+ | table {
3
+ | border-width: 0;
4
+ | border-style: none;
5
+ | border-color: #0000ff;
6
+ | border-collapse: collapse;
7
+ | }
8
+
9
+ | td {
10
+ | border-left: solid 10px #000;
11
+ | padding: 0;
12
+ | margin: 0;
13
+ | width: 0px;
14
+ | height: 10px;
15
+ | }
16
+
17
+ | td.black { border-color: #000; }
18
+ | td.white { border-color: #fff; }
19
+ = raw qrcode.as_html
20
+ = link_to "Back", :back
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :twofactor do
3
+ # # Task goes here
4
+ # end
data/lib/twofactor.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'base32'
2
+ require 'totp'
3
+ require 'rqrcode'
4
+ module Twofactor
5
+ class TwoStep
6
+ def self.enable_twofactor_auth(model_object)
7
+ model_object.update_attribute(Rails.application.class::TWOFACTOR_SECRET_ATTRIBUTE, ::Base32.encode(rand(36**6).to_s(36)))
8
+ model_object.update_attribute(Rails.application.class::TWOFACTOR_FLAG_ATTRIBUTE, true)
9
+ qrcode = ::RQRCode::QRCode.new("otpauth://totp/#{provider_name}:#{model_object[Rails.application.class::TWOFACTOR_REFERENCE_FIELD]}?secret=#{model_object[Rails.application.class::TWOFACTOR_SECRET_ATTRIBUTE]}&issuer=#{provider_name}", :size => Rails.application.class::TWOFACTOR_QRCODE_SIZE, :level => Rails.application.class::TWOFACTOR_QRCODE_QUALITY.to_sym)
10
+ return qrcode
11
+ end
12
+ def self.disable_twofactor_auth(model_object)
13
+ model_object.update_attribute(Rails.application.class::TWOFACTOR_SECRET_ATTRIBUTE, nil)
14
+ model_object.update_attribute(Rails.application.class::TWOFACTOR_FLAG_ATTRIBUTE, false)
15
+ return true
16
+ end
17
+ def self.valid_code?(model_object, code)
18
+ ::TOTP.valid?(model_object[Rails.application.class::TWOFACTOR_SECRET_ATTRIBUTE], code)
19
+ end
20
+
21
+ private
22
+
23
+ def self.provider_name
24
+ if Rails.application.class::TWOFACTOR_PROVIDER_NAME.blank?
25
+ return Rails.application.config.session_options[:key].sub(/^_/,'').sub(/_session/,'')
26
+ else
27
+ return Rails.application.class::TWOFACTOR_PROVIDER_NAME
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module Twofactor
2
+ VERSION = "0.0.2"
3
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twofactor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - venkatramanka
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rqrcode
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: totp
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A gem to integrate your Rails app with Google 2Factor-authenticator mobile
56
+ apps
57
+ email:
58
+ - venkatka@outlook.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - MIT-LICENSE
64
+ - Rakefile
65
+ - lib/generators/twofactor/install_generator.rb
66
+ - lib/generators/twofactor/templates/twofactor_config.rb
67
+ - lib/generators/twofactor/templates/twofactor_migration.rb
68
+ - lib/generators/twofactor/templates/twofactor_register.html.erb
69
+ - lib/generators/twofactor/templates/twofactor_register.html.haml
70
+ - lib/generators/twofactor/templates/twofactor_register.html.slim
71
+ - lib/tasks/twofactor_tasks.rake
72
+ - lib/twofactor.rb
73
+ - lib/twofactor/version.rb
74
+ homepage: https://github.com/venkatramanka/twofactor
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.4.5
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: A gem to integrate your Rails app with Google 2Factor-authenticator mobile
98
+ apps
99
+ test_files: []