diff --git a/app/controllers/game_controller.rb b/app/controllers/game_controller.rb index dc5e13e..d3f686a 100644 --- a/app/controllers/game_controller.rb +++ b/app/controllers/game_controller.rb @@ -9,9 +9,15 @@ 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]) - return head 208 # HTTP 208: Already reported + already_reported_response + return end players = [] @@ -25,6 +31,7 @@ class GameController < ApplicationController end @game = Game.create(game_params) + @game.client = @apikey.client if @game.save players.each do | player | @@ -38,9 +45,9 @@ class GameController < ApplicationController player.save end end - head :ok + render json: {}, status: :ok else - head :unprocessable_entity + render json: { error: "Unable to process input" }, status: :unprocessable_content end end @@ -49,4 +56,21 @@ class GameController < ApplicationController def game_params 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 end diff --git a/app/models/api_key.rb b/app/models/api_key.rb new file mode 100644 index 0000000..f44edba --- /dev/null +++ b/app/models/api_key.rb @@ -0,0 +1,2 @@ +class ApiKey < ApplicationRecord +end diff --git a/db/migrate/20250123200608_create_games.rb b/db/migrate/20250123200608_create_games.rb index f94ee23..4ba7f3b 100644 --- a/db/migrate/20250123200608_create_games.rb +++ b/db/migrate/20250123200608_create_games.rb @@ -9,6 +9,7 @@ class CreateGames < ActiveRecord::Migration[8.0] t.integer :high_roll t.integer :low_roll t.integer :payout + t.string :client t.timestamps end diff --git a/db/migrate/20250124201602_create_api_keys.rb b/db/migrate/20250124201602_create_api_keys.rb new file mode 100644 index 0000000..913c3cb --- /dev/null +++ b/db/migrate/20250124201602_create_api_keys.rb @@ -0,0 +1,10 @@ +class CreateApiKeys < ActiveRecord::Migration[8.0] + def change + create_table :api_keys do |t| + t.string :key + t.string :client + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index e95af9e..b8c7f3a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,14 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2025_01_23_200608) do +ActiveRecord::Schema[8.0].define(version: 2025_01_24_201602) do + create_table "api_keys", force: :cascade do |t| + t.string "key" + t.string "client" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "games", force: :cascade do |t| t.datetime "timestamp" t.integer "gametype" @@ -20,6 +27,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_23_200608) do t.integer "high_roll" t.integer "low_roll" t.integer "payout" + t.string "client" t.datetime "created_at", null: false t.datetime "updated_at", null: false end diff --git a/test/fixtures/api_keys.yml b/test/fixtures/api_keys.yml new file mode 100644 index 0000000..830fefe --- /dev/null +++ b/test/fixtures/api_keys.yml @@ -0,0 +1,7 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + key: MyString + +two: + key: MyString diff --git a/test/models/api_key_test.rb b/test/models/api_key_test.rb new file mode 100644 index 0000000..6dc74dc --- /dev/null +++ b/test/models/api_key_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class ApiKeyTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end