使用bcrypt进行Rails身份验证错误

时间:2016-07-21 05:37:20

标签: ruby-on-rails ruby api bcrypt

我有一个我想在rails app API上显示的用户列表。我使用bcrypt来哈希密码。我创建并检查了我在rails console上有几个用户的列表:

List<Intake_Details__c> Intake_detailNmbr = [SELECT name FROM Intake_Details__c]; 

List<Claim__c> Claim_detailNmbr = [SELECT name FROM Claim__c];

我在localhost上测试它。我的目标是转到2.2.2 :010 > User.all User Load (8.9ms) SELECT "users".* FROM "users" => #<ActiveRecord::Relation [#<User id: 2, created_at: "2016-07-19 06:23:35", updated_at: "2016-07-19 06:23:35", username: "iggy1", encrypted_password: "", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, password_digest: "$2a$10$We2V5sFx3XJNHP9iHTx.5udLA9hEbJVDOcA01nemj0R...">, ,并以json格式列出所有用户。

当我访问该网站时,系统会提示用户名和密码。我输入了用户的用户名和密码之一:“iggy1”,“helloworld”。我看到此错误消息:

http://localhost:3000/api/users

我的架构如下:

SQLite3::SQLException: no such column: users.password: SELECT "users".* FROM "users" WHERE "users"."username" = ? AND "users"."password" = 'helloworld'

我尝试了一下它。我按照bcrypt website上给出的代码。

以下是我的代码:

create_table "users", force: :cascade do |t|
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.string   "username"
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.string   "password_digest"
  end

现在,如果我对“bcrypt”行添加了未注释的内容,那么当我转到#controller/api/users_controller.rb class UsersController < ApplicationController def create @user = User.new(params[:user]) @user.password = params[:password] @user.save! end end #controllers/users_controller.rb class UsersController < ApplicationController def create @user = User.new(params[:user]) @user.password = params[:password] @user.save! end end #models/user.rb require 'bcrypt' class User < ActiveRecord::Base #include 'bcrypt' has_many :lists has_many :items, through: :lists has_secure_password def password @password ||= Password.new(password_hash) end def password=(new_password) @password = Password.create(new_password) self.password_hash = @password end end #controllers/application_controller.rb class ApiController < ApplicationController skip_before_action :verify_authenticity_token private def authenticated? authenticate_or_request_with_http_basic {|username, password| User.where( username: username, password: password).present? } end end #config routes Rails.application.routes.draw do namespace :api, defaults: { format: :json} do #supports JSON requests resources :users end end

时,我会看到一个不同的错误
http://localhost:3000/api/users

我有几个关于为什么会发生这种情况的假设。

首先,我认为这是因为我使用了bcrypt,并且它有password_digest。我没有在我的架构上专门有wrong argument type String (expected Module) (在应用程序控制器上它明确地说明了用户名和密码)。我认为rails可能试图寻找“密码”但却找不到它。我不知道bcrypt足以说明情况就是这样。

其次,在bcrypt website上,我没有实现以下代码,因为我不确定将它放在何处。这可能是bcrypt不按我的意思行事吗?

password

我不确定缺少什么。

1 个答案:

答案 0 :(得分:1)

DB Schema中有两个可用于密码的字段

如果您使用Devise进行身份验证,则encrypted_password字段用于存储密码。