Skip to content

Commit

Permalink
Fix code style
Browse files Browse the repository at this point in the history
  • Loading branch information
dankimio committed Dec 12, 2016
1 parent 3993b00 commit 5d20b9a
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 85 deletions.
38 changes: 18 additions & 20 deletions lib/generators/sorcery/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,35 @@ class InstallGenerator < Rails::Generators::Base

source_root File.expand_path('../templates', __FILE__)

argument :submodules, :optional => true, :type => :array, :banner => "submodules"
argument :submodules, optional: true, type: :array, banner: 'submodules'

class_option :model, :optional => true, :type => :string, :banner => "model",
:desc => "Specify the model class name if you will use anything other than 'User'"
class_option :model, optional: true, type: :string, banner: 'model',
desc: "Specify the model class name if you will use anything other than 'User'"

class_option :migrations, :optional => true, :type => :boolean, :banner => "migrations",
:desc => "[DEPRECATED] Please use --only-submodules option instead"

class_option :only_submodules, :optional => true, :type => :boolean, :banner => "only-submodules",
:desc => "Specify if you want to add submodules to an existing model\n\t\t\t # (will generate migrations files, and add submodules to config file)"
class_option :migrations, optional: true, type: :boolean, banner: 'migrations',
desc: '[DEPRECATED] Please use --only-submodules option instead'

class_option :only_submodules, optional: true, type: :boolean, banner: 'only-submodules',
desc: "Specify if you want to add submodules to an existing model\n\t\t\t # (will generate migrations files, and add submodules to config file)"

def check_deprecated_options
if options[:migrations]
warn("[DEPRECATED] `--migrations` option is deprecated, please use `--only-submodules` instead")
warn('[DEPRECATED] `--migrations` option is deprecated, please use `--only-submodules` instead')
end
end

# Copy the initializer file to config/initializers folder.
def copy_initializer_file
template "initializer.rb", sorcery_config_path unless only_submodules?
template 'initializer.rb', sorcery_config_path unless only_submodules?
end

def configure_initializer_file
# Add submodules to the initializer file.
if submodules
submodule_names = submodules.collect{ |submodule| ':' + submodule }
submodule_names = submodules.collect { |submodule| ':' + submodule }

gsub_file sorcery_config_path, /submodules = \[.*\]/ do |str|
current_submodule_names = (str =~ /\[(.*)\]/ ? $1 : '').delete(' ').split(',')
current_submodule_names = (str =~ /\[(.*)\]/ ? Regexp.last_match(1) : '').delete(' ').split(',')
"submodules = [#{(current_submodule_names | submodule_names).join(', ')}]"
end
end
Expand All @@ -54,7 +53,7 @@ def configure_model
end

def inject_sorcery_to_model
indents = " " * (namespaced? ? 2 : 1)
indents = ' ' * (namespaced? ? 2 : 1)

inject_into_class(model_path, model_class_name, "#{indents}authenticates_with_sorcery!\n")
end
Expand All @@ -63,29 +62,29 @@ def inject_sorcery_to_model
def copy_migration_files
# Copy core migration file in all cases except when you pass --only-submodules.
return unless defined?(Sorcery::Generators::InstallGenerator::ActiveRecord)
migration_template "migration/core.rb", "db/migrate/sorcery_core.rb", migration_class_name: migration_class_name unless only_submodules?
migration_template 'migration/core.rb', 'db/migrate/sorcery_core.rb', migration_class_name: migration_class_name unless only_submodules?

if submodules
submodules.each do |submodule|
unless submodule == "http_basic_auth" || submodule == "session_timeout" || submodule == "core"
unless submodule == 'http_basic_auth' || submodule == 'session_timeout' || submodule == 'core'
migration_template "migration/#{submodule}.rb", "db/migrate/sorcery_#{submodule}.rb", migration_class_name: migration_class_name
end
end
end

end

