refactor: try base controller
This commit is contained in:
parent
40ed59a231
commit
f434c6a6ad
1 changed files with 10 additions and 28 deletions
|
@ -1,15 +1,15 @@
|
||||||
class API::V2::BaseController < ApplicationController
|
class API::V2::BaseController < ApplicationController
|
||||||
# Disable forgery protection for API controllers when the request is authenticated
|
# Disable forgery protection for API controllers when the request is authenticated
|
||||||
# with a bearer token. Otherwise the session will be nullified and we'll lose curent_user
|
# with a bearer token. Otherwise the session will be nullified and we'll lose curent_user
|
||||||
protect_from_forgery with: :null_session, unless: :token?
|
skip_forgery_protection if: -> { request.headers.key?('HTTP_AUTHORIZATION') }
|
||||||
skip_before_action :setup_tracking
|
skip_before_action :setup_tracking
|
||||||
prepend_before_action :authenticate_administrateur_from_token
|
before_action :authenticate_from_token
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def context
|
def context
|
||||||
if api_token.present?
|
if @api_token.present?
|
||||||
api_token.context
|
@api_token.context
|
||||||
# web interface (/graphql) give current_administrateur
|
# web interface (/graphql) give current_administrateur
|
||||||
elsif current_administrateur.present?
|
elsif current_administrateur.present?
|
||||||
{
|
{
|
||||||
|
@ -20,32 +20,14 @@ class API::V2::BaseController < ApplicationController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def token?
|
private
|
||||||
authorization_bearer_token.present?
|
|
||||||
end
|
|
||||||
|
|
||||||
def authenticate_administrateur_from_token
|
def authenticate_from_token
|
||||||
if api_token.present?
|
@api_token = authenticate_with_http_token { |t, _o| APIToken.authenticate(t) }
|
||||||
@current_user = api_token.administrateur.user
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def api_token
|
if @api_token.present?
|
||||||
if @api_token.nil?
|
@api_token.touch(:last_v2_authenticated_at)
|
||||||
@api_token = APIToken
|
@current_user = @api_token.administrateur.user
|
||||||
.authenticate(authorization_bearer_token)
|
|
||||||
&.tap { _1.touch(:last_v2_authenticated_at) } || false
|
|
||||||
end
|
|
||||||
@api_token
|
|
||||||
end
|
|
||||||
|
|
||||||
def authorization_bearer_token
|
|
||||||
@authorization_bearer_token ||= begin
|
|
||||||
received_token = nil
|
|
||||||
authenticate_with_http_token do |token, _options|
|
|
||||||
received_token = token
|
|
||||||
end
|
|
||||||
received_token
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue