Perform private token and personal access token authentication in the same `before_action`.

- So that the check for valid personal access tokens happens only if
  private token auth fails.
This commit is contained in:
Timothy Andrew 2016-06-01 14:04:38 +05:30
parent 70add1388f
commit 05b319b0b4
2 changed files with 67 additions and 71 deletions

View File

@ -7,8 +7,7 @@ class ApplicationController < ActionController::Base
include GitlabRoutingHelper include GitlabRoutingHelper
include PageLayoutHelper include PageLayoutHelper
before_action :authenticate_user_from_private_token! before_action :authenticate_user_from_token!
before_action :authenticate_user_from_personal_access_token!
before_action :authenticate_user! before_action :authenticate_user!
before_action :validate_user_service_ticket! before_action :validate_user_service_ticket!
before_action :reject_blocked! before_action :reject_blocked!
@ -64,26 +63,8 @@ class ApplicationController < ActionController::Base
end end
end end
# From https://github.com/plataformatec/devise/wiki/How-To:-Simple-Token-Authentication-Example def authenticate_user_from_token!
# https://gist.github.com/josevalim/fb706b1e933ef01e4fb6 user = get_user_from_private_token || get_user_from_personal_access_token
def authenticate_user_from_private_token!
user_token = params[:private_token].presence || request.headers['PRIVATE-TOKEN'].presence
user = user_token && User.find_by_authentication_token(user_token.to_s)
if user
# Notice we are passing store false, so the user is not
# actually stored in the session and a token is needed
# for every request. If you want the token to work as a
# sign in token, you can simply remove store: false.
sign_in user, store: false
end
end
def authenticate_user_from_personal_access_token!
token_string = params[:private_token].presence || request.headers['PRIVATE-TOKEN'].presence
personal_access_token = PersonalAccessToken.active.find_by_token(token_string)
user = personal_access_token && personal_access_token.user
if user if user
# Notice we are passing store false, so the user is not # Notice we are passing store false, so the user is not
# actually stored in the session and a token is needed # actually stored in the session and a token is needed
@ -383,4 +364,17 @@ class ApplicationController < ActionController::Base
(controller_name == 'groups' && action_name == page_type) || (controller_name == 'groups' && action_name == page_type) ||
(controller_name == 'dashboard' && action_name == page_type) (controller_name == 'dashboard' && action_name == page_type)
end end
# From https://github.com/plataformatec/devise/wiki/How-To:-Simple-Token-Authentication-Example
# https://gist.github.com/josevalim/fb706b1e933ef01e4fb6
def get_user_from_private_token
user_token = params[:private_token].presence || request.headers['PRIVATE-TOKEN'].presence
User.find_by_authentication_token(user_token.to_s) if user_token
end
def get_user_from_personal_access_token
token_string = params[:private_token].presence || request.headers['PRIVATE-TOKEN'].presence
personal_access_token = PersonalAccessToken.active.find_by_token(token_string)
personal_access_token.user if personal_access_token
end
end end

View File

@ -31,7 +31,8 @@ describe ApplicationController do
end end
end end
describe "#authenticate_user_from_private_token!" do describe "#authenticate_user_from_token!" do
describe "authenticating a user from a private token" do
controller(ApplicationController) do controller(ApplicationController) do
def index def index
render text: "authenticated" render text: "authenticated"
@ -61,7 +62,7 @@ describe ApplicationController do
end end
end end
describe "#authenticate_user_from_personal_access_token!" do describe "authenticating a user from a personal access token" do
controller(ApplicationController) do controller(ApplicationController) do
def index def index
render text: 'authenticated' render text: 'authenticated'
@ -91,3 +92,4 @@ describe ApplicationController do
end end
end end
end end
end