# Define the next_migration_number method (necessary for the migration_template method to work)
def self.next_migration_number(dirname)
if ActiveRecord::Base.timestamped_migrations
sleep 1 # make sure each time we get a different timestamp
Time.new.utc.strftime("%Y%m%d%H%M%S")
Time.new.utc.strftime('%Y%m%d%H%M%S')
else
"%.3d" % (current_migration_number(dirname) + 1)
'%.3d' % (current_migration_number(dirname) + 1)
end
end

private

def only_submodules?
options[:migrations] || options[:only_submodules]
end
Expand All @@ -94,10 +93,9 @@ def migration_class_name
if Rails::VERSION::MAJOR >= 5
"ActiveRecord::Migration[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
else
"ActiveRecord::Migration"
'ActiveRecord::Migration'
end
end

end
end
end
28 changes: 14 additions & 14 deletions lib/sorcery/model/submodules/brute_force_protection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ def self.included(base)
base.sorcery_config.class_eval do
attr_accessor :failed_logins_count_attribute_name, # failed logins attribute name.
:lock_expires_at_attribute_name, # this field indicates whether user
# is banned and when it will be active again.
# is banned and when it will be active again.
:consecutive_login_retries_amount_limit, # how many failed logins allowed.
:login_lock_time_period, # how long the user should be banned.
# in seconds. 0 for permanent.
# in seconds. 0 for permanent.

:unlock_token_attribute_name, # Unlock token attribute name
:unlock_token_email_method_name, # Mailer method name
Expand Down Expand Up @@ -43,14 +43,14 @@ def self.included(base)
module ClassMethods
def load_from_unlock_token(token)
return nil if token.blank?
user = sorcery_adapter.find_by_token(sorcery_config.unlock_token_attribute_name,token)
user = sorcery_adapter.find_by_token(sorcery_config.unlock_token_attribute_name, token)
user
end

protected

def define_brute_force_protection_fields
sorcery_adapter.define_field sorcery_config.failed_logins_count_attribute_name, Integer, :default => 0
sorcery_adapter.define_field sorcery_config.failed_logins_count_attribute_name, Integer, default: 0
sorcery_adapter.define_field sorcery_config.lock_expires_at_attribute_name, Time
sorcery_adapter.define_field sorcery_config.unlock_token_attribute_name, String
end
Expand All @@ -61,11 +61,11 @@ module InstanceMethods
# Calls 'lock!' if login retries limit was reached.
def register_failed_login!
config = sorcery_config
return if !unlocked?
return unless unlocked?

sorcery_adapter.increment(config.failed_logins_count_attribute_name)

if self.send(config.failed_logins_count_attribute_name) >= config.consecutive_login_retries_amount_limit
if send(config.failed_logins_count_attribute_name) >= config.consecutive_login_retries_amount_limit
lock!
end
end
Expand All @@ -75,9 +75,9 @@ def register_failed_login!
# /!\
def unlock!
config = sorcery_config
attributes = {config.lock_expires_at_attribute_name => nil,
config.failed_logins_count_attribute_name => 0,
config.unlock_token_attribute_name => nil}
attributes = { config.lock_expires_at_attribute_name => nil,
config.failed_logins_count_attribute_name => 0,
config.unlock_token_attribute_name => nil }
sorcery_adapter.update_attributes(attributes)
end

Expand All @@ -89,8 +89,8 @@ def locked?

def lock!
config = sorcery_config
attributes = {config.lock_expires_at_attribute_name => Time.now.in_time_zone + config.login_lock_time_period,
config.unlock_token_attribute_name => TemporaryToken.generate_random_token}
attributes = { config.lock_expires_at_attribute_name => Time.now.in_time_zone + config.login_lock_time_period,
config.unlock_token_attribute_name => TemporaryToken.generate_random_token }
sorcery_adapter.update_attributes(attributes)

unless config.unlock_token_mailer_disabled || config.unlock_token_mailer.nil?
Expand All @@ -100,7 +100,7 @@ def lock!

