This repository has been archived by the owner on Feb 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 187
Adds CurrentUser processor #16
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,35 @@ | ||
class CASino::SessionsController < CASino::ApplicationController | ||
include CASino::SessionsHelper | ||
|
||
skip_before_filter :set_current_user, only:[:create, :validate_otp] | ||
|
||
def index | ||
processor(:TwoFactorAuthenticatorOverview).process(cookies, request.user_agent) | ||
processor(:SessionOverview).process(cookies, request.user_agent) | ||
processor(:TwoFactorAuthenticatorOverview).process(current_user, request.user_agent) | ||
processor(:SessionOverview).process(current_user, request.user_agent) | ||
end | ||
|
||
def new | ||
processor(:LoginCredentialRequestor).process(params, cookies, request.user_agent) | ||
processor(:LoginCredentialRequestor).process(params, current_user, request.user_agent) | ||
end | ||
|
||
def create | ||
processor(:LoginCredentialAcceptor).process(params, request.user_agent) | ||
end | ||
|
||
def destroy | ||
processor(:SessionDestroyer).process(params, cookies, request.user_agent) | ||
processor(:SessionDestroyer).process(params, current_user, request.user_agent) | ||
end | ||
|
||
def destroy_others | ||
processor(:OtherSessionsDestroyer).process(params, cookies, request.user_agent) | ||
processor(:OtherSessionsDestroyer).process(params, current_user, request.user_agent) | ||
end | ||
|
||
def logout | ||
processor(:Logout).process(params, cookies, request.user_agent) | ||
processor(:Logout).process(params, current_user, request.user_agent) | ||
end | ||
|
||
def validate_otp | ||
processor(:SecondFactorAuthenticationAcceptor).process(params, request.user_agent) | ||
processor(:CurrentUser).process(params, request.user_agent, ignore_two_factor:true) | ||
processor(:SecondFactorAuthenticationAcceptor).process(params, current_user, request.user_agent) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module CASino | ||
module ApplicationHelper | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
include CASino::CurrentUserHelper | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module CASino::CurrentUserHelper | ||
include CASino::ProcessorHelper | ||
|
||
def self.included(base) | ||
return unless base.ancestors.include? ActionController::Base | ||
|
||
base.helper_method :current_user, :user_signed_in? | ||
base.before_filter :set_current_user | ||
end | ||
|
||
def set_current_user | ||
params[:tgt] ||= cookies[:tgt] | ||
processor(:CurrentUser).process(params, request.user_agent) | ||
end | ||
|
||
def current_user | ||
@current_user | ||
end | ||
|
||
def user_signed_in? | ||
!!current_user | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module CASino::ProcessorHelper | ||
protected | ||
def processor(processor_name, listener_name = nil) | ||
listener_name ||= processor_name | ||
listener = CASino.const_get(:"#{listener_name}Listener").new(self) | ||
@processor = CASino.const_get(:"#{processor_name}Processor").new(listener) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class CASino::CurrentUserListener < CASino::Listener | ||
def user_not_logged_in | ||
# NO-OP | ||
end | ||
|
||
def user_not_logged_in! | ||
@controller.redirect_to login_path | ||
end | ||
|
||
def current_user_found(user) | ||
assign(:current_user, user) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,23 @@ | ||
module CASino | ||
class Listener | ||
|
||
# include helpers to have the route path methods (like sessions_path) | ||
include CASino::Engine.routes.url_helpers | ||
|
||
def initialize(controller) | ||
@controller = controller | ||
end | ||
|
||
protected | ||
def assign(name, value) | ||
@controller.instance_variable_set("@#{name}", value) | ||
end | ||
|
||
def assigned(name) | ||
@controller.instance_variable_get("@#{name}") | ||
end | ||
|
||
protected | ||
def cookies | ||
@controller.send(:cookies) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
class CASino::CurrentUserProcessor < CASino::Processor | ||
include CASino::ProcessorConcern::LoginTickets | ||
include CASino::ProcessorConcern::TicketGrantingTickets | ||
|
||
# This method will call `#user_not_logged_in` or `#current_user_found(User)` on the listener. | ||
# @param [Hash] params A Hash delivered by the client used to located the User's Ticket Granting Ticket | ||
# @param [String] user_agent user-agent delivered by the client | ||
def process(params = nil, user_agent = nil, options = {}) | ||
options ||= {} | ||
if user = handle_process(params, user_agent, options) | ||
@listener.current_user_found(user) | ||
else | ||
@listener.user_not_logged_in | ||
end | ||
end | ||
|
||
# This method will call `#user_not_logged_in!` or `#current_user_found(User)` on the listener. | ||
# @param [Hash] params A Hash delivered by the client used to located the User's Ticket Granting Ticket | ||
# @param [String] user_agent user-agent delivered by the client | ||
def process!(params = nil, user_agent = nil, options = {}) | ||
options ||= {} | ||
if handle_process(params, user_agent, options) | ||
@listener.current_user_found | ||
else | ||
@listener.user_not_logged_in! | ||
end | ||
end | ||
|
||
private | ||
def handle_process(params, user_agent, options) | ||
return current_user if user_signed_in? | ||
|
||
@params, @user_agent = (params || {}), user_agent | ||
|
||
ticket_granting_ticket(options).try(:user) | ||
end | ||
|
||
def current_user | ||
@listener.assigned :current_user | ||
end | ||
|
||
def user_signed_in? | ||
current_user | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
end | ||
|
||
def ticket_granting_ticket(options) | ||
@ticket_granting_ticket ||= begin | ||
find_valid_ticket_granting_ticket(@params[:tgt], @user_agent, options[:ignore_two_factor]) | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, I don't like this. It introduces a circular dependency. I don't think it is really necessary, since the current user will be passed in as a parameter to all the processors.