Sha256: 4e2ecf77d73e854103ae05c525463cf3690d4510c4597ca3087dcfc2e35677c4

Contents?: true

Size: 1.33 KB

Versions: 23

Compression:

Stored size: 1.33 KB

Contents

module Clearance
  module PasswordStrategies
    module BCryptMigrationFromSHA1
      class BCryptUser
        include Clearance::PasswordStrategies::BCrypt

        def initialize(user)
          @user = user
        end

        delegate :encrypted_password, :encrypted_password=, to: :@user
      end

      class SHA1User
        include Clearance::PasswordStrategies::SHA1

        def initialize(user)
          @user = user
        end

        delegate :salt, :salt=, :encrypted_password, :encrypted_password=, to: :@user
      end

      def authenticated?(password)
        authenticated_with_sha1?(password) || authenticated_with_bcrypt?(password)
      end

      def password=(new_password)
        @password = new_password
        BCryptUser.new(self).password = new_password
      end

      private

      def authenticated_with_bcrypt?(password)
        begin
          BCryptUser.new(self).authenticated? password
        rescue ::BCrypt::Errors::InvalidHash
          false
        end
      end

      def authenticated_with_sha1?(password)
        if sha1_password?
          if SHA1User.new(self).authenticated? password
            self.password = password
            self.save
            true
          end
        end
      end

      def sha1_password?
        self.encrypted_password =~ /^[a-f0-9]{40}$/
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 2 rubygems

Version Path
clearance-deprecated_password_strategies-1.10.2 lib/clearance/password_strategies/deprecated/bcrypt_migration_from_sha1.rb
clearance-deprecated_password_strategies-1.10.1 lib/clearance/password_strategies/deprecated/bcrypt_migration_from_sha1.rb
clearance-1.9.0 lib/clearance/password_strategies/bcrypt_migration_from_sha1.rb
clearance-1.8.1 lib/clearance/password_strategies/bcrypt_migration_from_sha1.rb
clearance-1.8.0 lib/clearance/password_strategies/bcrypt_migration_from_sha1.rb
clearance-1.7.0 lib/clearance/password_strategies/bcrypt_migration_from_sha1.rb
clearance-1.6.1 lib/clearance/password_strategies/bcrypt_migration_from_sha1.rb
clearance-1.6.0 lib/clearance/password_strategies/bcrypt_migration_from_sha1.rb
clearance-1.5.1 lib/clearance/password_strategies/bcrypt_migration_from_sha1.rb
clearance-1.5.0 lib/clearance/password_strategies/bcrypt_migration_from_sha1.rb
clearance-1.4.3 lib/clearance/password_strategies/bcrypt_migration_from_sha1.rb
clearance-1.4.2 lib/clearance/password_strategies/bcrypt_migration_from_sha1.rb
clearance-1.4.1 lib/clearance/password_strategies/bcrypt_migration_from_sha1.rb
clearance-1.4.0 lib/clearance/password_strategies/bcrypt_migration_from_sha1.rb
clearance-1.3.0 lib/clearance/password_strategies/bcrypt_migration_from_sha1.rb
clearance-1.2.1 lib/clearance/password_strategies/bcrypt_migration_from_sha1.rb
clearance-1.2.0 lib/clearance/password_strategies/bcrypt_migration_from_sha1.rb
clearance-1.1.0 lib/clearance/password_strategies/bcrypt_migration_from_sha1.rb
clearance-1.0.1 lib/clearance/password_strategies/bcrypt_migration_from_sha1.rb
clearance-1.0.0 lib/clearance/password_strategies/bcrypt_migration_from_sha1.rb