How to allow sign in with devise if user is already authenticated? - ruby-on-rails

There's an App which creates a new user when you first time open the site (if you don't have a cookie). Also you can sign in as different user. The problem is Devise doesn't allow to sign in as already authenticated user. Even if I override require_no_authentication in SessionsController warden still remembers the user through session.
I do aware about guest user pattern but there's no guest user. The app is intended to create real user accounts and allow to sign in as another users.
So the question is should I create new Warden strategy and if so, what checks shall I do there? Or there's another way?

Just reset the session in the desired action. You can just make the first line be reset_session, or make it a before filter, like so:
before_filter :deauthorize_user, only: [ :some_action ]
def deauthorize_user
reset_session
end

Related

Invalidating Devise user session identifier after password update

Scenario: As an Administrator I need to invalidate a user's session (log them out) after I update the user's password. This is in accordance with best practices as per https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html#Renew_the_Session_ID_After_Any_Privilege_Level_Change
I am using Devise and I saw here https://stackoverflow.com/a/45756884/664675 there is a config to log the user out: config.sign_in_after_reset_password = false
However, I have enabled this config in my devise.rb but the user remains logged in. Not sure why that is?
I am also using Redis as the session_store
Example::Application.config.session_store :cache_store,
key: '_example_session',
secure: true
Is it feasible to delete the particular user's session store from Redis upon Password reset by the Administrator? And if so how could I find their particular session key within Redis?
the flag sign_in_after_reset_password does not relate to logout user at all, sign_in_after_reset_password = false imply that in case a user update his account password by himself then do not automatically sign-in his account again, and that logic happen only on PasswordsController#update.
So you as admin try to change password of another user in a custom controller, of course it's not logout user no matter the value of sign_in_after_reset_password is.
devise use gem warden to logout user (in other word: destroy user session) and warden base on request session not base on database, that mean there's no way an admin can get another user's session to reset, so you can not force logout another user by only devise, you need to handle this feature outside devise (such as add session to user table or a devise hook something like timeoutable)
reference: https://github.com/heartcombo/devise/issues/5262
What you're looking for is the setting sign_in_after_change_password which you should set to false (it defaults to true). The method has a slightly confusing name – it means "should we sign the user in after changing their password" instead of "should the user have to sign in after changing their password".
References:
https://github.com/heartcombo/devise/blob/master/lib/devise.rb#L296-L298
https://github.com/heartcombo/devise/blob/c82e4cf47b02002b2fd7ca31d441cf1043fc634c/app/controllers/devise/registrations_controller.rb#L163-L167

Rails: Migrating to Auth0 while retaining postgres records and associations

My project currently uses has_secure_password to handle users (similar to this railscast). Everything works fine but looking down the road, I feel that a switch to Auth0 will provide benefits and would like to make the transition now before too many users sign up.
Users are saved to a User model with several associations (checkins/comments/etc.) How can I link an Auth0 user to a User record on my database. I figured I could either lookup by email (since they will be unique) or Auth0 User Id (and add a new column to the user table) once they login. It seems like I could simply use the Auth0 login callback to sign the user in (using my current sessions controller) but how can I create users on my database when they are created in OmniAuth. Is there a callback for that as well?
In a perfect world I would like to do something like:
User signs up using Auth0 lock. Creating a new account on the Auth0 database.
Callback triggers a new record to be made on my database that contains all the information from Auth0 and has columns for associations.
User logs in and their user_id (from my database) is stored in sessions.
Changes the user makes to personal information (name/email/etc.) is changed on both databases.
Is what I want possible? Or should I stick with my current authentication?
There is no major difference between implementing Auth0 authorization and any other OAuth provider such as Facebook, Twitter, Github etc. The Auth0 quickstart guide is just kind of strange since it does not show how you would link credentials to a user model in the application. Rather they just shove the whole shebang into the session.
The only real change you need to do to your user model here is add a uid column which is used to store the Auth0 uid for the user.
Basically the OAuth flow with OmniAuth works like this:
The user clicks a link that redirects the user to the providers OAuth portal. (Or in the case of Auth0 its a fancy JS Widget)
The provider redirects back to your application and it goes to either the callback or failure method on your callbacks controller.
At this point the user may be a new or returning user. So you try to find the user in your application based on the uid from the auth hash or create a new user from the auth hash.
You sign in and redirect the user.
class Auth0Controller < ActionController::Base
# the signed up or logged in on auth0
# GET "/auth/auth0/callback"
def callback
auth_hash = request.env['omniauth.auth']
#user = User.find_or_intialize_by(uid: auth_hash[:uid])
if #user.new_record?
#user.name = auth_hash.name
#user.email = auth_hash.email # etc
#user.save!
end
reset_session
session[:user_id] = #user.id
redirect_to root_path, success: "Hello #{#user.name}"
end
# the user declined to give your app permissions
# GET "/auth/failure"
def failure
redirect_to root_path, alert: "Oh noes"
end
end
See:
https://auth0.com/docs/quickstart/webapp/rails
https://github.com/omniauth/omniauth
https://auth0.com/docs/connections/database/migrating

