Skip to content

Commit

Permalink
Basic warden authentication.
Browse files Browse the repository at this point in the history
  • Loading branch information
rjurado committed Jan 25, 2017
0 parents commit 6f49237
Show file tree
Hide file tree
Showing 80 changed files with 1,374 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.bundle/
log/*.log
pkg/
spec/dummy/db/*.sqlite3
spec/dummy/db/*.sqlite3-journal
spec/dummy/log/*.log
spec/dummy/tmp/
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
10 changes: 10 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
source 'https://rubygems.org'

# Declare your gem's dependencies in rails_token_auth.gemspec.
# Bundler will treat runtime dependencies like base dependencies, and
# development dependencies will be added by default to the :development group.
gemspec

gem "rspec-rails"
gem "factory_girl_rails"
gem "mongoid"
148 changes: 148 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
PATH
remote: .
specs:
rails_token_auth (0.1.0)
rails (~> 5.0.1)

GEM
remote: https://rubygems.org/
specs:
actioncable (5.0.1)
actionpack (= 5.0.1)
nio4r (~> 1.2)
websocket-driver (~> 0.6.1)
actionmailer (5.0.1)
actionpack (= 5.0.1)
actionview (= 5.0.1)
activejob (= 5.0.1)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (5.0.1)
actionview (= 5.0.1)
activesupport (= 5.0.1)
rack (~> 2.0)
rack-test (~> 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (5.0.1)
activesupport (= 5.0.1)
builder (~> 3.1)
erubis (~> 2.7.0)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
activejob (5.0.1)
activesupport (= 5.0.1)
globalid (>= 0.3.6)
activemodel (5.0.1)
activesupport (= 5.0.1)
activerecord (5.0.1)
activemodel (= 5.0.1)
activesupport (= 5.0.1)
arel (~> 7.0)
activesupport (5.0.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (~> 0.7)
minitest (~> 5.1)
tzinfo (~> 1.1)
arel (7.1.4)
bson (4.2.1)
builder (3.2.3)
concurrent-ruby (1.0.4)
diff-lcs (1.2.5)
erubis (2.7.0)
factory_girl (4.7.0)
activesupport (>= 3.0.0)
factory_girl_rails (4.7.0)
factory_girl (~> 4.7.0)
railties (>= 3.0.0)
globalid (0.3.7)
activesupport (>= 4.1.0)
i18n (0.7.0)
loofah (2.0.3)
nokogiri (>= 1.5.9)
mail (2.6.4)
mime-types (>= 1.16, < 4)
method_source (0.8.2)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mini_portile2 (2.1.0)
minitest (5.10.1)
mongo (2.4.1)
bson (>= 4.2.1, < 5.0.0)
mongoid (6.0.3)
activemodel (~> 5.0)
mongo (~> 2.3)
nio4r (1.2.1)
nokogiri (1.7.0.1)
mini_portile2 (~> 2.1.0)
rack (2.0.1)
rack-test (0.6.3)
rack (>= 1.0)
rails (5.0.1)
actioncable (= 5.0.1)
actionmailer (= 5.0.1)
actionpack (= 5.0.1)
actionview (= 5.0.1)
activejob (= 5.0.1)
activemodel (= 5.0.1)
activerecord (= 5.0.1)
activesupport (= 5.0.1)
bundler (>= 1.3.0, < 2.0)
railties (= 5.0.1)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.2)
activesupport (>= 4.2.0, < 6.0)
nokogiri (~> 1.6)
rails-html-sanitizer (1.0.3)
loofah (~> 2.0)
railties (5.0.1)
actionpack (= 5.0.1)
activesupport (= 5.0.1)
method_source
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (12.0.0)
rspec-core (3.5.4)
rspec-support (~> 3.5.0)
rspec-expectations (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-mocks (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-rails (3.5.2)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-support (~> 3.5.0)
rspec-support (3.5.0)
sprockets (3.7.1)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.2.0)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
thor (0.19.4)
thread_safe (0.3.5)
tzinfo (1.2.2)
thread_safe (~> 0.1)
websocket-driver (0.6.4)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.2)

PLATFORMS
ruby

DEPENDENCIES
factory_girl_rails
mongoid
rails_token_auth!
rspec-rails

BUNDLED WITH
1.12.5
20 changes: 20 additions & 0 deletions MIT-LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright 2017 rjurado

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# RailsTokenAuth
Rails token authentication solution for Rails based on Warden and JWT.

## Usage
How to use my plugin.

## Installation
Add this line to your application's Gemfile:

```ruby
gem 'rails_token_auth'
```

And then execute:
```bash
$ bundle
```

Or install it yourself as:
```bash
$ gem install rails_token_auth
```

## Configuration
Include `AuthModel` module into your User class:

```ruby
# app/models/user.rb
class User
include Mongoid::Document
include Mongoid::AuthModel
end
```

Include `WardenHelper` into your `ApplicationController`:

```ruby
# app/controllers/application_controller.rb
class ApplicationController < ActionController::API
include WardenHelper
end
```

Authenticate your controllers:

```ruby
class MyController < ApplicationController
before_action :authenticate!
end
```

## Contributing
Contribution directions go here.

## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
26 changes: 26 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
begin
require 'bundler/setup'
rescue LoadError
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
end

require 'rdoc/task'

RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'RailsTokenAuth'
rdoc.options << '--line-numbers'
rdoc.rdoc_files.include('README.md')
rdoc.rdoc_files.include('lib/**/*.rb')
end

APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
load 'rails/tasks/engine.rake'


load 'rails/tasks/statistics.rake'



require 'bundler/gem_tasks'

Empty file added app/controllers/.keep
Empty file.
17 changes: 17 additions & 0 deletions app/controllers/concerns/warden_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module WardenHelper
def signed_in?
!current_user.nil?
end

def current_user
warden.user
end

def warden
request.env['warden']
end

def authenticate!
warden.authenticate!
end
end
11 changes: 11 additions & 0 deletions app/controllers/unauthorized_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class UnauthorizedController < ActionController::Metal
def self.call(env)
@respond ||= action(:respond)
@respond.call(env)
end

def respond
self.response_body = "Unauthorized Action"
self.status = :unauthorized
end
end
Empty file added app/helpers/.keep
Empty file.
Empty file added app/mailers/.keep
Empty file.
Empty file added app/models/.keep
Empty file.
16 changes: 16 additions & 0 deletions app/services/auth_token_strategy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class AuthTokenStrategy < ::Warden::Strategies::Base
def valid?
authentication_token
end

def authenticate!
user = User.where(auth_token: authentication_token).first
user.nil? ? fail!('strategies.authentication_token.failed') : success!(user)
end

private

def authentication_token
params['auth_token']
end
end
11 changes: 11 additions & 0 deletions app/services/mongoid/auth_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
include ActiveModel::SecurePassword

module Mongoid::AuthModel
def self.included(base)
base.send(:field, :email, {type: String})
base.send(:field, :password_digest, {type: String})
base.send(:field, :auth_token, {type: String})

base.send(:has_secure_password)
end
end
13 changes: 13 additions & 0 deletions bin/rails
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails gems
# installed from the root of your application.

ENGINE_ROOT = File.expand_path('../..', __FILE__)
ENGINE_PATH = File.expand_path('../../lib/rails_token_auth/engine', __FILE__)

# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])

require 'rails/all'
require 'rails/engine/commands'
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Rails.application.routes.draw do
end
8 changes: 8 additions & 0 deletions lib/rails_token_auth.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "warden"
require "bcrypt"

require "rails_token_auth/engine"

module RailsTokenAuth
# Your code goes here...
end
17 changes: 17 additions & 0 deletions lib/rails_token_auth/engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module RailsTokenAuth
class Engine < ::Rails::Engine
config.generators do |g|
g.test_framework :rspec
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
end

initializer "rails_token_auth.warden" do |app|
app.middleware.insert_after ActionDispatch::Callbacks, Warden::Manager do |manager|
manager.default_strategies :authentication_token
manager.failure_app = UnauthorizedController
end

Warden::Strategies.add(:authentication_token, AuthTokenStrategy)
end
end
end
3 changes: 3 additions & 0 deletions lib/rails_token_auth/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module RailsTokenAuth
VERSION = '0.1.0'
end
4 changes: 4 additions & 0 deletions lib/tasks/rails_token_auth_tasks.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# desc "Explaining what the task does"
# task :rails_token_auth do
# # Task goes here
# end
Loading

0 comments on commit 6f49237

Please sign in to comment.