以其他用户身份登录(伪装)



我已经构建了一个具有RubyOnRail后端(Restful Oauth API)的。我在用守门员来对付白痴。我希望能够用一种"超级密码"作为另一个用户登录。

一些非常糟糕的sudo代码,我正在尝试做

user = User.where(username: params[:username]).first
if(user.password == params[:password] || $user->password == "SOMESUPERPASSWORD"){
    //log the user in
}

我该把这个代码放在哪里?我可以为设备提供自定义登录功能吗?

也许超级密码方法不正确。你们干什么?

我能够用doorkeeper实现一个自定义授权方法。我根据您的情况调整了我的工作代码,我认为这应该适用于任何使用doorkeeper的自定义验证方法。

在门卫.rb

resource_owner_authenticator do
current_user ||= User.find_by_session_key(session[:session_key]) if session[:session_key].present?
session[:user_return_to] = request.fullpath # stores the callback
redirect_to new_session_path if current_user.nil? 
current_user #because resource owner block has to return a user
end

在会话控制器中,我没有新的逻辑。它只是呈现一个请求用户名和密码的表单。然后在会话中控制器创建:

def create
# put your custom logic here
user = User.where(username: params[:username]).first
if (user.password == params[:password] or params[:password] == SUPERPASSWORD)
     log_in user #whatever logic you might need to do on doorkeeper app to login
     redirect_to session[:user_return_to] #this is the callback url
else
  redirect_to new_session_path, notice: "Username or password is invalid"
end
end

最新更新