Skip to content

Commit

Permalink
add project auth
Browse files Browse the repository at this point in the history
  • Loading branch information
kaiomagalhaes committed Jun 5, 2024
1 parent baa4f32 commit f43dd5b
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 6 deletions.
28 changes: 24 additions & 4 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,38 @@
class ApplicationController < ActionController::API
before_action :set_default_response_format
before_action :authenticate
attr_reader :current_user
attr_reader :current_user, :current_project

def authenticate
return user_invalid! unless authorization_header
if authorization_header
authenticate_user
elsif project_auth_key
authenticate_project
else
user_invalid!
end
end

private

def authenticate_user
user = User.find_or_initialize_by({
email: user_auth_params['email'],
google_id: user_auth_params['google_id']
})
return user_invalid! unless user.valid?

save_user!(user)

@current_user = user
end

def authenticate_project
project_auth = ProjectAuth.find_by(key: project_auth_key)
return user_invalid! unless project_auth

@current_project = project_auth.project
end

def save_user!(user)
if user.new_record?
user.first_name = user_auth_params['first_name']
Expand All @@ -29,7 +45,7 @@ def save_user!(user)
end

def user_invalid!
render_error('Invalid user', :unauthorized)
render_error('Invalid user or project', :unauthorized)
end

def render_error(message, status)
Expand All @@ -45,6 +61,10 @@ def authorization_header
request.headers['Authorization']
end

def project_auth_key
request.headers['Project-Auth-Key']
end

def user_auth_params
return @user_auth_params if @user_auth_params

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def destroy
private

def set_project
@project = Project.friendly.find(params[:id])
@project = @current_project || Project.friendly.find(params[:id])
end

def project_params
Expand Down
35 changes: 35 additions & 0 deletions app/models/project_auth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: project_auths
#
# id :bigint not null, primary key
# key :string
# created_at :datetime not null
# updated_at :datetime not null
# project_id :bigint not null
#
# Indexes
#
# index_project_auths_on_project_id (project_id)
#
# Foreign Keys
#
# fk_rails_... (project_id => projects.id)
#
class ProjectAuth < ApplicationRecord
belongs_to :project

before_create :generate_key, unless: :key_present?

private

def generate_key
self.key = SecureRandom.hex(10) # Generates a random hex string of 20 characters
end

def key_present?
key.present?
end
end
12 changes: 12 additions & 0 deletions db/migrate/20240605183622_create_project_auths.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class CreateProjectAuths < ActiveRecord::Migration[7.0]
def change
create_table :project_auths do |t|
t.references :project, null: false, foreign_key: true
t.string :key

t.timestamps
end
end
end
11 changes: 10 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions spec/factories/project_auths.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: project_auths
#
# id :bigint not null, primary key
# key :string
# created_at :datetime not null
# updated_at :datetime not null
# project_id :bigint not null
#
# Indexes
#
# index_project_auths_on_project_id (project_id)
#
# Foreign Keys
#
# fk_rails_... (project_id => projects.id)
#
FactoryBot.define do
factory :project_auth do
project { nil }
key { 'MyString' }
end
end
25 changes: 25 additions & 0 deletions spec/models/project_auth_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

# == Schema Information
#
# Table name: project_auths
#
# id :bigint not null, primary key
# key :string
# created_at :datetime not null
# updated_at :datetime not null
# project_id :bigint not null
#
# Indexes
#
# index_project_auths_on_project_id (project_id)
#
# Foreign Keys
#
# fk_rails_... (project_id => projects.id)
#
require 'rails_helper'

RSpec.describe ProjectAuth, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

0 comments on commit f43dd5b

Please sign in to comment.