Skip to content

Commit

Permalink
Replace api key with token per query
Browse files Browse the repository at this point in the history
  • Loading branch information
abuisman committed Sep 11, 2023
1 parent 1e2628a commit bab1890
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 19 deletions.
11 changes: 6 additions & 5 deletions app/controllers/blazer/queries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ def edit
end

def share
if params[:token] && params[:query_id] && params[:token] == Blazer.sharing.query_token(params[:query_id])
run
else
render_forbidden
end
return render_forbidden unless params[:token] && params[:query_id]

@query = Query.find_by(id: params[:query_id]) if params[:query_id]
return render_forbidden unless @query.correct_token?(params[:token])

run
end

def run
Expand Down
6 changes: 6 additions & 0 deletions app/models/blazer/query.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Blazer
class Query < Record
has_secure_token :secret_token, length: 36

belongs_to :creator, optional: true, class_name: Blazer.user_class.to_s if Blazer.user_class
has_many :checks, dependent: :destroy
has_many :dashboard_queries, dependent: :destroy
Expand All @@ -15,6 +17,10 @@ def to_param
[id, name].compact.join("-").gsub("'", "").parameterize
end

def correct_token?(token)
ActiveSupport::SecurityUtils.secure_compare(secret_token, token)
end

def friendly_name
name.to_s.sub(/\A[#\*]/, "").gsub(/\[.+\]/, "").strip
end
Expand Down
17 changes: 7 additions & 10 deletions lib/blazer/sharing.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module Blazer
class Sharing
attr_accessor :api_key, :path
attr_accessor :path, :enabled

def initialize(api_key: ENV.fetch('BLAZER_DOWNLOAD_API_KEY', nil), path: '/blazer_share')
@api_key = api_key
def initialize(enabled: false, path: '/blazer_share')
@path = path.sub(/\/$/, '') # Strip trailing /
@enabled = enabled
end

def route_path
Expand All @@ -15,16 +15,13 @@ def to_controller
'blazer/queries#share'
end

def query_token(query_id)
Digest::SHA1.hexdigest("#{query_id}-#{api_key}")
end

def enabled?
api_key.present?
enabled
end

def share_path(query_id, format: nil)
"#{path}/#{query_token(query_id)}/#{query_id}#{".#{format}" if format}"
def share_path(query_id, format: nil, token: nil)
query = Query.find(query_id)
"#{path}/#{token}/#{query_id}#{".#{format}" if format}"
end

def url_for(query_id, current_url, format: 'csv')
Expand Down
1 change: 1 addition & 0 deletions lib/generators/blazer/templates/install.rb.tt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version
t.string :name
t.text :description
t.text :statement
t.text :secret_token
t.string :data_source
t.string :status
t.timestamps null: false
Expand Down
4 changes: 4 additions & 0 deletions test/internal/config/blazer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,7 @@ uploads:
url: postgres://localhost/blazer_test
schema: uploads
data_source: main

sharing:
path: /blazer_share
enabled: true
2 changes: 2 additions & 0 deletions test/internal/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Rails.application.routes.draw do
mount Blazer::Engine, at: "/"

get Blazer.sharing.route_path, to: Blazer.sharing.to_controller, as: :share_query if Blazer.sharing.enabled?
end
1 change: 1 addition & 0 deletions test/internal/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
t.string :name
t.text :description
t.text :statement
t.text :secret_token
t.string :data_source
t.string :status
t.timestamps null: false
Expand Down
24 changes: 20 additions & 4 deletions test/queries_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ def test_variables_time_range
assert_match "daterangepicker", response.body
end

def test_correct_token
query = create_query(statement: "SELECT 1")
get share_query_path(query.id, token: query.secret_token, format: 'csv')

assert_response :success
assert_equal "text/csv", response.content_type
end

def test_incorrect_token
query = create_query(statement: "SELECT 1")
get share_query_path(query.id, token: "x")

assert_response :forbidden
assert_match "Access denied", response.body
end

def test_variable_defaults
query = create_query(statement: "SELECT {default_var}")
get blazer.query_path(query)
Expand Down Expand Up @@ -108,12 +124,12 @@ def test_csv
end

def test_share
Blazer.sharing.api_key = "123"
query = create_query
get blazer.query_share_path(query_id: query.id, token: Digest::SHA1.hexdigest("#{query.id}-123"), format: 'csv')
assert query.secret_token

get blazer.query_share_path(query_id: query.id, token: query.secret_token, format: 'csv')

assert_response :success
assert_match query.name, response.body
Blazer.sharing.api_key = nil
end

def test_url
Expand Down

0 comments on commit bab1890

Please sign in to comment.