-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement basic authentication mechanism
- The format of Authorization header values: 'Bearer BASE64(username:password)' - Added a translation helper that shortens the lookup key based on the nesting
- Loading branch information
Showing
23 changed files
with
347 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
module REST | ||
class API < Grape::API | ||
format "json" | ||
prefix "api" | ||
|
||
helpers Helpers::Auth | ||
|
||
before do | ||
authenticate! | ||
end | ||
|
||
get "hello" do | ||
"World" | ||
end | ||
|
||
add_swagger_documentation mount_path: "/swagger" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module REST | ||
class API | ||
module Helpers | ||
module Auth | ||
def authenticate! | ||
@current_user = Services::Authentication.new.call(headers) | ||
rescue Services::Authentication::Error => error | ||
error!(error.message, 401) | ||
end | ||
|
||
def current_user | ||
@current_user | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
module REST | ||
module Services | ||
class Authentication | ||
include Translations | ||
|
||
HEADER = "Authorization" | ||
SCHEME = "Bearer" | ||
|
||
Error = Class.new(StandardError) | ||
|
||
def call(headers) | ||
payload = header_payload!(headers) | ||
username, password = parse_payload!(payload) | ||
authenticate!(username, password) | ||
end | ||
|
||
private | ||
|
||
def header_payload!(headers) | ||
raise Error, t!("errors.missing_header", header: HEADER) unless headers.key?(HEADER) | ||
|
||
headers.fetch(HEADER) | ||
end | ||
|
||
def parse_payload!(payload) | ||
scheme, token = payload.split | ||
invalid_format! if invalid_scheme?(scheme) | ||
|
||
Base64 | ||
.decode64(token) | ||
.then { |decoded| decoded.split(":") } | ||
.tap { |username, password| invalid_format! if username.nil? || password.nil? } | ||
end | ||
|
||
def authenticate!(username, password) | ||
creds = ::Authentication::Credentials.find_by(username:) | ||
invalid_creds! if creds.nil? | ||
invalid_creds! unless creds.authenticate(password) | ||
|
||
creds.owner | ||
end | ||
|
||
def invalid_format! | ||
raise Error, t!("errors.invalid_format", header: HEADER, scheme: SCHEME) | ||
end | ||
|
||
def invalid_creds! | ||
raise Error, t!("errors.invalid_creds") | ||
end | ||
|
||
def invalid_scheme?(scheme) | ||
scheme != SCHEME | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module Translations | ||
private | ||
|
||
def _translations_base_path | ||
self.class.name.split("::").map(&:underscore) | ||
end | ||
|
||
def t(path, **options) | ||
I18n.t(_translations_full_path(path), **options) | ||
end | ||
|
||
def t!(path, **options) | ||
I18n.t!(_translations_full_path(path), **options) | ||
end | ||
|
||
def _translations_full_path(path) | ||
[*_translations_base_path, path].join(".") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
class Admin < ApplicationRecord | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
module Authentication | ||
def self.table_name_prefix | ||
"authentication_" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module Authentication | ||
class Credentials < ApplicationRecord | ||
belongs_to :owner, polymorphic: true | ||
|
||
has_secure_password | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,5 @@ | |
|
||
ActiveSupport::Inflector.inflections(:en) do |inflect| | ||
inflect.acronym "API" | ||
inflect.acronym "REST" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,11 @@ | ||
# Files in the config/locales directory are used for internationalization | ||
# and are automatically loaded by Rails. If you want to use locales other | ||
# than English, add the necessary files in this directory. | ||
# | ||
# To use the locales, use `I18n.t`: | ||
# | ||
# I18n.t "hello" | ||
# | ||
# In views, this is aliased to just `t`: | ||
# | ||
# <%= t("hello") %> | ||
# | ||
# To use a different locale, set it with `I18n.locale`: | ||
# | ||
# I18n.locale = :es | ||
# | ||
# This would use the information in config/locales/es.yml. | ||
# | ||
# The following keys must be escaped otherwise they will not be retrieved by | ||
# the default I18n backend: | ||
# | ||
# true, false, on, off, yes, no | ||
# | ||
# Instead, surround them with single quotes. | ||
# | ||
# en: | ||
# "true": "foo" | ||
# | ||
# To learn more, please read the Rails Internationalization guide | ||
# available at https://guides.rubyonrails.org/i18n.html. | ||
|
||
en: | ||
rest: | ||
services: | ||
authentication: | ||
errors: | ||
missing_header: Header "%{header}" is missing" | ||
invalid_format: > | ||
Invalid payload for the header %{header} | ||
Please, use the following format: "%{scheme} Base64(USERNAME:PASSWORD) | ||
invalid_creds: Invalid username or password | ||
hello: "Hello world" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,5 +5,5 @@ | |
|
||
# Defines the root path route ("/") | ||
# root "articles#index" | ||
mount API => "/" | ||
mount REST::API => "/" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateAuthCredentials < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :authentication_credentials do |t| | ||
t.string :username, null: false, index: { unique: true } | ||
t.text :password_digest, null: false | ||
|
||
t.string :owner_type, null: false | ||
t.bigint :owner_id, null: false | ||
t.timestamps | ||
end | ||
|
||
add_index(:authentication_credentials, %i[owner_id owner_type], unique: true) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateAdmins < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :admins, &:timestamps | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
namespace :admin do | ||
task create: :environment do | ||
username = ENV.fetch("ADMIN_USERNAME") | ||
password = ENV.fetch("ADMIN_PASSWORD") | ||
|
||
Authentication::Credentials.create!(username:, password:, owner: Admin.create!) | ||
token = Base64.encode64("#{username}:#{password}") | ||
payload = "Bearer #{token}" | ||
puts "Your payload for #{REST::Services::Authentication::HEADER} header: #{payload}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :authentication_credentials, class: "Authentication::Credentials" do | ||
password { Faker::Internet.password } | ||
username { Faker::Internet.username } | ||
end | ||
end |
Oops, something went wrong.