def unlocked?
config = sorcery_config
self.send(config.lock_expires_at_attribute_name).nil?
send(config.lock_expires_at_attribute_name).nil?
end

def send_unlock_token_email!
Expand All @@ -113,8 +113,8 @@ def send_unlock_token_email!
# Runs as a hook before authenticate.
def prevent_locked_user_login
config = sorcery_config
if !self.unlocked? && config.login_lock_time_period != 0
self.unlock! if self.send(config.lock_expires_at_attribute_name) <= Time.now.in_time_zone
if !unlocked? && config.login_lock_time_period != 0
unlock! if send(config.lock_expires_at_attribute_name) <= Time.now.in_time_zone
end
unlocked?
end
Expand Down
12 changes: 5 additions & 7 deletions lib/sorcery/model/submodules/remember_me.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def self.included(base)
:remember_me_token_expires_at_attribute_name, # the expires attribute in the model class.
:remember_me_token_persist_globally, # persist a single token globally for all logins/logouts (supporting multiple simultaneous browsers)
:remember_me_for # how long in seconds to remember.

end

base.sorcery_config.instance_eval do
Expand All @@ -36,7 +35,6 @@ def define_remember_me_fields
sorcery_adapter.define_field sorcery_config.remember_me_token_attribute_name, String
sorcery_adapter.define_field sorcery_config.remember_me_token_expires_at_attribute_name, Time
end

end

module InstanceMethods
Expand All @@ -47,14 +45,14 @@ def remember_me!
update_options = { config.remember_me_token_expires_at_attribute_name => Time.now.in_time_zone + config.remember_me_for }

unless config.remember_me_token_persist_globally && has_remember_me_token?
update_options.merge!(config.remember_me_token_attribute_name => TemporaryToken.generate_random_token)
update_options[config.remember_me_token_attribute_name] = TemporaryToken.generate_random_token
end

self.sorcery_adapter.update_attributes(update_options)
sorcery_adapter.update_attributes(update_options)
end

def has_remember_me_token?
self.send(sorcery_config.remember_me_token_attribute_name).present?
send(sorcery_config.remember_me_token_attribute_name).present?
end

# You shouldn't really use this one yourself - it's called by the controller's 'forget_me!' method.
Expand All @@ -66,8 +64,8 @@ def forget_me!
# You shouldn't really use this one yourself - it's called by the controller's 'force_forget_me!' method.
def force_forget_me!
config = sorcery_config
self.sorcery_adapter.update_attributes(config.remember_me_token_attribute_name => nil,
config.remember_me_token_expires_at_attribute_name => nil)
sorcery_adapter.update_attributes(config.remember_me_token_attribute_name => nil,
config.remember_me_token_expires_at_attribute_name => nil)
end
end
end
Expand Down
40 changes: 20 additions & 20 deletions sorcery.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'sorcery/version'

Gem::Specification.new do |s|
s.name = "sorcery"
s.name = 'sorcery'
s.version = Sorcery::VERSION
s.authors = ["Noam Ben Ari", "Kir Shatrov", "Grzegorz Witek"]
s.email = "[email protected]"
s.description = "Provides common authentication needs such as signing in/out, activating by email and resetting password."
s.summary = "Magical authentication for Rails 3 & 4 applications"
s.homepage = "https://github.com/Sorcery/sorcery"
s.authors = ['Noam Ben Ari', 'Kir Shatrov', 'Grzegorz Witek']
s.email = '[email protected]'
s.description = 'Provides common authentication needs such as signing in/out, activating by email and resetting password.'
s.summary = 'Magical authentication for Rails 3 & 4 applications'
s.homepage = 'https://github.com/Sorcery/sorcery'
s.post_install_message = "As of version 1.0 oauth/oauth2 won't be automatically bundled\n"
s.post_install_message += "you need to add those dependencies to your Gemfile"
s.post_install_message += 'you need to add those dependencies to your Gemfile'

