rails_otp 1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f3e47045b4b6d34525bad4ac2031ffd777c6d822
4
+ data.tar.gz: a6082001f9ee2b796ad7e9310a0f9363d30d23a4
5
+ SHA512:
6
+ metadata.gz: b5670c341d5cc9ca591f9e7e4ffee82a57adc6b5102dfb31b903561e0cd270f390d3e811eafd937059dc34e530ac7beb0662787b9b7e647e8b45355de915dd0f
7
+ data.tar.gz: c3b6635dabf82cb3c1cfeb555de6ba0186b58f072f4976a50e364775cc75e37ac433c7de6b35ec62aa863723c4bc195b21d39b782083b5c8dc75f6143fb7f22d
@@ -0,0 +1,3 @@
1
+ .DS_Store
2
+ /pkg/*
3
+ .gem
@@ -0,0 +1,8 @@
1
+ image: ruby:2.3
2
+
3
+ before_script:
4
+ - gem install bundler
5
+ - bundle install
6
+
7
+ rspec:
8
+ script: "rake spec"
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at nitesh.sharma@cybrilla.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in otpgem.gemspec
4
+ gemspec
@@ -0,0 +1,41 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rails_otp (1.1.0)
5
+ httparty (~> 0.9.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.2.5)
11
+ httparty (0.9.0)
12
+ multi_json (~> 1.0)
13
+ multi_xml
14
+ multi_json (1.12.1)
15
+ multi_xml (0.5.5)
16
+ rake (10.5.0)
17
+ rspec (3.4.0)
18
+ rspec-core (~> 3.4.0)
19
+ rspec-expectations (~> 3.4.0)
20
+ rspec-mocks (~> 3.4.0)
21
+ rspec-core (3.4.4)
22
+ rspec-support (~> 3.4.0)
23
+ rspec-expectations (3.4.0)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.4.0)
26
+ rspec-mocks (3.4.1)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.4.0)
29
+ rspec-support (3.4.1)
30
+
31
+ PLATFORMS
32
+ ruby
33
+
34
+ DEPENDENCIES
35
+ bundler (~> 1.12)
36
+ rails_otp!
37
+ rake (~> 10.0)
38
+ rspec (~> 3.0)
39
+
40
+ BUNDLED WITH
41
+ 1.12.5
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 sinscary
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,75 @@
1
+ # Rails Otp
2
+ This gem is for adding two-factor-authentication. It generates OTP and Verifies it. And sends otp. For sending it uses Exotel, Smsgupshup etc. service providers' api
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'rails_otp'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install rails_otp
19
+
20
+ ## Usage
21
+
22
+ * **Migrations**
23
+
24
+ To generate migration run
25
+
26
+ `$ rails g opal:migration table_name`
27
+
28
+ Run `rake db:migrate` to run the migration.
29
+
30
+ ```
31
+ == 20160711094521 OtpAddToUsers: migrating ====================================
32
+ -- change_table(:users)
33
+ -> 0.0013s
34
+ == 20160711094521 OtpAddToUsers: migrated (0.0014s) ===========================
35
+ ```
36
+
37
+ Once done with this step, include module `RailsOtp` to your model.
38
+
39
+ * **Configurations**
40
+
41
+ Setup your configuraion as follows
42
+
43
+ ```
44
+ RailsOtp.configure do |config|
45
+ config.sid = 'exotel_sid'
46
+ config.token = 'exotel_token'
47
+ config.otp_expiry_time = 'set expiry time for otp'
48
+ end
49
+ ```
50
+
51
+ Now For e.g. you have instance of `user`. then use `user.generate_otp!` to generate_otp for user.
52
+
53
+ To regenerate otp run `user.regenerate_otp!`
54
+
55
+ To verify otp use `user.verify_otp('otp')`.
56
+
57
+ To send otp using exotel api use following method
58
+
59
+ ```
60
+ Opal::Exotel.send(:to => phone_munber, :body => 'Your message here')
61
+ ```
62
+
63
+
64
+
65
+ ## Development
66
+
67
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
68
+
69
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
70
+
71
+
72
+ ## License
73
+
74
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
75
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rails_otp"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,12 @@
1
+ require 'rails/generators/active_record'
2
+
3
+
4
+ module RailsOtp
5
+ class MigrationGenerator < ActiveRecord::Generators::Base
6
+ source_root File.expand_path("../../templates", __FILE__)
7
+
8
+ def copy_otp_migration
9
+ migration_template "migration.rb", "db/migrate/otp_add_to_#{table_name}.rb"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ class OtpAddTo<%= table_name.camelize %> < ActiveRecord::Migration
2
+ def self.up
3
+ change_table :<%= table_name %> do |t|
4
+ t.string :otp_code
5
+ t.boolean :verified_status
6
+
7
+ t.datetime :otp_expiry_time
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,22 @@
1
+ require "rails_otp/version"
2
+ require "rails_otp/otp"
3
+ require "rails_otp/exotel"
4
+ require "rails_otp/configuration"
5
+
6
+ module RailsOtp
7
+ class << self
8
+ attr_accessor :configuration
9
+ end
10
+
11
+ def self.configuration
12
+ @configuration ||= Configuration.new
13
+ end
14
+
15
+ def self.reset
16
+ @configuration = Configuration.new
17
+ end
18
+
19
+ def self.configure
20
+ yield(configuration)
21
+ end
22
+ end
@@ -0,0 +1,11 @@
1
+ module RailsOtp
2
+ class Configuration
3
+ attr_accessor :sid, :token, :otp_expiry_time
4
+
5
+ def initialize
6
+ @sid = 'exotel_sid'
7
+ @token = 'exotel_token'
8
+ @otp_expiry_time = Time.now + 5.minutes
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,28 @@
1
+ require 'httparty'
2
+
3
+ module RailsOtp
4
+ class Exotel
5
+ include HTTParty
6
+ base_uri "https://twilix.exotel.in/v1/Accounts"
7
+
8
+ def self.send(params={})
9
+ self.new.send(params)
10
+ end
11
+
12
+ def send(params={})
13
+ params = transform_params(params)
14
+ response = self.class.post("/#{RailsOtp::configuration.sid}/Sms/send", {:body => params, :basic_auth => auth })
15
+ response.response.code
16
+ end
17
+
18
+ protected
19
+
20
+ def transform_params(params)
21
+ params.inject({}){ |h, (key, value)| h[key.to_s.capitalize.to_sym] = value; h }
22
+ end
23
+
24
+ def auth
25
+ {:username => RailsOtp::configuration.sid, :password => RailsOtp::configuration.token}
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,59 @@
1
+ require 'securerandom'
2
+
3
+ module RailsOtp
4
+ def generate_otp
5
+ self.verified_status = false
6
+ #TODO: add expiry time to config
7
+ self.otp_expiry_time = RailsOtp::configuration.otp_expiry_time #code is valid only for 5 minutes
8
+ self.otp_code = SecureRandom.hex(4)
9
+ end
10
+
11
+ def generate_otp!
12
+ generate_otp
13
+ save
14
+ end
15
+
16
+ def verify_otp(otp)
17
+ otp_code_valid?(otp) && code_not_expired
18
+ end
19
+
20
+ def regenerate_otp
21
+ generate_otp
22
+ end
23
+
24
+ def regenerate_otp!
25
+ regenerate_otp
26
+ save
27
+ end
28
+
29
+ private
30
+
31
+ #check whether both generated and entered otp match or not
32
+ def matches?(otp)
33
+ otp == self.otp_code
34
+ end
35
+
36
+ #function to check whether otp is expired or not
37
+ def expired?
38
+ self.otp_expiry_time < Time.now
39
+ end
40
+
41
+ # custom messages for invalid code and code expiry time.
42
+ def otp_code_valid?(otp)
43
+ if matches?(otp)
44
+ true
45
+ else
46
+ errors.add(:base, "you have entered a invalid code")
47
+ false
48
+ end
49
+ end
50
+
51
+ def code_not_expired
52
+ if !expired?
53
+ true
54
+ else
55
+ errors.add(:base, "this otp code has been expired")
56
+ false
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module RailsOtp
2
+ VERSION = "1.1.0"
3
+ end
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails_otp/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails_otp"
8
+ spec.version = RailsOtp::VERSION
9
+ spec.authors = ["Nitesh Sharma"]
10
+ spec.email = ["nbsharma@outlook.com"]
11
+
12
+ spec.summary = %q{A ruby gem for two-factor-authentication}
13
+ spec.description = %q{This gem generates and verifies otp and sets api configuration based on service provider.}
14
+ spec.homepage = "https://github.com/sinscary/rails_otp"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.12"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec", "~> 3.0"
33
+
34
+ spec.add_dependency "httparty", "~>0.9.0"
35
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_otp
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nitesh Sharma
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-07-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: httparty
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.9.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 0.9.0
69
+ description: This gem generates and verifies otp and sets api configuration based
70
+ on service provider.
71
+ email:
72
+ - nbsharma@outlook.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - .gitlab-ci.yml
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/generators/rails_otp/migration_generator.rb
88
+ - lib/generators/templates/migration.rb
89
+ - lib/rails_otp.rb
90
+ - lib/rails_otp/configuration.rb
91
+ - lib/rails_otp/exotel.rb
92
+ - lib/rails_otp/otp.rb
93
+ - lib/rails_otp/version.rb
94
+ - rails_otp.gemspec
95
+ homepage: https://github.com/sinscary/rails_otp
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.6.6
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: A ruby gem for two-factor-authentication
119
+ test_files: []