-
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.
Merge pull request #59 from gmmcal/improve-auth
Improve authentication. Use session instead of basic auth
- Loading branch information
Showing
13 changed files
with
130 additions
and
13 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,25 @@ | ||
html, | ||
body { | ||
height: 100%; | ||
} | ||
|
||
.form-signin { | ||
max-width: 330px; | ||
padding: 1rem; | ||
} | ||
|
||
.form-signin .form-floating:focus-within { | ||
z-index: 2; | ||
} | ||
|
||
.form-signin input[type="email"] { | ||
margin-bottom: -1px; | ||
border-bottom-right-radius: 0; | ||
border-bottom-left-radius: 0; | ||
} | ||
|
||
.form-signin input[type="password"] { | ||
margin-bottom: 10px; | ||
border-top-left-radius: 0; | ||
border-top-right-radius: 0; | ||
} |
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,15 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApplicationController < ActionController::Base | ||
before_action :authenticate | ||
|
||
protected | ||
|
||
def authenticate | ||
return unless Rails.env.production? | ||
|
||
authenticate_or_request_with_http_basic do |username, password| | ||
username == ENV['USERNAME'] && password == ENV['PASSWORD'] | ||
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,19 @@ | ||
# frozen_string_literal: true | ||
|
||
class AuthsController < ApplicationController | ||
def create | ||
if params[:auth][:password] == password | ||
session[:authenticated] = true | ||
redirect_to root_path | ||
else | ||
session[:authenticated] = nil | ||
redirect_to new_auth_path, flash: { danger: 'Invalid password' } | ||
end | ||
end | ||
|
||
private | ||
|
||
def password | ||
ENV.fetch('APPLICATION_PASSWORD', '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
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 | ||
|
||
class SecuredController < ApplicationController | ||
before_action :authenticate | ||
|
||
protected | ||
|
||
def authenticate | ||
return if session[:authenticated] | ||
|
||
redirect_to new_auth_path | ||
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 | ||
|
||
module AuthsHelper | ||
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,11 @@ | ||
<%= form_for :auth, url: auths_path do |form| %> | ||
<% flash.each do |name, msg| -%> | ||
<%= content_tag :div, msg, class: 'text-bg-danger p-2 mt-2 mb-2' %> | ||
<% end -%> | ||
|
||
<div class="form-floating"> | ||
<%= form.text_field :password, placeholder: 'password', class: 'form-control' %> | ||
<%= form.label :password %> | ||
</div> | ||
<%= form.submit 'Sign in', class:'btn btn-primary w-100 py-2' %> | ||
<% 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,5 @@ | ||
<main class="form-signin w-100 m-auto"> | ||
<h1 class="h3 mb-3 fw-normal">Authentication</h1> | ||
|
||
<%= render 'form' %> | ||
</main> |
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,15 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>YNAB</title> | ||
<%= csrf_meta_tags %> | ||
<%= csp_meta_tag %> | ||
|
||
<%= stylesheet_link_tag "application", "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css", "data-turbo-track": "reload" %> | ||
<%= javascript_importmap_tags %> | ||
</head> | ||
|
||
<body class="d-flex align-items-center py-4 bg-body-tertiary"> | ||
<%= yield %> | ||
</body> | ||
</html> |
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,24 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class AuthsControllerTest < ActionDispatch::IntegrationTest | ||
test 'should show authentication page' do | ||
get new_auth_url | ||
assert_response :success | ||
end | ||
|
||
test 'should authenticate and redirect to root on success' do | ||
post auths_url, params: { auth: { password: 'password' } } | ||
|
||
assert_response :redirect | ||
assert_redirected_to root_url | ||
end | ||
|
||
test 'should fail authentication on failure' do | ||
post auths_url, params: { auth: { password: 'something else' } } | ||
|
||
assert_response :redirect | ||
assert_redirected_to new_auth_path | ||
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