-
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.
- Loading branch information
Showing
12 changed files
with
90 additions
and
0 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
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 |
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 @@ | ||
module APIHelpers | ||
extend Grape::API::Helpers | ||
|
||
def session | ||
env['rack.session'] | ||
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,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 |
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 @@ | ||
hAbLzw==--Izatsmxe7g0ccMzf--NlgK4PY2R7zLYMwSPx4Yeg== |
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 @@ | ||
DClZHkmJZpADs4292n3axT+PHxN5YgAXk7CUqJEiQlW9+BfPJjeCH6IgLVI13ERsPqjE5HLwPwkewSbjw2pzwTDoZ6IbLbJFCCE5TSMnDvfOo8mjzaFoqMyFSouUN5BRinqJlbm7tLIuWauoH5UjE2LmWN6n7hr8iO6dchDzyWyhDJ+sH2xqaiDrlIgTeJGbxhSEOg4dHw==--I5PVRTkkRL16keFX--RS9Oq7j9pwZalWyU/Q26lw== |
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,3 @@ | ||
# Be sure to restart your server when you modify this file. | ||
|
||
Rails.application.config.middleware.use ActionDispatch::Cookies |
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,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 |
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,3 @@ | ||
# Be sure to restart your server when you modify this file. | ||
|
||
Rails.application.config.middleware.use ActionDispatch::Session::CookieStore, key: '_sbhacks_session' |
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,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 |
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
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 |