Rails: Persist params through an Unauthorized request?

This Rails project has an API side to send data to the iOS version of the app. It uses Devise::Lockable to lock user accounts when they fail login 3 times.
This is a strange edge case, but maybe not that uncommon: The situation begins with being already logged in to the desktop version. If I then go fail login in the iOS app 3 times, but then click the reset password link in my email from the desktop version, because I'm already logged in, the request for the edit_password_url gets unauthorized (because the account is locked) and I get redirected to the login page.
What are some ways I could have it still go to the edit_password_url while keeping the params (specifically the password reset token)? I'm thinking either:
Skip authenticating the password edit page (seems bad, but I still haven't see how to do this)
Create some series of filters that check requests then redirect back
to the edit_password_url ... but how do I keep that reset password
token?
Alternatively, I could somehow force sign out of the user in the main
app somewhere in the process of that user failing login through the API. But these are namespaced and under different controllers and I haven't found a way to force a sign out that way.
There are several actions in devise where it redirects if there's an active session. I never liked this behavior and usually override it. If I hit a link, it's because I want to perform that action, after all.
For example, visiting the sign-in link, you might want to sign in as a different user, so you could override that action to sign out if it's hit directly, rather than redirecting. So for my example you would subclass Devise::SessionsController
class SessionsController < Devise::SessionsController
skip_before_filter :require_no_authentication, only: [:new]
def new
if warden.authenticated?(resource_name)
sign_out
end
super
end
private
def sign_out
# From Devise::SessionsController#destroy
Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)
end
end
and override it in routes:
devise_for :users, controllers: {sessions: 'sessions'}

Allow users to edit account, without id being in url

I know that in devise if you want to edit your account you can go to /registrations/edit to edit the account.
However I'm on a project where I'm just using omniauth. So I created a controller called
users_controller
that will allow the user to edit their account information etc. However if I do
edit_user_path
it want's me to give the id, how can I do this but still have a clean URL like devise does, where I don't have to make it users/:id/edit?
In devise, you don't need the id because the id is calculated by the current_user which is stored in session.
So, if you want to edit your user's profile without an id, you must ensure the id could be found somewhere, via session/cookie.
# set into session
session[:user_id] = 3
# read from session
User.find session[:user_id]
refer to: http://guides.rubyonrails.org/action_controller_overview.html#session

Rails: sign out logged in user on event

I'm using Rail3 with Devise gem. It does a great job when you need to lock user from signing in.
But it works just for new login attempts.
If he is already logged in - it won't sign him out immediately.
Here's is the typical use case:
Given admin user
when detects suspicious activity of certain user he locks it with malicious_user.lock('locking-reason')
% can config/initializers/session_store.rb
AppFoo::Application.config.session_store :cookie_store, :key => '_foo_session'
Given HTTP's statelessness, you can't immediately log out a user because you will need to wait until they make another request to your server. You could get around this via a push service I suppose, but that would be overkill.
My solution would be to add that person to a blacklist and then check if they're on the blacklist whenever they try to access a section intended for logged-on users only. This will render them unable to log on until you decide whether or not their activity is suspicious.
Example:
User is suspected of intolerable activity
Admin wants to check this out, so they temporarily add the user to the blacklist.
User clicks on an area of the page they were currently on when added to the blacklist.
Code checks for loggin status and blacklisted users.
Since the user is blacklisted, they are informed that they need to sign in to access the content
Once the user tries to sign in again you can inform them that their account has been temporarily disabled (or you can do this in the previous step).
perhaps the easiest way would be to redirect the user to the logout action when you lock them so:
malicious_user.lock('locking-reason')
redirect_to '/logout' and return
I'm not familiar with Devise so this may not be the best solution or even possible but it's how I would approach the problem
Use a before_filter in the ApplicationController that will do the following
before_filter :kick_out_blocked_user
protected
def kick_out_blocked_user
unless current_user.try(:active?)
redirect_to destroy_user_session_path
end
end

Resources