Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format with rubyfmt #88

Merged
merged 2 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Format with Rubyfmt
  • Loading branch information
mikker committed Oct 7, 2020
commit 16329c30ae5244fe2f0e124f3983e3aff296d5b0
12 changes: 6 additions & 6 deletions app/controllers/passwordless/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,23 @@ def create
def show
# Make it "slow" on purpose to make brute-force attacks more of a hassle
BCrypt::Password.create(params[:token])
sign_in passwordless_session
sign_in(passwordless_session)

redirect_to passwordless_success_redirect_path
redirect_to(passwordless_success_redirect_path)
rescue Errors::TokenAlreadyClaimedError
flash[:error] = I18n.t(".passwordless.sessions.create.token_claimed")
redirect_to passwordless_failure_redirect_path
redirect_to(passwordless_failure_redirect_path)
rescue Errors::SessionTimedOutError
flash[:error] = I18n.t(".passwordless.sessions.create.session_expired")
redirect_to passwordless_failure_redirect_path
redirect_to(passwordless_failure_redirect_path)
end

# match '/sign_out', via: %i[get delete].
# Signs user out. Redirects to root_path
# @see ControllerHelpers#sign_out
def destroy
sign_out authenticatable_class
redirect_to passwordless_sign_out_redirect_path
sign_out(authenticatable_class)
redirect_to(passwordless_sign_out_redirect_path)
end

protected
Expand Down
3 changes: 1 addition & 2 deletions app/mailers/passwordless/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ class Mailer < Passwordless.parent_mailer.constantize
def magic_link(session)
@session = session

@magic_link = send(Passwordless.mounted_as)
.token_sign_in_url(session.token)
@magic_link = send(Passwordless.mounted_as).token_sign_in_url(session.token)

