add api key

This commit is contained in:
Evan Burkey 2025-01-24 13:03:47 -08:00
parent fc0a446b01
commit 3a0b818041
7 changed files with 63 additions and 4 deletions

View File

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

2
app/models/api_key.rb Normal file
View File

@ -0,0 +1,2 @@
class ApiKey < ApplicationRecord
end

View File

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

View File

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

10
db/schema.rb generated
View File

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

7
test/fixtures/api_keys.yml vendored Normal file
View File

@ -0,0 +1,7 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
key: MyString
two:
key: MyString

View File

@ -0,0 +1,7 @@
require "test_helper"
class ApiKeyTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end