Skip to content

Commit

Permalink
Remane project to rails_jwt_auth.
Browse files Browse the repository at this point in the history
  • Loading branch information
rjurado committed Feb 1, 2017
1 parent 61ecfe8 commit 7a009af
Show file tree
Hide file tree
Showing 24 changed files with 73 additions and 75 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source 'https://rubygems.org'

# Declare your gem's dependencies in rails_token_auth.gemspec.
# Declare your gem's dependencies in rails_jwt_auth.gemspec.
# Bundler will treat runtime dependencies like base dependencies, and
# development dependencies will be added by default to the :development group.
gemspec
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
rails_token_auth (0.1.0)
rails_jwt_auth (0.1.0)
bcrypt (~> 3.1.11)
jwt (~> 1.5.6)
rails (~> 5.0.1)
Expand Down Expand Up @@ -149,7 +149,7 @@ PLATFORMS
DEPENDENCIES
factory_girl_rails
mongoid
rails_token_auth!
rails_jwt_auth!
rspec-rails
sqlite3

Expand Down
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# RailsTokenAuth
Rails token authentication solution for Rails based on Warden and JWT.
# RailsJwtAuth
Rails authentication solution based on Warden and JWT.

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

```ruby
gem 'rails_token_auth'
gem 'rails_jwt_auth'
```

And then execute:
Expand All @@ -15,12 +15,12 @@ $ bundle

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

Finally execute:
```bash
rails g rails_token_auth:install
rails g rails_jwt_auth:install
```

## Configuration
Expand All @@ -32,7 +32,7 @@ You can edit configuration options into `config/initializers/auth_token_auth.rb`
| auth_field_name | 'email' | Field used to authenticate user with password |
| auth_field_email | true | Validate auth field email format |
| jwt_expiration_time | 7.days | Tokens expiration time |
| jwt_issuer | 'RTA' | The "iss" (issuer) claim identifies the principal that issued the JWT |
| jwt_issuer | 'RailsJwtAuth' | The "iss" (issuer) claim identifies the principal that issued the JWT |
| simultaneous_sessions | 2 | Number of simultaneous sessions for an user |


Expand All @@ -44,12 +44,12 @@ You can change default model (User) usin the configuration property
`model_name`.

### ActiveRecord
Include `RailsTokenAuth::Authenticatable` module into your User class:
Include `RailsJwtAuth::Authenticatable` module into your User class:

```ruby
# app/models/user.rb
class User < ApplicationRecord
include RailsTokenAuth::Authenticatable
include RailsJwtAuth::Authenticatable
end
```

Expand All @@ -70,13 +70,13 @@ end


### Mongoid
Include `RailsTokenAuth::Authenticatable` module into your User class:
Include `RailsJwtAuth::Authenticatable` module into your User class:

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

Expand All @@ -85,7 +85,7 @@ Fields are added automatically.

## Controller helpers

RailsTokenAuth will create some helpers to use inside your controllers.
RailsJwtAuth will create some helpers to use inside your controllers.

To use this helpers we need to include `WardenHelper` into `ApplicationController`:

Expand Down Expand Up @@ -117,7 +117,7 @@ end


## Session
Session api is defined by RailsTokenAuth::SessionController.
Session api is defined by RailsJwtAuth::SessionController.

1. Get session token:

Expand All @@ -144,7 +144,7 @@ Session api is defined by RailsTokenAuth::SessionController.


## Registration
Registration api is defined by RailsTokenAuth::RegistrationController.
Registration api is defined by RailsJwtAuth::RegistrationController.

1. Register user:

Expand Down Expand Up @@ -177,7 +177,7 @@ To create your your own registration controller see this.

## Custom controllers

You can overwrite RailsTokenAuth controller to edit actions, responses,
You can overwrite RailsJwtAuth controller to edit actions, responses,
permited parameters...

For example, if we want to change registration strong parameters we
Expand All @@ -186,7 +186,7 @@ create new registration controller inherited from default controller:

