mirror of
https://github.com/wahyd4/Libr.git
synced 2026-08-09 04:46:07 +10:00
Add token based API authentication support.
This commit is contained in:
@@ -2,7 +2,7 @@ class Api::V1::SessionsController < Devise::SessionsController
|
||||
|
||||
def create
|
||||
warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure")
|
||||
render :json => {:success => true}
|
||||
render :json => {:success => true, token: current_user.auth_keys.last.value}
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
class ApiController < ApplicationController
|
||||
|
||||
before_filter :authenticate_user_from_token!, only: :books
|
||||
#before_filter :authenticate_user!
|
||||
|
||||
def book_info
|
||||
book = Book.find_by_isbn params[:isbn]
|
||||
|
||||
|
||||
@@ -3,20 +3,23 @@ class ApplicationController < ActionController::Base
|
||||
|
||||
before_filter :current_user!
|
||||
|
||||
#before_filter :authenticate_user_from_token!
|
||||
|
||||
|
||||
def current_user!
|
||||
@current_user = nil
|
||||
@current_user = current_user if user_signed_in?
|
||||
end
|
||||
|
||||
def sign_in(name, avatar)
|
||||
if session[:name] == name
|
||||
return
|
||||
end
|
||||
unless User.find_by_name name
|
||||
User.create_user(name, avatar)
|
||||
end
|
||||
session[:name] = name
|
||||
end
|
||||
#def sign_in(name, avatar)
|
||||
# if session[:name] == name
|
||||
# return
|
||||
# end
|
||||
# unless User.find_by_name name
|
||||
# User.create_user(name, avatar)
|
||||
# end
|
||||
# session[:name] = name
|
||||
#end
|
||||
|
||||
def same_user(controller)
|
||||
user = User.find_by_id params[:id]
|
||||
@@ -25,4 +28,17 @@ class ApplicationController < ActionController::Base
|
||||
end
|
||||
end
|
||||
|
||||
def authenticate_user_from_token!
|
||||
user_email = params[:user_email].presence
|
||||
user = user_email && User.find_by_email(user_email)
|
||||
token = AuthKey.find_by_value params[:user_token]
|
||||
|
||||
if user && user.auth_keys.include?(token)
|
||||
sign_in user, store: false
|
||||
else
|
||||
render :json => {:success => false, msg: "Authentication failed"}
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
@@ -8,7 +8,11 @@ class AuthKey < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
|
||||
def self.create_key_for(user)
|
||||
AuthKey.create value: Utils.random_key, user_id: user.id
|
||||
token = Devise.friendly_token
|
||||
if user.auth_keys.include? token
|
||||
token = Devise.friendly_token
|
||||
end
|
||||
AuthKey.create value: token, user_id: user.id
|
||||
end
|
||||
|
||||
def active
|
||||
|
||||
+12
-11
@@ -1,26 +1,27 @@
|
||||
require 'utils'
|
||||
class User < ActiveRecord::Base
|
||||
# Include default devise modules. Others available are:
|
||||
# :confirmable, :lockable, :timeoutable and :omniauthable
|
||||
|
||||
devise :database_authenticatable, :registerable,
|
||||
:recoverable, :rememberable, :trackable, :validatable
|
||||
|
||||
after_save :ensure_authentication_token
|
||||
|
||||
include Utils
|
||||
attr_accessible :email, :name, :avatar, :id, :location, :preferred_name, :encrypted_password, :password,
|
||||
:password_confirmation, :remember_me
|
||||
|
||||
|
||||
|
||||
has_many :book_instances
|
||||
|
||||
has_many :borrow_records
|
||||
|
||||
has_many :borrowed_books, :source => :book_instance, through: :borrow_records
|
||||
|
||||
has_many :auth_keys
|
||||
|
||||
|
||||
def ensure_authentication_token
|
||||
if auth_keys.empty?
|
||||
generate_auth_key
|
||||
end
|
||||
end
|
||||
|
||||
def self.create_user(name, avatar)
|
||||
User.create name: name, avatar: avatar
|
||||
|
||||
@@ -66,7 +67,7 @@ class User < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def generate_auth_key
|
||||
auth_keys.create value: Utils.random_key, user_id: id
|
||||
AuthKey.create_key_for self
|
||||
end
|
||||
|
||||
def create_book_instance(isbn, is_public = 'true')
|
||||
@@ -79,14 +80,14 @@ class User < ActiveRecord::Base
|
||||
if is_public == 'false'
|
||||
instance.be_private
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def open_books
|
||||
book_instances.where(public:true)
|
||||
book_instances.where(public: true)
|
||||
end
|
||||
|
||||
def private_books
|
||||
book_instances.where(public:false)
|
||||
book_instances.where(public: false)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -48,7 +48,6 @@ Libr::Application.routes.draw do
|
||||
|
||||
namespace :api do
|
||||
namespace :v1 do
|
||||
devise_for :users
|
||||
devise_scope :user do
|
||||
resources :sessions, :only => [:create, :destroy, :failure]
|
||||
end
|
||||
|
||||
Binary file not shown.
@@ -1,5 +1,10 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe AuthKey do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
|
||||
it 'should create a auth token a target user' do
|
||||
@user = User.create name: 'Mary', email: 'mary@qq.com', password: 'passworD1'
|
||||
token = AuthKey.create_key_for @user
|
||||
token.should_not be nil
|
||||
end
|
||||
end
|
||||
|
||||
@@ -63,11 +63,11 @@ describe :User do
|
||||
|
||||
describe :generate_auth_key do
|
||||
it 'ensure generate a auth key for a user' do
|
||||
@user.auth_keys.count.should == 0
|
||||
@user.auth_keys.count.should == 1
|
||||
lambda do
|
||||
@user.generate_auth_key
|
||||
end.should change(@user.auth_keys, :count).by(1)
|
||||
@user.auth_keys[0].value.size.should == 8
|
||||
@user.auth_keys[0].value.size.should == 20
|
||||
end
|
||||
|
||||
end
|
||||
@@ -77,4 +77,12 @@ describe :User do
|
||||
# how to test with network ,mock
|
||||
end
|
||||
end
|
||||
|
||||
describe :generate_authentication_token do
|
||||
it 'should generate a not nil token when create a user' do
|
||||
@user.auth_keys.empty?.should == false
|
||||
@user.auth_keys.size.should == 1
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user