s.files = `git ls-files`.split($/)
s.require_paths = ["lib"]
s.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
s.require_paths = ['lib']

s.licenses = ["MIT"]
s.licenses = ['MIT']

s.required_ruby_version = '>= 2.0.0'

s.add_dependency "oauth", "~> 0.4", ">= 0.4.4"
s.add_dependency "oauth2", ">= 0.8.0"
s.add_dependency "bcrypt", "~> 3.1"
s.add_dependency 'oauth', '~> 0.4', '>= 0.4.4'
s.add_dependency 'oauth2', '>= 0.8.0'
s.add_dependency 'bcrypt', '~> 3.1'

s.add_development_dependency "abstract", ">= 1.0.0"
s.add_development_dependency "json", ">= 1.7.7"
s.add_development_dependency "yard", "~> 0.6.0"
s.add_development_dependency 'abstract', '>= 1.0.0'
s.add_development_dependency 'json', '>= 1.7.7'
s.add_development_dependency 'yard', '~> 0.6.0'

s.add_development_dependency "timecop"
s.add_development_dependency "simplecov", ">= 0.3.8"
s.add_development_dependency "rspec-rails", "~> 3.5.0"
s.add_development_dependency "test-unit", "~> 3.1.0"
s.add_development_dependency 'timecop'
s.add_development_dependency 'simplecov', '>= 0.3.8'
s.add_development_dependency 'rspec-rails', '~> 3.5.0'
s.add_development_dependency 'test-unit', '~> 3.1.0'
end
18 changes: 8 additions & 10 deletions spec/rails_app/app/controllers/sorcery_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ class SorceryController < ActionController::Base
before_action :require_login_from_http_basic, only: [:test_http_basic_auth]
before_action :require_login, only: [:test_logout, :test_logout_with_force_forget_me, :test_should_be_logged_in, :some_action]

def index
end
def index; end

def some_action
head :ok
Expand Down Expand Up @@ -87,7 +86,7 @@ def login_at_test_twitter
login_at(:twitter)
end

alias :login_at_test :login_at_test_twitter
alias login_at_test login_at_test_twitter

def login_at_test_facebook
login_at(:facebook)
Expand Down Expand Up @@ -126,7 +125,7 @@ def login_at_test_slack
end

def login_at_test_with_state
login_at(:facebook, {state: 'bla'})
login_at(:facebook, state: 'bla')
end

def test_login_from_twitter
Expand All @@ -137,7 +136,7 @@ def test_login_from_twitter
end
end

alias :test_login_from :test_login_from_twitter
alias test_login_from test_login_from_twitter

def test_login_from_facebook
if @user = login_from(:facebook)
Expand Down Expand Up @@ -227,7 +226,7 @@ def test_return_to_with_external_jira
end
end

alias :test_return_to_with_external :test_return_to_with_external_twitter
alias test_return_to_with_external test_return_to_with_external_twitter

def test_return_to_with_external_facebook
if @user = login_from(:facebook)
Expand Down Expand Up @@ -307,17 +306,17 @@ def test_add_second_provider
provider = params[:provider]
if logged_in?
if @user = add_provider_to_user(provider)
redirect_to "bla", :notice => "Success!"
redirect_to 'bla', notice: 'Success!'
else
redirect_to "blu", :alert => "Failed!"
redirect_to 'blu', alert: 'Failed!'
end
end
end

def test_create_from_provider_with_block
provider = params[:provider]
login_from(provider)
@user = create_from(provider) do |user|
@user = create_from(provider) do |_user|
# check uniqueness of email
# User.where(email: user.email).empty?
false
Expand All @@ -328,5 +327,4 @@ def test_create_from_provider_with_block
redirect_to 'blu', alert: 'Failed!'
end
end

end
Loading

0 comments on commit 5d20b9a

Please sign in to comment.