devise中如何使用auth_token認證 與 RubyChina api認證的區別
1. 修改配置檔案 config/initializers/devise.rb
config.token_authentication_key = :auth_token
2. 修改controller action
class Users::SessionsController < Devise::SessionsController
def create
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
set_flash_message(:notice, :signed_in) if is_navigational_format?
sign_in(resource_name, resource)
respond_to do |format|
format.html do
respond_with resource, :location => redirect_location(resource_name, resource)
end
format.json do
render :json => { :response => 'ok', :auth_token => current_user.authentication_token }.to_json, :status => :ok
end
end
end
end
3, 可以使用如下的命令測試
curl -X POST 'http://localhost:3000/users/sign_in.json' -d 'user[email]=example@example.com&user[password]=password'
-> {"response":"ok","auth_token":"ABCDE0123456789"}
curl -L 'http://localhost:3000/profile?auth_token=ABCDE0123456789'
-> got page that I wanted that needs authentication
devise 提供的auth token 方式不是很合適,需要修改devise.rb配置檔案,與web認證衝突
還是RubyChina實現的比較好一點
Ruby-China的程式碼中如何實現tokenauthentication登入
1, 登入的時候生成private_token程式碼
# 使用者金鑰,用於客戶端驗證
field :private_token
# 重新生成 Private Token
def update_private_token
random_key = "#{SecureRandom.hex(10)}:#{self.id}"
self.update_attribute(:private_token, random_key)
end
2, 在客戶端登入的時候,返回 private_token
def create
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
set_flash_message(:notice, :signed_in) if is_navigational_format?
sign_in(resource_name, resource)
respond_to do |format|
format.html { redirect_to after_sign_in_path_for(resource) }
format.json { render :status => '201', :json => resource.as_json(:only => [:login, :email, :private_token]) }
end
end
3, 可以使用如下命令測試
curl -X POST 'http://ruby-china.org/account/sign_in.json' -d "user[login]=xxxx&user[password]=xxxxxx"
4, 如何在下次介面定義中使用?
# file path: lib/api.rb
#
# Post a new topic
# require authentication
# params:
# title
# body
# node_id
post do
authenticate!
@topic = current_user.topics.new(:title => params[:title], :body => params[:body])
@topic.node_id = params[:node_id]
@topic.save!
#TODO error handling
end
5, authenticate!的原始碼【在lib/api/helper.rb檔案中】
# user helpers
def current_user
@current_user ||= User.where(:private_token => params[:token] || '').first
end
def authenticate!
error!({ "error" => "401 Unauthorized" }, 401) unless current_user
end
RubyChina沒有使用Devise預設的認證碼,定義了一套新的認證機制, 結合 Grape 做介面,與 web層
config.token_authentication_key = :auth_token
2. 修改controller action
class Users::SessionsController < Devise::SessionsController
def create
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
set_flash_message(:notice, :signed_in) if is_navigational_format?
sign_in(resource_name, resource)
respond_to do |format|
format.html do
respond_with resource, :location => redirect_location(resource_name, resource)
end
format.json do
render :json => { :response => 'ok', :auth_token => current_user.authentication_token }.to_json, :status => :ok
end
end
end
end
3, 可以使用如下的命令測試
curl -X POST 'http://localhost:3000/users/sign_in.json' -d 'user[email]=example@example.com&user[password]=password'
-> {"response":"ok","auth_token":"ABCDE0123456789"}
curl -L 'http://localhost:3000/profile?auth_token=ABCDE0123456789'
-> got page that I wanted that needs authentication
devise 提供的auth token 方式不是很合適,需要修改devise.rb配置檔案,與web認證衝突
還是RubyChina實現的比較好一點
Ruby-China的程式碼中如何實現tokenauthentication登入
1, 登入的時候生成private_token程式碼
# 使用者金鑰,用於客戶端驗證
field :private_token
# 重新生成 Private Token
def update_private_token
random_key = "#{SecureRandom.hex(10)}:#{self.id}"
self.update_attribute(:private_token, random_key)
end
2, 在客戶端登入的時候,返回 private_token
def create
resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#new")
set_flash_message(:notice, :signed_in) if is_navigational_format?
sign_in(resource_name, resource)
respond_to do |format|
format.html { redirect_to after_sign_in_path_for(resource) }
format.json { render :status => '201', :json => resource.as_json(:only => [:login, :email, :private_token]) }
end
end
3, 可以使用如下命令測試
curl -X POST 'http://ruby-china.org/account/sign_in.json' -d "user[login]=xxxx&user[password]=xxxxxx"
4, 如何在下次介面定義中使用?
# file path: lib/api.rb
#
# Post a new topic
# require authentication
# params:
# title
# body
# node_id
post do
authenticate!
@topic = current_user.topics.new(:title => params[:title], :body => params[:body])
@topic.node_id = params[:node_id]
@topic.save!
#TODO error handling
end
5, authenticate!的原始碼【在lib/api/helper.rb檔案中】
# user helpers
def current_user
@current_user ||= User.where(:private_token => params[:token] || '').first
end
def authenticate!
error!({ "error" => "401 Unauthorized" }, 401) unless current_user
end
RubyChina沒有使用Devise預設的認證碼,定義了一套新的認證機制, 結合 Grape 做介面,與 web層
相關文章
- Basic認證和Bearer Token認證的區別
- Oracle中兩種認證方式:OS認證與口令檔案認證Oracle
- Lumen 8.0 使用 Jwt 認證的 ApiJWTAPI
- 多因子認證是什麼意思?與雙因子認證有什麼區別?
- Laravel——Api認證之Passport使用LaravelAPIPassport
- Laravel Passport API 認證使用小結LaravelPassportAPI
- 蘋果兩步驗證雙重認證區別 蘋果雙重認證是什麼?蘋果
- 使用OpenSSH證書認證
- 【認證與授權】2、基於session的認證方式Session
- 選擇檢測機構,有CNAS認證和沒CNAS認證的區別
- Oracle OS認證與口令檔案認證詳解Oracle
- 細說API - 認證、授權和憑證API
- ILSSI認證和一般的證書有什麼區別?
- passport API 認證 -- 多表登入PassportAPI
- Solon Auth 認證框架使用演示(更簡單的認證框架)框架
- Laravel使用JWT來建立使用者認證APILaravelJWTAPI
- Laravel 5.5 使用 Passport 服務做 API 認證LaravelPassportAPI
- 如何獲得PMP認證證書
- 福祿克的驗證測試和認證測試的區別
- JAAS中的認證與授權問題
- 圖解Jwt和shiro認證方式的區別圖解JWT
- HTTP認證之基本認證——Basic(一)HTTP
- HTTP認證之基本認證——Basic(二)HTTP
- 【認證與授權】Spring Security系列之認證流程解析Spring
- Laravel5.3 Passport API 認證 密碼模式使用LaravelPassportAPI密碼模式
- Yii2.0 RESTful API 認證教程RESTAPI
- REST API簽名認證機制RESTAPI
- Django REST framework API 指南(13):認證DjangoRESTFrameworkAPI
- mongodb的使用者認證MongoDB
- 認證系統之登入認證系統的進階使用 (二)
- 如何使用 SAP API Portal Policy Editor 給 SAP API 呼叫自動新增認證資訊API
- devise使用人機驗證dev
- 認證授權方案之JwtBearer認證JWT
- HTTP認證之摘要認證——Digest(一)HTTP
- Laravel 認證原理及完全自定義認證Laravel
- 網路身份認證——Kerberos配置及認證ROS
- 計算機認證公司的認證網址 (轉)計算機
- 作業系統認證與ORACLE密碼檔案認證方式作業系統Oracle密碼