Skip to content

Commit

Permalink
Create authentication endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
btk5h committed Aug 26, 2020
1 parent e73b49f commit 041b5ba
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@

# Ignore master key for decrypting credentials and more.
/config/master.key
/config/credentials/development.key
/config/credentials/production.key
23 changes: 23 additions & 0 deletions app/api/api/auth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module API
class Auth < Grape::API
namespace :session do
desc 'Acquires a session cookie from a username and password'
params do
requires :username, type: String
requires :password, type: String
end
post do
session.destroy

user = User.find_by(email: params[:username])

error! :not_found, 404 unless user.try(:authenticate, params[:password])

session[:user_id] = user.id

status :ok
{}
end
end
end
end
7 changes: 7 additions & 0 deletions app/api/api_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module APIHelpers
extend Grape::API::Helpers

def session
env['rack.session']
end
end
17 changes: 17 additions & 0 deletions app/lib/auth/session_auth_middleware.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Auth
class SessionAuthMiddleware < Rack::Auth::AbstractHandler
def call(env)
session = env['rack.session']

return @app.call(env) if session[:user_id]

unauthorized
end

private

def challenge
'Session-Cookie'
end
end
end
1 change: 1 addition & 0 deletions config/credentials/development.yml.enc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hAbLzw==--Izatsmxe7g0ccMzf--NlgK4PY2R7zLYMwSPx4Yeg==
1 change: 1 addition & 0 deletions config/credentials/production.yml.enc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DClZHkmJZpADs4292n3axT+PHxN5YgAXk7CUqJEiQlW9+BfPJjeCH6IgLVI13ERsPqjE5HLwPwkewSbjw2pzwTDoZ6IbLbJFCCE5TSMnDvfOo8mjzaFoqMyFSouUN5BRinqJlbm7tLIuWauoH5UjE2LmWN6n7hr8iO6dchDzyWyhDJ+sH2xqaiDrlIgTeJGbxhSEOg4dHw==--I5PVRTkkRL16keFX--RS9Oq7j9pwZalWyU/Q26lw==
3 changes: 3 additions & 0 deletions config/initializers/cookies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Be sure to restart your server when you modify this file.

Rails.application.config.middleware.use ActionDispatch::Cookies
5 changes: 5 additions & 0 deletions config/initializers/grape.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Be sure to restart your server when you modify this file.

Rails.configuration.to_prepare do
Grape::Middleware::Auth::Strategies.add(:from_session, Auth::SessionAuthMiddleware)
end
3 changes: 3 additions & 0 deletions config/initializers/session_store.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Be sure to restart your server when you modify this file.

Rails.application.config.middleware.use ActionDispatch::Session::CookieStore, key: '_sbhacks_session'
17 changes: 17 additions & 0 deletions test/api/auth_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'test_helper'

class AuthTest < ActionDispatch::IntegrationTest
test 'signing in sets the session id' do
user = sign_in_as(:user)

assert_equal user.id, @request.session[:user_id]
end

test 'signing in with invalid credentials clears the session' do
sign_in_as(:user)

post session_path, params: { username: 'nonexistent user', password: '' }

assert_nil @request.session[:user_id]
end
end
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
require 'test_helpers/sign_in_helper'

module ActiveSupport
class TestCase
include GrapeRouteHelpers::NamedRouteMatcher
include FactoryBot::Syntax::Methods
include SignInHelper
# Run tests in parallel with specified workers
parallelize(workers: :number_of_processors)

Expand Down
9 changes: 9 additions & 0 deletions test/test_helpers/sign_in_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module SignInHelper
def sign_in_as(user)
user = create(user) if user.is_a? Symbol

post session_path, params: { username: user.email, password: user.password }

user
end
end

0 comments on commit 041b5ba

Please sign in to comment.