Skip to content

Commit

Permalink
Merge pull request #59 from gmmcal/improve-auth
Browse files Browse the repository at this point in the history
Improve authentication. Use session instead of basic auth
  • Loading branch information
gmmcal authored Apr 1, 2024
2 parents 84ae0a5 + 141ee77 commit 42b54ff
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jobs:
runs-on: "ubuntu-latest"
permissions:
security-events: write
actions: read
contents: read
strategy:
fail-fast: false
matrix:
Expand Down
25 changes: 25 additions & 0 deletions app/assets/stylesheets/form.css
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;
}
11 changes: 0 additions & 11 deletions app/controllers/application_controller.rb
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
19 changes: 19 additions & 0 deletions app/controllers/auths_controller.rb
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
2 changes: 1 addition & 1 deletion app/controllers/report_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class ReportController < ApplicationController
class ReportController < SecuredController
before_action :report, :load_category, :year
def index; end

Expand Down
13 changes: 13 additions & 0 deletions app/controllers/secured_controller.rb
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
4 changes: 4 additions & 0 deletions app/helpers/auths_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

module AuthsHelper
end
11 changes: 11 additions & 0 deletions app/views/auths/_form.html.erb
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 %>
5 changes: 5 additions & 0 deletions app/views/auths/new.html.erb
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>
15 changes: 15 additions & 0 deletions app/views/layouts/auths.html.erb
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>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
root 'report#index'

resources :auths, only: %i[new create]

scope 'report' do
get '/', to: 'report#all'
get '/(:year)', to: 'report#yearly', constraints: { year: /\d*/ }
Expand Down
24 changes: 24 additions & 0 deletions test/controllers/auths_controller_test.rb
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
10 changes: 9 additions & 1 deletion test/controllers/report_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
require 'test_helper'

class ReportControllerTest < ActionDispatch::IntegrationTest
test 'should get index' do
test 'should redirect to auth when client is unauthenticated' do
get root_url
assert_response :redirect
assert_redirected_to new_auth_path
end

test 'should show report when user is logged in' do
post auths_url, params: { auth: { password: 'password' } }

get root_url
assert_response :success
end
Expand Down

0 comments on commit 42b54ff

Please sign in to comment.