```ruby
# app/controllers/registrations_controller.rb
class RegistrationsController < RailsTokenAuth::RegistrationsController
class RegistrationsController < RailsJwtAuth::RegistrationsController
private

def create_params
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require 'rdoc/task'

RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'RailsTokenAuth'
rdoc.title = 'RailsJwtAuth'
rdoc.options << '--line-numbers'
rdoc.rdoc_files.include('README.md')
rdoc.rdoc_files.include('lib/**/*.rb')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class RailsTokenAuth::RegistrationsController < ApplicationController
class RailsJwtAuth::RegistrationsController < ApplicationController
def create
user = RTA.model.new(create_params)
user = RailsJwtAuth.model.new(create_params)

if user.save
render json: create_success_response(user), status: 201
Expand All @@ -17,14 +17,14 @@ def destroy
private

def create_success_response(user)
{user: {id: user.id.to_s, RTA.auth_field_name => user.send(RTA.auth_field_name)} }
{user: {id: user.id.to_s, RailsJwtAuth.auth_field_name => user.send(RailsJwtAuth.auth_field_name)} }
end

def create_error_response(user)
{user: user.errors}
end

def create_params
params.require(:user).permit(RTA.auth_field_name, :password, :password_confirmation)
params.require(:user).permit(RailsJwtAuth.auth_field_name, :password, :password_confirmation)
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class RailsTokenAuth::SessionsController < ApplicationController
class RailsJwtAuth::SessionsController < ApplicationController
def create
user = RTA.model.where(RTA.auth_field_name => params[RTA.auth_field_name].to_s.downcase).first
user = RailsJwtAuth.model.where(
RailsJwtAuth.auth_field_name => params[RailsJwtAuth.auth_field_name].to_s.downcase).first

if user && user.authenticate(params[:password])
token = user.regenerate_auth_token
Expand All @@ -23,6 +24,6 @@ def create_success_response(user, jwt)
end

def create_error_response(user)
{session: {error: "Invalid #{RTA.auth_field_name} / password"}}
{session: {error: "Invalid #{RailsJwtAuth.auth_field_name} / password"}}
end
end
2 changes: 1 addition & 1 deletion app/services/auth_token_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def authenticate!
fail!('strategies.authentication_token.failed')
end

if model = RTA.model.get_by_token(payload[0]['auth_token'])
if model = RailsJwtAuth.model.get_by_token(payload[0]['auth_token'])
success!(model)
else
fail!('strategies.authentication_token.failed')
Expand Down
4 changes: 2 additions & 2 deletions app/services/json_web_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def self.valid_payload?(payload)
# Default options to be encoded in the token
def self.meta
{
exp: RTA.jwt_expiration_time.from_now.to_i,
iss: RTA.jwt_issuer
exp: RailsJwtAuth.jwt_expiration_time.from_now.to_i,
iss: RailsJwtAuth.jwt_issuer
}
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
include ActiveModel::SecurePassword

module RailsTokenAuth
module RailsJwtAuth
module Authenticatable
def regenerate_auth_token(token=nil)
new_token = SecureRandom.base58(24)

if RTA.simultaneous_sessions > 1
tokens = ((self.auth_tokens || []) - [token]).last(RTA.simultaneous_sessions)
if RailsJwtAuth.simultaneous_sessions > 1
tokens = ((self.auth_tokens || []) - [token]).last(RailsJwtAuth.simultaneous_sessions)
self.update_attribute(:auth_tokens, (tokens + [new_token]).uniq)
else
self.update_attribute(:auth_tokens, [new_token])
Expand All @@ -16,7 +16,7 @@ def regenerate_auth_token(token=nil)
end

def destroy_auth_token(token)
if RTA.simultaneous_sessions > 1
if RailsJwtAuth.simultaneous_sessions > 1
tokens = self.auth_tokens || []
self.update_attribute(:auth_tokens, tokens - [token])
else
Expand All @@ -27,24 +27,24 @@ def destroy_auth_token(token)
module ClassMethods
def get_by_token(token)
if defined? Mongoid
RTA.model.where(auth_tokens: token).first
RailsJwtAuth.model.where(auth_tokens: token).first
else
RTA.model.where("auth_tokens like ?", "% #{token}\n%").first
RailsJwtAuth.model.where("auth_tokens like ?", "% #{token}\n%").first
end
end
end

def self.included(base)
if base.ancestors.include? Mongoid::Document
base.send(:field, RTA.auth_field_name, {type: String})
base.send(:field, RailsJwtAuth.auth_field_name, {type: String})
base.send(:field, :password_digest, {type: String})
base.send(:field, :auth_tokens, {type: Array})
elsif base.ancestors.include? ActiveRecord::Base
base.send(:serialize, :auth_tokens, Array)
end

base.send(:validates, RTA.auth_field_name, {presence: true, uniqueness: true})
base.send(:validates, RTA.auth_field_name, {email: true}) if RTA.auth_field_email
base.send(:validates, RailsJwtAuth.auth_field_name, {presence: true, uniqueness: true})
base.send(:validates, RailsJwtAuth.auth_field_name, {email: true}) if RailsJwtAuth.auth_field_email

base.send(:has_secure_password)

Expand Down
2 changes: 1 addition & 1 deletion bin/rails
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# installed from the root of your application.

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

# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
Expand Down
12 changes: 12 additions & 0 deletions lib/generators/rails_jwt_auth/install_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class RailsJwtAuth::InstallGenerator < Rails::Generators::Base
source_root File.expand_path('../../templates', __FILE__)

def create_initializer_file
copy_file "initializer.rb", "config/initializers/rails_jwt_auth.rb"
end

def create_routes
route "resource :session, controller: 'rails_jwt_auth/sessions', only: [:create, :destroy]"
route "resource :registration, controller: 'rails_jwt_auth/registrations', only: [:create, :update, :destroy]"
end
end
12 changes: 0 additions & 12 deletions lib/generators/rails_token_auth/install_generator.rb

This file was deleted.

4 changes: 2 additions & 2 deletions lib/generators/templates/initializer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RailsTokenAuth.setup do |config|
RailsJwtAuth.setup do |config|
# authentication model class name
#config.model_name = 'User'

Expand All @@ -12,7 +12,7 @@
#config.jwt_expiration_time = 7.days

# the "iss" (issuer) claim identifies the principal that issued the JWT
#config.jwt_issuer = 'RTA'
#config.jwt_issuer = 'RailsJwtAuth'

# number of simultaneously sessions for an user
#config.simultaneously_sessions = 3
Expand Down
9 changes: 3 additions & 6 deletions lib/rails_token_auth.rb → lib/rails_jwt_auth.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require "warden"
require "bcrypt"

require "rails_token_auth/engine"
require "rails_jwt_auth/engine"

module RailsTokenAuth
module RailsJwtAuth
mattr_accessor :model_name
@@model_name = 'User'

Expand All @@ -17,7 +17,7 @@ module RailsTokenAuth
@@jwt_expiration_time = 7.days

mattr_accessor :jwt_issuer
@@jwt_issuer = 'RTA'
@@jwt_issuer = 'RailsJwtAuth'

mattr_accessor :simultaneous_sessions
@@simultaneous_sessions = 2
Expand All @@ -30,6 +30,3 @@ def self.setup
yield self
end
end

# create alias
RTA = RailsTokenAuth
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module RailsTokenAuth
module RailsJwtAuth
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|
initializer "rails_jwt_auth.warden" do |app|
app.middleware.insert_after ActionDispatch::Callbacks, Warden::Manager do |manager|
manager.default_strategies :authentication_token
manager.failure_app = UnauthorizedController
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module RailsTokenAuth
module RailsJwtAuth
VERSION = '0.1.0'
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# desc "Explaining what the task does"
# task :rails_token_auth do
# task :rails_jwt_auth do
# # Task goes here
# end
6 changes: 3 additions & 3 deletions rails_token_auth.gemspec → rails_jwt_auth.gemspec
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
$:.push File.expand_path("../lib", __FILE__)

# Maintain your gem's version:
require "rails_token_auth/version"
require "rails_jwt_auth/version"

# Describe your gem and declare its dependencies:
Gem::Specification.new do |s|
s.name = "rails_token_auth"
s.version = RailsTokenAuth::VERSION
s.name = "rails_jwt_auth"
s.version = RailsJwtAuth::VERSION
s.authors = ["rjurado"]
s.email = ["[email protected]"]
s.homepage = "https://github.com/rjurado01/rails-token-auth"
Expand Down
Loading

0 comments on commit 7a009af

Please sign in to comment.