42 lines
1.1 KiB
Ruby
Executable File
42 lines
1.1 KiB
Ruby
Executable File
module TokenVerification
|
|
extend ActiveSupport::Concern
|
|
include ActionController::HttpAuthentication::Token::ControllerMethods
|
|
|
|
included do
|
|
before_action :authenticate
|
|
end
|
|
|
|
|
|
protected
|
|
# Authenticate the user with token based authentication
|
|
def authenticate
|
|
authenticate_token || render_unauthorized
|
|
end
|
|
|
|
def authenticate_token
|
|
authenticate_with_http_token do |token, options|
|
|
# Rails.logger.debug "token - " + token.to_s
|
|
if(!options.from.nil? && options.from == "DOEMAL"){
|
|
if(ENV["SERVER_MODE"] === "cloud"){
|
|
from = request.subdomain.downcase + "." + request.domain.downcase
|
|
aes = MyAesCrypt.new
|
|
return aes.checkKeyForAuth(from, token)
|
|
}
|
|
}
|
|
|
|
@user = Employee.authenticate_by_token(token)
|
|
if @user
|
|
return true
|
|
#Maybe log - login?
|
|
end
|
|
|
|
end
|
|
end
|
|
|
|
def render_unauthorized(realm = "Application")
|
|
self.headers["WWW-Authenticate"] = %(Token realm="#{realm.gsub(/"/, "")}")
|
|
render json: 'Bad credentials', status: :unauthorized
|
|
end
|
|
|
|
end
|