Store strategy name in the Rails 3 log for Warden

194 Views Asked by At

we are using Warden for authentication and we have so many strategies it is difficult to track which one has been successful. Instead putting lines like

Rails.logger.debug "Authenticated with SSO" if user

to every strategy I would like to put one simple line somewhere to log the strategy message. It is available in the Warden somewhere because it stores the successful message:

success!(username, message)

How to do that? What is the best place to put this line in?

I guess I need a callback or something like that:

https://github.com/hassox/warden/wiki/Callbacks

1

There are 1 best solutions below

0
On BEST ANSWER

Got it:

Warden::Manager.after_authentication do |user,auth,opts|
  user = user.username if user.respond_to? :username
  message = auth.winning_strategy.message
  Rails.logger.info "User #{user} authenticated: #{auth.winning_strategy.message}"
end

And in the strategies:

success!(u, "with LDAP")

for example. That works.