From 2d85fdb5cd5ad0b8b9d8ff332ebc72924178171b Mon Sep 17 00:00:00 2001 From: Evan Burkey Date: Mon, 27 Jan 2025 10:36:57 -0800 Subject: [PATCH] cleanup auth, start bot --- app/controllers/application_controller.rb | 19 +++++++++++++++++++ app/controllers/bot_controller.rb | 16 ++++++++++++++++ app/controllers/game_controller.rb | 19 +------------------ app/helpers/bot_helper.rb | 2 ++ config/routes.rb | 5 +++++ test/controllers/bot_controller_test.rb | 7 +++++++ 6 files changed, 50 insertions(+), 18 deletions(-) create mode 100644 app/controllers/bot_controller.rb create mode 100644 app/helpers/bot_helper.rb create mode 100644 test/controllers/bot_controller_test.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0d95db2..95ff55b 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,4 +1,23 @@ class ApplicationController < ActionController::Base # Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has. allow_browser versions: :modern + + def authenticate + unless authenticate_api_key? + unauthorized_response + end + end + + def authenticate_api_key? + api_key = request.headers["X-API-KEY"] + if api_key.present? && ApiKey.exists?(key: api_key) + @apikey = ApiKey.find_by(key: api_key) + return true + end + false + end + + def unauthorized_response + render json: { error: "API key is missing or empty" }, status: :unauthorized + end end diff --git a/app/controllers/bot_controller.rb b/app/controllers/bot_controller.rb new file mode 100644 index 0000000..58093b4 --- /dev/null +++ b/app/controllers/bot_controller.rb @@ -0,0 +1,16 @@ +class BotController < ApplicationController + skip_before_action :verify_authenticity_token + before_action :authenticate + + def players + + end + + def summary + + end + + def player_name + + end +end diff --git a/app/controllers/game_controller.rb b/app/controllers/game_controller.rb index 242ea2e..d49ab76 100644 --- a/app/controllers/game_controller.rb +++ b/app/controllers/game_controller.rb @@ -1,5 +1,6 @@ class GameController < ApplicationController skip_before_action :verify_authenticity_token, only: [ :create ] + before_action :authenticate, only: [ :create ] respond_to? :json def index @@ -11,11 +12,6 @@ class GameController < ApplicationController end def create - unless authenticate_api_key? - unauthorized_response - return - end - game_params[:timestamp] = Time.at(params[:timestamp].to_i).utc if params[:timestamp].present? if Game.find_by(timestamp: game_params[:timestamp]) already_reported_response @@ -59,19 +55,6 @@ class GameController < ApplicationController params.require(:game).permit(:timestamp, :gametype, :wager, :winner, :loser, :high_roll, :low_roll, :payout) end - def authenticate_api_key? - api_key = request.headers["X-API-KEY"] - if api_key.present? && ApiKey.exists?(key: api_key) - @apikey = ApiKey.find_by(key: api_key) - return true - end - false - end - - def unauthorized_response - render json: { error: "API key is missing or empty" }, status: :unauthorized - end - def already_reported_response render json: { error: "Already reported this gambo" }, status: :already_reported end diff --git a/app/helpers/bot_helper.rb b/app/helpers/bot_helper.rb new file mode 100644 index 0000000..1ea1fe0 --- /dev/null +++ b/app/helpers/bot_helper.rb @@ -0,0 +1,2 @@ +module BotHelper +end diff --git a/config/routes.rb b/config/routes.rb index 1dfd697..57f4b1f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -22,4 +22,9 @@ Rails.application.routes.draw do # Discord match "discord" => redirect("https://discord.gg/hQShdPMy7p"), via: [ :get ] + + # Bot + get "bot/players" => "bot#players" + get "bot/player/:id" => "bot#player_name" + get "bot/summary" => "bot#summary" end diff --git a/test/controllers/bot_controller_test.rb b/test/controllers/bot_controller_test.rb new file mode 100644 index 0000000..59c8d73 --- /dev/null +++ b/test/controllers/bot_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class BotControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end