[refs #374] Add custom http basic auth for api

This commit is contained in:
konstantin.grabar 2012-09-21 23:15:48 +04:00
parent 5bcebcf384
commit 7812adfdbc
10 changed files with 32 additions and 8 deletions

View File

@ -396,11 +396,11 @@ module GitHub
}
ERROR_AUTH = {
"error" => "You need to sign in or sign up before continuing."
"message" => "You need to sign in or sign up before continuing."
}
ERROR_WRONG_PASS = {
"error" => "Invalid email or password."
"message" => "Invalid email or password."
}
ERROR_RATE_LIMIT = {

View File

@ -1,5 +1,6 @@
# -*- encoding : utf-8 -*-
class Api::V1::BaseController < ApplicationController
before_filter :http_auth
before_filter :restrict_paginate, :only => :index
protected
@ -8,4 +9,12 @@ class Api::V1::BaseController < ApplicationController
params[:per_page] = 30 if params[:per_page].blank? or params[:per_page].to_i < 1
params[:per_page] = 100 if params[:per_page].to_i >100
end
def http_auth
authenticate_or_request_with_http_basic do |email, password|
raise HttpBasicAuthError if email.blank? && password.blank?
@current_user = User.find_by_email(email)
@current_user && @current_user.valid_password?(password) ? true : raise(HttpBasicWrongPassError)
end
end
end

View File

@ -1,7 +1,7 @@
# -*- encoding : utf-8 -*-
class Api::V1::BuildListsController < Api::V1::BaseController
before_filter :authenticate_user!
skip_before_filter :authenticate_user!, :only => [:show, :index] if APP_CONFIG['anonymous_access']
#before_filter :authenticate_user!
#skip_before_filter :authenticate_user!, :only => [:show, :index] if APP_CONFIG['anonymous_access']
load_and_authorize_resource :project, :only => :index
load_and_authorize_resource :build_list, :only => [:show, :create, :cancel, :publish, :reject_publish]#, :shallow => true

View File

@ -1,8 +1,8 @@
# -*- encoding : utf-8 -*-
class Api::V1::PlatformsController < Platforms::BaseController
before_filter :authenticate_user!
skip_before_filter :authenticate_user!, :only => [:advisories] if APP_CONFIG['anonymous_access']
#before_filter :authenticate_user!
#skip_before_filter :authenticate_user!, :only => [:advisories] if APP_CONFIG['anonymous_access']
load_and_authorize_resource
autocomplete :user, :uname

View File

@ -1,6 +1,6 @@
# -*- encoding : utf-8 -*-
class Api::V1::ProjectsController < Api::V1::BaseController
before_filter :authenticate_user!
#before_filter :authenticate_user!
load_and_authorize_resource
def get_id

View File

@ -1,6 +1,6 @@
# -*- encoding : utf-8 -*-
class Api::V1::RepositoriesController < Api::V1::BaseController
before_filter :authenticate_user!
#before_filter :authenticate_user!
load_and_authorize_resource :repository, :through => :platform, :shallow => true
end

View File

@ -40,6 +40,13 @@ class ApplicationController < ActionController::Base
end
end
rescue_from HttpBasicAuthError do |exception|
render :json => {:message => t("flash.http_basic_error_msg")}.to_json, :status => 401
end
rescue_from HttpBasicWrongPassError do |exception|
render :json => {:message => t("flash.http_basic_wrong_pass_error_message")}.to_json, :status => 401
end
rescue_from Grit::NoSuchPathError, :with => :not_found
protected

View File

@ -0,0 +1,4 @@
class HttpBasicWrongPassError < StandardError
end
class HttpBasicAuthError < StandardError
end

View File

@ -138,6 +138,8 @@ en:
500_message: Error 500. Something went wrong. We've been notified about this issue and we'll take a look at it shortly.
404_message: Error 404. Resource not found!
http_basic_auth_error_message: You need to sign in or sign up before continuing
http_basic_wrong_pass_error_message: Invalid email or password
collaborators:
successfully_changed: Collaborators list successfully changed

View File

@ -138,6 +138,8 @@ ru:
500_message: Ошибка 500. Что-то пошло не так. Мы уже в курсе данной проблемы и постараемся поскорее ее решить.
404_message: Ошибка 404. Страница не найдена!
http_basic_auth_error_message: Вы должны авторизоваться или зарегестрироваться
http_basic_wrong_pass_error_message: Неверный имейл или пароль
collaborators:
successfully_changed: Список коллабораторов успешно изменен