add api key
This commit is contained in:
		| @@ -9,9 +9,15 @@ 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]) | ||||||
|       return head 208 # HTTP 208: Already reported |       already_reported_response | ||||||
|  |       return | ||||||
|     end |     end | ||||||
|  |  | ||||||
|     players = [] |     players = [] | ||||||
| @@ -25,6 +31,7 @@ class GameController < ApplicationController | |||||||
|     end |     end | ||||||
|  |  | ||||||
|     @game = Game.create(game_params) |     @game = Game.create(game_params) | ||||||
|  |     @game.client = @apikey.client | ||||||
|  |  | ||||||
|     if @game.save |     if @game.save | ||||||
|       players.each do | player | |       players.each do | player | | ||||||
| @@ -38,9 +45,9 @@ class GameController < ApplicationController | |||||||
|           player.save |           player.save | ||||||
|         end |         end | ||||||
|       end |       end | ||||||
|       head :ok |       render json: {}, status: :ok | ||||||
|     else |     else | ||||||
|       head :unprocessable_entity |       render json: { error: "Unable to process input" }, status: :unprocessable_content | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
|  |  | ||||||
| @@ -49,4 +56,21 @@ class GameController < ApplicationController | |||||||
|   def game_params |   def game_params | ||||||
|     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 | ||||||
|  |     render json: { error: "Already reported this gambo" }, status: :already_reported | ||||||
|  |   end | ||||||
| end | end | ||||||
|   | |||||||
							
								
								
									
										2
									
								
								app/models/api_key.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								app/models/api_key.rb
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,2 @@ | |||||||
|  | class ApiKey < ApplicationRecord | ||||||
|  | end | ||||||
| @@ -9,6 +9,7 @@ class CreateGames < ActiveRecord::Migration[8.0] | |||||||
|       t.integer :high_roll |       t.integer :high_roll | ||||||
|       t.integer :low_roll |       t.integer :low_roll | ||||||
|       t.integer :payout |       t.integer :payout | ||||||
|  |       t.string :client | ||||||
|  |  | ||||||
|       t.timestamps |       t.timestamps | ||||||
|     end |     end | ||||||
|   | |||||||
							
								
								
									
										10
									
								
								db/migrate/20250124201602_create_api_keys.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								db/migrate/20250124201602_create_api_keys.rb
									
									
									
									
									
										Normal 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
									
									
									
								
							
							
						
						
									
										10
									
								
								db/schema.rb
									
									
									
										generated
									
									
									
								
							| @@ -10,7 +10,14 @@ | |||||||
| # | # | ||||||
| # It's strongly recommended that you check this file into your version control system. | # 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| |   create_table "games", force: :cascade do |t| | ||||||
|     t.datetime "timestamp" |     t.datetime "timestamp" | ||||||
|     t.integer "gametype" |     t.integer "gametype" | ||||||
| @@ -20,6 +27,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_23_200608) do | |||||||
|     t.integer "high_roll" |     t.integer "high_roll" | ||||||
|     t.integer "low_roll" |     t.integer "low_roll" | ||||||
|     t.integer "payout" |     t.integer "payout" | ||||||
|  |     t.string "client" | ||||||
|     t.datetime "created_at", null: false |     t.datetime "created_at", null: false | ||||||
|     t.datetime "updated_at", null: false |     t.datetime "updated_at", null: false | ||||||
|   end |   end | ||||||
|   | |||||||
							
								
								
									
										7
									
								
								test/fixtures/api_keys.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								test/fixtures/api_keys.yml
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | |||||||
|  | # Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | ||||||
|  |  | ||||||
|  | one: | ||||||
|  |   key: MyString | ||||||
|  |  | ||||||
|  | two: | ||||||
|  |   key: MyString | ||||||
							
								
								
									
										7
									
								
								test/models/api_key_test.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								test/models/api_key_test.rb
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,7 @@ | |||||||
|  | require "test_helper" | ||||||
|  |  | ||||||
|  | class ApiKeyTest < ActiveSupport::TestCase | ||||||
|  |   # test "the truth" do | ||||||
|  |   #   assert true | ||||||
|  |   # end | ||||||
|  | end | ||||||
		Reference in New Issue
	
	Block a user