Skip to content

Commit

Permalink
Merge pull request #14 from zmekonnen251/authentication_and_authoriza…
Browse files Browse the repository at this point in the history
…tion

Authentication and authorization
  • Loading branch information
zmekonnen251 authored Aug 22, 2022
2 parents efc2c74 + b764c4e commit 321b1cd
Show file tree
Hide file tree
Showing 51 changed files with 1,446 additions and 30 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ env:
FORCE_COLOR: 1

jobs:
rubocop:
rubocop:
name: Rubocop
runs-on: ub stylelint:untu-18.04
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- uses: actions/setup-ruby@v1
Expand Down
26 changes: 17 additions & 9 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
source "https://rubygems.org"
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby "3.1.2"
ruby '3.1.2'

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.0.3", ">= 7.0.3.1"
gem 'rails', '~> 7.0.3', '>= 7.0.3.1'

# Use postgresql as the database for Active Record
gem "pg", "~> 1.1"
gem 'pg', '~> 1.1'

# Use the Puma web server [https://github.com/puma/puma]
gem "puma", "~> 5.0"
gem 'puma', '~> 5.0'

# Build JSON APIs with ease [https://github.com/rails/jbuilder]
# gem "jbuilder"
Expand All @@ -25,24 +25,32 @@ gem "puma", "~> 5.0"
# gem "bcrypt", "~> 3.1.7"

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]

# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", require: false
gem 'bootsnap', require: false

# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"

# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
# gem "rack-cors"
gem 'rack-cors'

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem "debug", platforms: %i[ mri mingw x64_mingw ]
gem 'bullet'
gem 'database_cleaner'
gem 'debug', platforms: %i[mri mingw x64_mingw]
gem 'rails-controller-testing'
gem 'rspec-rails'
end

group :development do
# Speed up commands on slow machines / big apps [https://github.com/rails/spring]
# gem "spring"
end