email_field = @session.authenticatable.class.passwordless_email_field
mail(
Expand Down
18 changes: 12 additions & 6 deletions app/models/passwordless/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,33 @@ module Passwordless
# The session responsible for holding the connection between the record
# trying to log in and the unique tokens.
class Session < ApplicationRecord
belongs_to :authenticatable,
polymorphic: true, inverse_of: :passwordless_sessions
belongs_to(
:authenticatable,
polymorphic: true,
inverse_of: :passwordless_sessions
)

validates \
validates(
:authenticatable,
:timeout_at,
:expires_at,
:user_agent,
:remote_addr,
:token,
presence: true
)

before_validation :set_defaults

scope :available, lambda {
where("expires_at > ?", Time.current)
}
scope(
:available,
lambda { where("expires_at > ?", Time.current) }
)

def self.valid
available
end

class << self
deprecate :valid, deprecator: SessionValidDeprecation
end
Expand Down
8 changes: 4 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

Passwordless::Engine.routes.draw do
get "/sign_in", to: "sessions#new", as: :sign_in
post "/sign_in", to: "sessions#create"
get "/sign_in/:token", to: "sessions#show", as: :token_sign_in
match "/sign_out", to: "sessions#destroy", via: %i[get delete], as: :sign_out
get("/sign_in", to: "sessions#new", as: :sign_in)
post("/sign_in", to: "sessions#create")
get("/sign_in/:token", to: "sessions#show", as: :token_sign_in)
match("/sign_out", to: "sessions#destroy", via: %i[get delete], as: :sign_out)
end
15 changes: 8 additions & 7 deletions db/migrate/20171104221735_create_passwordless_sessions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

class CreatePasswordlessSessions < ActiveRecord::Migration[5.1]
def change
create_table :passwordless_sessions do |t|
create_table(:passwordless_sessions) do |t|
t.belongs_to(
:authenticatable,
polymorphic: true,
index: {name: "authenticatable"}
)
t.datetime :timeout_at, null: false
t.datetime :expires_at, null: false
t.datetime :claimed_at
t.text :user_agent, null: false
t.string :remote_addr, null: false
t.string :token, null: false

t.datetime(:timeout_at, null: false)
t.datetime(:expires_at, null: false)
t.datetime(:claimed_at)
t.text(:user_agent, null: false)
t.string(:remote_addr, null: false)
t.string(:token, null: false)

t.timestamps
end
Expand Down
20 changes: 11 additions & 9 deletions lib/passwordless/controller_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def authenticate_by_cookie(authenticatable_class)

authenticate_by_session(authenticatable_class)
end

deprecate :authenticate_by_cookie, deprecator: CookieDeprecation

def upgrade_passwordless_cookie(authenticatable_class)
Expand All @@ -51,7 +52,7 @@ def upgrade_passwordless_cookie(authenticatable_class)
return unless (record = authenticatable_class.find_by(id: authenticatable_id))
new_session = build_passwordless_session(record).tap { |s| s.save! }

sign_in new_session
sign_in(new_session)

new_session.authenticatable
end
Expand All @@ -73,15 +74,16 @@ def authenticate_by_session(authenticatable_class)
# to sign in
# @return [ActiveRecord::Base] the record that is passed in.
def sign_in(record)
passwordless_session =
if record.is_a?(Passwordless::Session)
record
else
warn "Passwordless::ControllerHelpers#sign_in with authenticatable " \
passwordless_session = if record.is_a?(Passwordless::Session)
record
else
warn(
"Passwordless::ControllerHelpers#sign_in with authenticatable " \
"(`#{record.class}') is deprecated. Falling back to creating a " \
"new Passwordless::Session"
build_passwordless_session(record).tap { |s| s.save! }
end
)
build_passwordless_session(record).tap { |s| s.save! }
end

passwordless_session.claim! if Passwordless.restrict_token_reuse

Expand All @@ -105,8 +107,8 @@ def sign_out(authenticatable_class)
key = cookie_name(authenticatable_class)
cookies.encrypted.permanent[key] = {value: nil}
cookies.delete(key)
# /deprecated

# /deprecated
reset_session
true
end
Expand Down
6 changes: 4 additions & 2 deletions lib/passwordless/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ class Engine < ::Rails::Engine

config.to_prepare do
require "passwordless/router_helpers"

ActionDispatch::Routing::Mapper.include RouterHelpers
require "passwordless/model_helpers"

ActiveRecord::Base.extend ModelHelpers
require "passwordless/controller_helpers"

end

config.before_initialize do |app|
app.config.i18n.load_path +=
Dir[Engine.root.join("config", "locales", "*.yml")]
app.config.i18n.load_path += Dir[Engine.root.join("config", "locales", "*.yml")]
end
end
end
6 changes: 4 additions & 2 deletions lib/passwordless/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
module Passwordless
module Errors
# Raise this exception when a session is expired.
class SessionTimedOutError < StandardError; end
class SessionTimedOutError < StandardError
end

# Raise this exception when the token has been previously claimed
class TokenAlreadyClaimedError < StandardError; end
class TokenAlreadyClaimedError < StandardError
end
end
end
4 changes: 3 additions & 1 deletion lib/passwordless/model_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ module ModelHelpers
# field name (e.g. `:email`)
# @param field [string] email submitted by user.
def passwordless_with(field)
has_many :passwordless_sessions,
has_many(
:passwordless_sessions,
class_name: "Passwordless::Session",
as: :authenticatable
)

define_singleton_method(:passwordless_email_field) { field }
end
Expand Down
6 changes: 4 additions & 2 deletions lib/passwordless/router_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ def passwordless_for(resource, at: nil, as: nil)
mount_at = at || resource.to_s
mount_as = as || resource.to_s
mount(
Passwordless::Engine, at: mount_at, as: mount_as,
defaults: {authenticatable: resource.to_s.singularize}
Passwordless::Engine,
at: mount_at,
as: mount_as,
defaults: {authenticatable: resource.to_s.singularize}
)

Passwordless.mounted_as = mount_as
Expand Down
3 changes: 2 additions & 1 deletion lib/passwordless/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

module Passwordless
VERSION = "0.10.0" # :nodoc:
# :nodoc:
VERSION = "0.10.0"
end
13 changes: 8 additions & 5 deletions test/controllers/deprecated_secrets_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ def create_session_for(user)
end

def login(passwordless_session)
post "/deprecated_fake_login", params: {
authenticatable_type: passwordless_session.authenticatable_type,
authenticatable_id: passwordless_session.authenticatable_id,
}
post(
"/deprecated_fake_login",
params: {
authenticatable_type: passwordless_session.authenticatable_type,
authenticatable_id: passwordless_session.authenticatable_id
}
)
end

test "authenticate_by_cookies" do
test("authenticate_by_cookies") do
user = User.create(email: "[email protected]")
passwordless_session = create_session_for(user)
login(passwordless_session)
Expand Down
Loading