From e1364958366c792b52e41508f2fa5cff3cd79bd0 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Thu, 23 Jun 2022 14:55:30 +0100 Subject: [PATCH] Hard fork and rename project --- .codeclimate.yml | 21 ----------- .travis.yml | 28 -------------- README.md | 4 +- .../two_factor_authentication_controller.rb | 4 +- ...uthentication.gemspec => devise2fa.gemspec | 16 ++++---- ..._factor_authentication.rb => devise2fa.rb} | 18 ++++----- .../controllers/helpers.rb | 6 +-- .../hooks/two_factor_authenticatable.rb | 6 +-- .../models/two_factor_authenticatable.rb | 4 +- lib/devise2fa/orm/active_record.rb | 14 +++++++ .../rails.rb | 4 +- .../routes.rb | 0 .../schema.rb | 2 +- lib/devise2fa/version.rb | 3 ++ .../orm/active_record.rb | 14 ------- lib/two_factor_authentication/version.rb | 3 -- spec/rails_app/config/application.rb | 5 ++- spec/rails_app/db/schema.rb | 37 +++++++++---------- spec/support/controller_helper.rb | 2 +- spec/support/features_spec_helper.rb | 4 +- 20 files changed, 74 insertions(+), 121 deletions(-) delete mode 100644 .codeclimate.yml delete mode 100644 .travis.yml rename two_factor_authentication.gemspec => devise2fa.gemspec (75%) rename lib/{two_factor_authentication.rb => devise2fa.rb} (60%) rename lib/{two_factor_authentication => devise2fa}/controllers/helpers.rb (85%) rename lib/{two_factor_authentication => devise2fa}/hooks/two_factor_authenticatable.rb (65%) rename lib/{two_factor_authentication => devise2fa}/models/two_factor_authenticatable.rb (98%) create mode 100644 lib/devise2fa/orm/active_record.rb rename lib/{two_factor_authentication => devise2fa}/rails.rb (52%) rename lib/{two_factor_authentication => devise2fa}/routes.rb (100%) rename lib/{two_factor_authentication => devise2fa}/schema.rb (95%) create mode 100644 lib/devise2fa/version.rb delete mode 100644 lib/two_factor_authentication/orm/active_record.rb delete mode 100644 lib/two_factor_authentication/version.rb diff --git a/.codeclimate.yml b/.codeclimate.yml deleted file mode 100644 index 35973cb..0000000 --- a/.codeclimate.yml +++ /dev/null @@ -1,21 +0,0 @@ -engines: - brakeman: - enabled: true - duplication: - enabled: true - config: - languages: - - ruby - # mass_threshold: 30 - exclude_paths: - - 'spec/**/*' - fixme: - enabled: true - rubocop: - enabled: true - -ratings: - paths: - - app/** - - lib/** - - '**.rb' diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index c3fc35f..0000000 --- a/.travis.yml +++ /dev/null @@ -1,28 +0,0 @@ -language: ruby - -env: - - "RAILS_VERSION=4.2" - - "RAILS_VERSION=5.2" - - "RAILS_VERSION=master" - -rvm: - - 2.3.8 - - 2.4.5 - - 2.5.3 - -matrix: - fast_finish: true - allow_failures: - - env: "RAILS_VERSION=master" - include: - - rvm: 2.2 - env: RAILS_VERSION=4.2 - -before_install: - - gem uninstall -v '>= 2' -i $(rvm gemdir)@global -ax bundler || true - - gem install bundler -v '< 2' - -before_script: - - bundle exec rake app:db:setup - -script: bundle exec rake spec diff --git a/README.md b/README.md index 36fda65..c88dee7 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # Two factor authentication for Devise -[![Build Status](https://github.com/devise2fa/two_factor_authentication/actions/workflows/tests.yml/badge.svg?branch=master&event=push)](https://github.com/devise2fa/two_factor_authentication/actions/workflows/tests.yml) +[![Build Status](https://github.com/devise2fa/two_factor_authentication/actions/workflows/tests.yml/badge.svg?branch=master&event=push)](https://github.com/baarkerlounger/two_factor_authentication/actions/workflows/tests.yml) + +This is a fork of [Houdini/two_factor_authentication](https://github.com/Houdini/two_factor_authentication) to enable development to continue as that repository no longer seems to be actively developed. With thanks to all contributors. ## Features diff --git a/app/controllers/devise/two_factor_authentication_controller.rb b/app/controllers/devise/two_factor_authentication_controller.rb index 9430d6a..490eeda 100644 --- a/app/controllers/devise/two_factor_authentication_controller.rb +++ b/app/controllers/devise/two_factor_authentication_controller.rb @@ -27,7 +27,7 @@ class Devise::TwoFactorAuthenticationController < DeviseController def after_two_factor_success_for(resource) set_remember_two_factor_cookie(resource) - warden.session(resource_name)[TwoFactorAuthentication::NEED_AUTHENTICATION] = false + warden.session(resource_name)[Devise2Fa::NEED_AUTHENTICATION] = false # For compatability with devise versions below v4.2.0 # https://github.com/plataformatec/devise/commit/2044fffa25d781fcbaf090e7728b48b65c854ccb if respond_to?(:bypass_sign_in) @@ -45,7 +45,7 @@ class Devise::TwoFactorAuthenticationController < DeviseController expires_seconds = resource.class.remember_otp_session_for_seconds if expires_seconds && expires_seconds > 0 - cookies.signed[TwoFactorAuthentication::REMEMBER_TFA_COOKIE_NAME] = { + cookies.signed[Devise2Fa::REMEMBER_TFA_COOKIE_NAME] = { value: "#{resource.class}-#{resource.public_send(Devise.second_factor_resource_id)}", expires: expires_seconds.seconds.from_now } diff --git a/two_factor_authentication.gemspec b/devise2fa.gemspec similarity index 75% rename from two_factor_authentication.gemspec rename to devise2fa.gemspec index 9580f11..11a78ac 100644 --- a/two_factor_authentication.gemspec +++ b/devise2fa.gemspec @@ -1,14 +1,14 @@ # -*- encoding: utf-8 -*- $:.push File.expand_path("../lib", __FILE__) -require "two_factor_authentication/version" +require "devise2fa/version" Gem::Specification.new do |s| - s.name = "two_factor_authentication" - s.version = TwoFactorAuthentication::VERSION.dup - s.authors = ["Dmitrii Golub"] - s.email = ["dmitrii.golub@gmail.com"] - s.homepage = "https://github.com/Houdini/two_factor_authentication" - s.summary = %q{Two factor authentication plugin for devise} + s.name = "devise2fa" + s.version = Devise2Fa::VERSION.dup + s.authors = ["baarkerlounger"] + s.email = ["baarkerlounger@protonmail.com"] + s.homepage = "https://github.com/devise2fa/devise2fa" + s.summary = %q{Two factor authentication plugin for devise forked from Houdini/two_factor_authentication} s.license = "MIT" s.description = <<-EOF ### Features ### @@ -18,7 +18,7 @@ Gem::Specification.new do |s| * your own sms logic EOF - s.rubyforge_project = "two_factor_authentication" + s.rubyforge_project = "devise2fa" s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") diff --git a/lib/two_factor_authentication.rb b/lib/devise2fa.rb similarity index 60% rename from lib/two_factor_authentication.rb rename to lib/devise2fa.rb index 7b1bbbc..7eb687a 100644 --- a/lib/two_factor_authentication.rb +++ b/lib/devise2fa.rb @@ -1,4 +1,4 @@ -require 'two_factor_authentication/version' +require 'devise2fa/version' require 'devise' require 'active_support/concern' require "active_model" @@ -34,19 +34,19 @@ module Devise @@delete_cookie_on_logout = false end -module TwoFactorAuthentication +module Devise2Fa NEED_AUTHENTICATION = 'need_two_factor_authentication' REMEMBER_TFA_COOKIE_NAME = "remember_tfa" - autoload :Schema, 'two_factor_authentication/schema' + autoload :Schema, 'devise2fa/schema' module Controllers - autoload :Helpers, 'two_factor_authentication/controllers/helpers' + autoload :Helpers, 'devise2fa/controllers/helpers' end end -Devise.add_module :two_factor_authenticatable, :model => 'two_factor_authentication/models/two_factor_authenticatable', :controller => :two_factor_authentication, :route => :two_factor_authentication +Devise.add_module :two_factor_authenticatable, :model => 'devise2fa/models/two_factor_authenticatable', :controller => :two_factor_authentication, :route => :two_factor_authentication -require 'two_factor_authentication/orm/active_record' if defined?(ActiveRecord::Base) -require 'two_factor_authentication/routes' -require 'two_factor_authentication/models/two_factor_authenticatable' -require 'two_factor_authentication/rails' +require 'devise2fa/orm/active_record' if defined?(ActiveRecord::Base) +require 'devise2fa/routes' +require 'devise2fa/models/two_factor_authenticatable' +require 'devise2fa/rails' diff --git a/lib/two_factor_authentication/controllers/helpers.rb b/lib/devise2fa/controllers/helpers.rb similarity index 85% rename from lib/two_factor_authentication/controllers/helpers.rb rename to lib/devise2fa/controllers/helpers.rb index 64e8377..73928b4 100644 --- a/lib/two_factor_authentication/controllers/helpers.rb +++ b/lib/devise2fa/controllers/helpers.rb @@ -1,4 +1,4 @@ -module TwoFactorAuthentication +module Devise2Fa module Controllers module Helpers extend ActiveSupport::Concern @@ -12,7 +12,7 @@ module TwoFactorAuthentication def handle_two_factor_authentication unless devise_controller? Devise.mappings.keys.flatten.any? do |scope| - if signed_in?(scope) and warden.session(scope)[TwoFactorAuthentication::NEED_AUTHENTICATION] + if signed_in?(scope) and warden.session(scope)[Devise2Fa::NEED_AUTHENTICATION] handle_failed_second_factor(scope) end end @@ -47,7 +47,7 @@ module Devise module Controllers module Helpers def is_fully_authenticated? - !session["warden.user.user.session"].try(:[], TwoFactorAuthentication::NEED_AUTHENTICATION) + !session["warden.user.user.session"].try(:[], Devise2Fa::NEED_AUTHENTICATION) end end end diff --git a/lib/two_factor_authentication/hooks/two_factor_authenticatable.rb b/lib/devise2fa/hooks/two_factor_authenticatable.rb similarity index 65% rename from lib/two_factor_authentication/hooks/two_factor_authenticatable.rb rename to lib/devise2fa/hooks/two_factor_authenticatable.rb index 3ff0341..4c4e070 100644 --- a/lib/two_factor_authentication/hooks/two_factor_authenticatable.rb +++ b/lib/devise2fa/hooks/two_factor_authenticatable.rb @@ -1,17 +1,17 @@ Warden::Manager.after_authentication do |user, auth, options| if auth.env["action_dispatch.cookies"] expected_cookie_value = "#{user.class}-#{user.public_send(Devise.second_factor_resource_id)}" - actual_cookie_value = auth.env["action_dispatch.cookies"].signed[TwoFactorAuthentication::REMEMBER_TFA_COOKIE_NAME] + actual_cookie_value = auth.env["action_dispatch.cookies"].signed[Devise2Fa::REMEMBER_TFA_COOKIE_NAME] bypass_by_cookie = actual_cookie_value == expected_cookie_value end if user.respond_to?(:need_two_factor_authentication?) && !bypass_by_cookie - if auth.session(options[:scope])[TwoFactorAuthentication::NEED_AUTHENTICATION] = user.need_two_factor_authentication?(auth.request) + if auth.session(options[:scope])[Devise2Fa::NEED_AUTHENTICATION] = user.need_two_factor_authentication?(auth.request) user.send_new_otp if user.send_new_otp_after_login? end end end Warden::Manager.before_logout do |user, auth, _options| - auth.cookies.delete TwoFactorAuthentication::REMEMBER_TFA_COOKIE_NAME if Devise.delete_cookie_on_logout + auth.cookies.delete Devise2Fa::REMEMBER_TFA_COOKIE_NAME if Devise.delete_cookie_on_logout end diff --git a/lib/two_factor_authentication/models/two_factor_authenticatable.rb b/lib/devise2fa/models/two_factor_authenticatable.rb similarity index 98% rename from lib/two_factor_authentication/models/two_factor_authenticatable.rb rename to lib/devise2fa/models/two_factor_authenticatable.rb index d23cae8..d345dbe 100644 --- a/lib/two_factor_authentication/models/two_factor_authenticatable.rb +++ b/lib/devise2fa/models/two_factor_authenticatable.rb @@ -1,4 +1,4 @@ -require 'two_factor_authentication/hooks/two_factor_authenticatable' +require 'devise2fa/hooks/two_factor_authenticatable' require 'rotp' require 'encryptor' @@ -40,7 +40,7 @@ module Devise raise "authenticate_totp called with no otp_secret_key set" if totp_secret.nil? totp = ROTP::TOTP.new(totp_secret, digits: digits) new_timestamp = totp.verify( - without_spaces(code), + without_spaces(code), drift_ahead: drift, drift_behind: drift, after: totp_timestamp ) return false unless new_timestamp diff --git a/lib/devise2fa/orm/active_record.rb b/lib/devise2fa/orm/active_record.rb new file mode 100644 index 0000000..9620fb1 --- /dev/null +++ b/lib/devise2fa/orm/active_record.rb @@ -0,0 +1,14 @@ +require "active_record" + +module Devise2Fa + module Orm + module ActiveRecord + module Schema + # include Devise2Fa::Schema + end + end + end +end + +ActiveRecord::ConnectionAdapters::Table.send :include, Devise2Fa::Orm::ActiveRecord::Schema +ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Devise2Fa::Orm::ActiveRecord::Schema diff --git a/lib/two_factor_authentication/rails.rb b/lib/devise2fa/rails.rb similarity index 52% rename from lib/two_factor_authentication/rails.rb rename to lib/devise2fa/rails.rb index 206c0b2..5b544d7 100644 --- a/lib/two_factor_authentication/rails.rb +++ b/lib/devise2fa/rails.rb @@ -1,7 +1,7 @@ -module TwoFactorAuthentication +module Devise2Fa class Engine < ::Rails::Engine ActiveSupport.on_load(:action_controller) do - include TwoFactorAuthentication::Controllers::Helpers + include Devise2Fa::Controllers::Helpers end end end diff --git a/lib/two_factor_authentication/routes.rb b/lib/devise2fa/routes.rb similarity index 100% rename from lib/two_factor_authentication/routes.rb rename to lib/devise2fa/routes.rb diff --git a/lib/two_factor_authentication/schema.rb b/lib/devise2fa/schema.rb similarity index 95% rename from lib/two_factor_authentication/schema.rb rename to lib/devise2fa/schema.rb index 9f938b7..3294553 100644 --- a/lib/two_factor_authentication/schema.rb +++ b/lib/devise2fa/schema.rb @@ -1,4 +1,4 @@ -module TwoFactorAuthentication +module Devise2Fa module Schema def second_factor_attempts_count apply_devise_schema :second_factor_attempts_count, Integer, :default => 0 diff --git a/lib/devise2fa/version.rb b/lib/devise2fa/version.rb new file mode 100644 index 0000000..8e43036 --- /dev/null +++ b/lib/devise2fa/version.rb @@ -0,0 +1,3 @@ +module Devise2Fa + VERSION = "3.0.0".freeze +end diff --git a/lib/two_factor_authentication/orm/active_record.rb b/lib/two_factor_authentication/orm/active_record.rb deleted file mode 100644 index 8053ee3..0000000 --- a/lib/two_factor_authentication/orm/active_record.rb +++ /dev/null @@ -1,14 +0,0 @@ -require "active_record" - -module TwoFactorAuthentication - module Orm - module ActiveRecord - module Schema - include TwoFactorAuthentication::Schema - end - end - end -end - -ActiveRecord::ConnectionAdapters::Table.send :include, TwoFactorAuthentication::Orm::ActiveRecord::Schema -ActiveRecord::ConnectionAdapters::TableDefinition.send :include, TwoFactorAuthentication::Orm::ActiveRecord::Schema diff --git a/lib/two_factor_authentication/version.rb b/lib/two_factor_authentication/version.rb deleted file mode 100644 index 239fae1..0000000 --- a/lib/two_factor_authentication/version.rb +++ /dev/null @@ -1,3 +0,0 @@ -module TwoFactorAuthentication - VERSION = "2.2.0".freeze -end diff --git a/spec/rails_app/config/application.rb b/spec/rails_app/config/application.rb index 2d31d58..88193a2 100644 --- a/spec/rails_app/config/application.rb +++ b/spec/rails_app/config/application.rb @@ -6,7 +6,7 @@ require "action_mailer/railtie" require "sprockets/railtie" Bundler.require(*Rails.groups) -require "two_factor_authentication" +require "devise2fa" module Dummy class Application < Rails::Application @@ -47,6 +47,8 @@ module Dummy # like if you have constraints or database-specific column types # config.active_record.schema_format = :sql + config.active_record.legacy_connection_handling = false + # Enable the asset pipeline config.assets.enabled = true @@ -60,4 +62,3 @@ module Dummy config.secret_key_base = 'secretvalue' end end - diff --git a/spec/rails_app/db/schema.rb b/spec/rails_app/db/schema.rb index 9c0d0f1..f50aca1 100644 --- a/spec/rails_app/db/schema.rb +++ b/spec/rails_app/db/schema.rb @@ -2,29 +2,28 @@ # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # -# Note that this schema.rb definition is the authoritative source for your -# database schema. If you need to create the application database on another -# system, you should be using db:schema:load, not running all the migrations -# from scratch. The latter is a flawed and unsustainable approach (the more migrations -# you'll amass, the slower it'll run and the greater likelihood for issues). +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2016_02_09_032439) do - +ActiveRecord::Schema[7.0].define(version: 2016_02_09_032439) do create_table "admins", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" - t.datetime "reset_password_sent_at" - t.datetime "remember_created_at" + t.datetime "reset_password_sent_at", precision: nil + t.datetime "remember_created_at", precision: nil t.integer "sign_in_count", default: 0, null: false - t.datetime "current_sign_in_at" - t.datetime "last_sign_in_at" + t.datetime "current_sign_in_at", precision: nil + t.datetime "last_sign_in_at", precision: nil t.string "current_sign_in_ip" t.string "last_sign_in_ip" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", precision: nil, null: false + t.datetime "updated_at", precision: nil, null: false t.index ["email"], name: "index_admins_on_email", unique: true t.index ["reset_password_token"], name: "index_admins_on_reset_password_token", unique: true end @@ -33,15 +32,15 @@ ActiveRecord::Schema.define(version: 2016_02_09_032439) do t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false t.string "reset_password_token" - t.datetime "reset_password_sent_at" - t.datetime "remember_created_at" + t.datetime "reset_password_sent_at", precision: nil + t.datetime "remember_created_at", precision: nil t.integer "sign_in_count", default: 0, null: false - t.datetime "current_sign_in_at" - t.datetime "last_sign_in_at" + t.datetime "current_sign_in_at", precision: nil + t.datetime "last_sign_in_at", precision: nil t.string "current_sign_in_ip" t.string "last_sign_in_ip" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", precision: nil, null: false + t.datetime "updated_at", precision: nil, null: false t.integer "second_factor_attempts_count", default: 0 t.string "nickname", limit: 64 t.string "encrypted_otp_secret_key" diff --git a/spec/support/controller_helper.rb b/spec/support/controller_helper.rb index 2ca3a31..6f1d701 100644 --- a/spec/support/controller_helper.rb +++ b/spec/support/controller_helper.rb @@ -2,7 +2,7 @@ module ControllerHelper def sign_in(user = create_user('not_encrypted')) allow(warden).to receive(:authenticated?).with(:user).and_return(true) allow(controller).to receive(:current_user).and_return(user) - warden.session(:user)[TwoFactorAuthentication::NEED_AUTHENTICATION] = true + warden.session(:user)[Devise2Fa::NEED_AUTHENTICATION] = true end end diff --git a/spec/support/features_spec_helper.rb b/spec/support/features_spec_helper.rb index 9662e19..592f5c6 100644 --- a/spec/support/features_spec_helper.rb +++ b/spec/support/features_spec_helper.rb @@ -20,11 +20,11 @@ module FeaturesSpecHelper end def set_tfa_cookie value - set_cookie TwoFactorAuthentication::REMEMBER_TFA_COOKIE_NAME, value + set_cookie Devise2Fa::REMEMBER_TFA_COOKIE_NAME, value end def get_tfa_cookie - get_cookie TwoFactorAuthentication::REMEMBER_TFA_COOKIE_NAME + get_cookie Devise2Fa::REMEMBER_TFA_COOKIE_NAME end end