cleanup auth, start bot

This commit is contained in:
Evan Burkey 2025-01-27 10:36:57 -08:00
parent db22f4a942
commit 2d85fdb5cd
6 changed files with 50 additions and 18 deletions

View File

@ -1,4 +1,23 @@
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has. # Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
allow_browser versions: :modern 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 end

View File

@ -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

View File

@ -1,5 +1,6 @@
class GameController < ApplicationController class GameController < ApplicationController
skip_before_action :verify_authenticity_token, only: [ :create ] skip_before_action :verify_authenticity_token, only: [ :create ]
before_action :authenticate, only: [ :create ]
respond_to? :json respond_to? :json
def index def index
@ -11,11 +12,6 @@ class GameController < ApplicationController
end end
def create def create
unless authenticate_api_key?
unauthorized_response
return
end
game_params[:timestamp] = Time.at(params[:timestamp].to_i).utc if params[:timestamp].present? game_params[:timestamp] = Time.at(params[:timestamp].to_i).utc if params[:timestamp].present?
if Game.find_by(timestamp: game_params[:timestamp]) if Game.find_by(timestamp: game_params[:timestamp])
already_reported_response already_reported_response
@ -59,19 +55,6 @@ class GameController < ApplicationController
params.require(:game).permit(:timestamp, :gametype, :wager, :winner, :loser, :high_roll, :low_roll, :payout) params.require(:game).permit(:timestamp, :gametype, :wager, :winner, :loser, :high_roll, :low_roll, :payout)
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
def already_reported_response def already_reported_response
render json: { error: "Already reported this gambo" }, status: :already_reported render json: { error: "Already reported this gambo" }, status: :already_reported
end end

View File

@ -0,0 +1,2 @@
module BotHelper
end

View File

@ -22,4 +22,9 @@ Rails.application.routes.draw do
# Discord # Discord
match "discord" => redirect("https://discord.gg/hQShdPMy7p"), via: [ :get ] 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 end

View File

@ -0,0 +1,7 @@
require "test_helper"
class BotControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end