gem 'cancancan'
gem 'devise'
gem 'devise-jwt'
gem 'fast_jsonapi'
77 changes: 77 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,56 @@ GEM
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
bcrypt (3.1.18)
bootsnap (1.13.0)
msgpack (~> 1.2)
builder (3.2.4)
bullet (7.0.3)
activesupport (>= 3.0.0)
uniform_notifier (~> 1.11)
cancancan (3.4.0)
concurrent-ruby (1.1.10)
crass (1.0.6)
database_cleaner (2.0.1)
database_cleaner-active_record (~> 2.0.0)
database_cleaner-active_record (2.0.1)
activerecord (>= 5.a)
database_cleaner-core (~> 2.0.0)
database_cleaner-core (2.0.1)
debug (1.6.2)
irb (>= 1.3.6)
reline (>= 0.3.1)
devise (4.8.1)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 4.1.0)
responders
warden (~> 1.2.3)
devise-jwt (0.9.0)
devise (~> 4.0)
warden-jwt_auth (~> 0.6)
diff-lcs (1.5.0)
digest (3.1.0)
dry-auto_inject (0.9.0)
dry-container (>= 0.3.4)
dry-configurable (0.15.0)
concurrent-ruby (~> 1.0)
dry-core (~> 0.6)
dry-container (0.10.1)
concurrent-ruby (~> 1.0)
dry-core (0.8.1)
concurrent-ruby (~> 1.0)
erubi (1.11.0)
fast_jsonapi (1.5)
activesupport (>= 4.2)
globalid (1.0.0)
activesupport (>= 5.0)
i18n (1.12.0)
concurrent-ruby (~> 1.0)
io-console (0.5.11)
irb (1.4.1)
reline (>= 0.3.0)
jwt (2.4.1)
loofah (2.18.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
Expand Down Expand Up @@ -110,11 +143,14 @@ GEM
nio4r (2.5.8)
nokogiri (1.13.8-x86_64-linux)
racc (~> 1.4)
orm_adapter (0.5.0)
pg (1.4.3)
puma (5.6.4)
nio4r (~> 2.0)
racc (1.6.0)
rack (2.2.4)
rack-cors (1.1.1)
rack (>= 2.0.0)
rack-test (2.0.2)
rack (>= 1.3)
rails (7.0.3.1)
Expand All @@ -131,6 +167,10 @@ GEM
activesupport (= 7.0.3.1)
bundler (>= 1.15.0)
railties (= 7.0.3.1)
rails-controller-testing (1.0.5)
actionpack (>= 5.0.1.rc1)
actionview (>= 5.0.1.rc1)
activesupport (>= 5.0.1.rc1)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
Expand All @@ -146,11 +186,39 @@ GEM
rake (13.0.6)
reline (0.3.1)
io-console (~> 0.5)
responders (3.0.1)
actionpack (>= 5.0)
railties (>= 5.0)
rspec-core (3.11.0)
rspec-support (~> 3.11.0)
rspec-expectations (3.11.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-mocks (3.11.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-rails (5.1.2)
actionpack (>= 5.2)
activesupport (>= 5.2)
railties (>= 5.2)
rspec-core (~> 3.10)
rspec-expectations (~> 3.10)
rspec-mocks (~> 3.10)
rspec-support (~> 3.10)
rspec-support (3.11.0)
strscan (3.0.4)
thor (1.2.1)
timeout (0.3.0)
tzinfo (2.0.5)
concurrent-ruby (~> 1.0)
uniform_notifier (1.16.0)
warden (1.2.9)
rack (>= 2.0.9)
warden-jwt_auth (0.6.0)
dry-auto_inject (~> 0.8)
dry-configurable (~> 0.13)
jwt (~> 2.1)
warden (~> 1.2)
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
Expand All @@ -161,10 +229,19 @@ PLATFORMS

DEPENDENCIES
bootsnap
bullet
cancancan
database_cleaner
debug
devise
devise-jwt
fast_jsonapi
pg (~> 1.1)
puma (~> 5.0)
rack-cors
rails (~> 7.0.3, >= 7.0.3.1)
rails-controller-testing
rspec-rails
tzinfo-data

RUBY VERSION
Expand Down
85 changes: 85 additions & 0 deletions app/controllers/api/v1/mentors/registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
class Api::V1::Mentors::RegistrationsController < Devise::RegistrationsController
# before_action :configure_sign_up_params, only: [:create]
# before_action :configure_account_update_params, only: [:update]
respond_to :json

# GET /resource/sign_up
# def new
# super
# end

# POST /resource
def create
build_resource(sign_up_params)

resource.save

if resource.persisted?
render json: { message: 'Signed up successfully' }, status: :created
else
render json: { errors: resource.errors }, status: :unprocessable_entity
end
end

# GET /resource/edit
# def edit
# super
# end

# PUT /resource
# def update
# super
# end

# DELETE /resource
# def destroy
# super
# end

# GET /resource/cancel
# Forces the session data which is usually expired after sign
# in to be expired now. This is useful if the user wants to
# cancel oauth signing in/up in the middle of the process,
# removing all OAuth session data.
# def cancel
# super
# end

# protected

# If you have extra params to permit, append them to the sanitizer.
# def configure_sign_up_params
# devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute])
# end

# If you have extra params to permit, append them to the sanitizer.
# def configure_account_update_params
# devise_parameter_sanitizer.permit(:account_update, keys: [:attribute])
# end

# The path used after sign up.
# def after_sign_up_path_for(resource)
# super(resource)
# end

# The path used after sign up for inactive accounts.
# def after_inactive_sign_up_path_for(resource)
# super(resource)
# end

private

def respond_with(resource, _opts = {})
register_success && return if resource.persisted?

register_failed
end

def register_success
render json: { message: 'Signed up successfully.' }, status: :ok
end

def register_failed
render json: { message: 'Something went wrong.' }, status: :unprocessable_entity
end
end
59 changes: 59 additions & 0 deletions app/controllers/api/v1/mentors/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class Api::V1::Mentors::SessionsController < Devise::SessionsController
# before_action :configure_sign_in_params, only: [:create]
respond_to :json

def create
possible_aud = request.headers['HTTP_JWT_AUD'].presence || request.headers['JWT_AUD'].presence
self.resource = warden.authenticate!(auth_options)
sign_in(resource_name, resource)
if mentor_signed_in?
last = resource.allowlisted_mentors_jwts.where(aud: possible_aud).last
aud = possible_aud || 'UNKNOWN'

if last.present?
last.update_columns({
browser_data: params[:browser],
os_data: params[:os],
remote_ip: params[:ip]
})
aud = last.aud
end

respond_with(resource, { aud: })
else
render json: resource.errors, status: 401
end
rescue StandardError
render json: { errors: I18n.t('api.oops') }, status: 500
end

private

def current_token
request.env['warden-jwt_auth.token']
end

def respond_with(resource, opts = {})
render json: {
mentor: resource,
jwt: current_token,
aud: opts[:aud]
}
end

def respond_to_on_destroy
render json: { message: 'Signed out successfully' }
end

# DELETE /resource/sign_out
# def destroy
# super
# end

# protected

# If you have extra params to permit, append them to the sanitizer.
# def configure_sign_in_params
# devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute])
# end
end
31 changes: 31 additions & 0 deletions app/controllers/api/v1/users/registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Api::V1::Users::RegistrationsController < Devise::RegistrationsController
respond_to :json

def create
build_resource(sign_up_params)

resource.save

if resource.persisted?
render json: { message: 'Signed up successfully' }, status: :created
else
render json: { errors: resource.errors }, status: :unprocessable_entity
end
end

private

def respond_with(resource, _opts = {})
register_success && return if resource.persisted?

register_failed
end

def register_success
render json: { message: 'Signed up successfully.' }, status: :ok
end

def register_failed
render json: { message: 'Something went wrong.' }, status: :unprocessable_entity
end
end
Loading

0 comments on commit 321b1cd

Please sign in to comment.