This repository has been archived by the owner on Jan 29, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
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 #94 from dhollinger/authentication
Added authentication model, controller, and tasks
- Loading branch information
Showing
15 changed files
with
100 additions
and
8 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
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
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
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,36 @@ | ||
# This class handles application authentication. | ||
class AuthenticationController < ApplicationController | ||
post '/unauthenticated' do | ||
content_type :json | ||
status 401 | ||
json(message: 'Sorry, this request can not be authenticated. Try again.') | ||
end | ||
|
||
use Warden::Manager do |config| | ||
config.scope_defaults :default, | ||
strategies: [:access_token], | ||
action: '/unauthenticated' | ||
config.failure_app = self | ||
end | ||
|
||
Warden::Manager.before_failure do |env, opts| | ||
logger.error("Authentication failed to `#{opts[:attempted_path]}`\nReason: #{opts[:message]}") | ||
env['REQUEST_METHOD'] = 'POST' | ||
end | ||
|
||
Warden::Strategies.add(:access_token) do | ||
def valid? | ||
# Validate that the access token is properly formatted. | ||
# Currently only checks that it's actually a string. | ||
request.env['HTTP_ACCESS_TOKEN'].is_a?(String) | ||
end | ||
|
||
def authenticate! | ||
access_granted = request.env['HTTP_ACCESS_TOKEN'] == AuthToken.first.token | ||
|
||
!access_granted ? fail!('Invalid authentication token!') : success!(access_granted) | ||
rescue NoMethodError | ||
fail!('No token created! Please create a token with `rake db:generate_token` or set protected to `false` in the config.') | ||
end | ||
end | ||
end |
Empty file.
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 @@ | ||
class AuthToken < ActiveRecord::Base; 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
Empty file.
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 @@ | ||
class AuthTokens < ActiveRecord::Migration | ||
def change | ||
create_table :auth_tokens do |t| | ||
t.string :token | ||
t.datetime :created_at | ||
t.datetime :updated_at | ||
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,22 @@ | ||
# encoding: UTF-8 | ||
# This file is auto-generated from the current state of the database. Instead | ||
# of editing this file, please use the migrations feature of Active Record to | ||
# incrementally modify your database, and then regenerate this schema definition. | ||
# | ||
# Note that this schema.rb definition is the authoritative source for your | ||
# database schema. If you need to create the application database on another | ||
# system, you should be using db:schema:load, not running all the migrations | ||
# from scratch. The latter is a flawed and unsustainable approach (the more migrations | ||
# you'll amass, the slower it'll run and the greater likelihood for issues). | ||
# | ||
# It's strongly recommended that you check this file into your version control system. | ||
|
||
ActiveRecord::Schema.define(version: 20190305055755) do | ||
|
||
create_table "auth_tokens", force: :cascade do |t| | ||
t.string "token" | ||
t.datetime "created_at" | ||
t.datetime "updated_at" | ||
end | ||
|
||
end |
Empty file.