Skip to content

Commit

Permalink
Merge pull request #71 from ignaciojonas/master
Browse files Browse the repository at this point in the history
Release 4.1.0
  • Loading branch information
chenkie authored Jul 21, 2016
2 parents 90bf4da + 1c4e820 commit 73c62b8
Show file tree
Hide file tree
Showing 92 changed files with 1,914 additions and 627 deletions.
3 changes: 2 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
inherit_from: .rubocop_todo.yml
Rails:
Enabled: true
AllCops:
RunRailsCops: true
Exclude:
- bin/**/*
- vendor/**/*
Expand Down
2 changes: 0 additions & 2 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 15

# Configuration parameters: AllowURI, URISchemes.
Metrics/LineLength:
Max: 121
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ Using [APIv2](https://auth0.com/docs/api/v2)
require "auth0"

auth0 = Auth0Client.new(
:client_id => "YOUR CLIENT ID"
:token => "YOUR JWT HERE",
:domain => "<YOUR ACCOUNT>.auth0.com"
:domain => "<YOUR ACCOUNT>.auth0.com"
)

puts auth0.get_users
Expand All @@ -40,7 +41,24 @@ require "auth0"
auth0 = Auth0Client.new(
:client_id => "YOUR CLIENT ID",
:client_secret => "YOUR CLIENT SECRET",
:domain => "<YOUR ACCOUNT>.auth0.com"
:domain => "<YOUR ACCOUNT>.auth0.com",
:api_version => "1"
)

puts auth0.get_users
```

### Timeout
You can setup a custom timeout in the Auth0Client. By default it is set to 10 minutes.

```ruby
require "auth0"

auth0 = Auth0Client.new(
:client_id => "YOUR CLIENT ID"
:token => "YOUR JWT HERE",
:domain => "<YOUR ACCOUNT>.auth0.com",
:timeout => 15
)

puts auth0.get_users
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ begin
RuboCop::RakeTask.new(:rubocop)

require 'yard'
DOC_FILES = ['lib/auth0/api/v2/*.rb', 'lib/auth0/api/authentication_endpoints.rb']
DOC_FILES = ['lib/auth0/api/v2/*.rb', 'lib/auth0/api/authentication_endpoints.rb'].freeze

desc 'Build Documentation'
YARD::Rake::YardocTask.new(:documentation) do |t|
Expand Down
3 changes: 2 additions & 1 deletion auth0.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
s.require_paths = ['lib']

s.add_runtime_dependency 'httmultiparty', '~> 0.3.16'
s.add_runtime_dependency 'rest-client', '~> 1.8.0'

s.add_development_dependency 'rake', '~> 10.4'
s.add_development_dependency 'fuubar', '~> 2.0'
Expand All @@ -28,6 +28,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'pry-nav', '~> 0.2.4'
s.add_development_dependency 'rspec', '~> 3.1', '>= 3.1.0'
s.add_development_dependency 'rack-test', '~> 0.6'
s.add_development_dependency 'rack', '~> 1.6.4'
s.add_development_dependency 'simplecov', '~> 0.9'
s.add_development_dependency 'faker', '~> 1.4'
s.add_development_dependency 'yard', '~> 0.8'
Expand Down
2 changes: 1 addition & 1 deletion examples/ruby-api/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ source 'http://rubygems.org'
# gem "rails"
gem 'sinatra', '~> 1.4'
gem 'jwt', '~> 1.5'
gem 'dotenv'
gem 'dotenv'
2 changes: 1 addition & 1 deletion examples/ruby-api/config.ru
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
require './main'
run Sinatra::Application
run Sinatra::Application
28 changes: 11 additions & 17 deletions examples/ruby-api/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,20 @@
Dotenv.load '.env'
set :show_exceptions, false


class InvalidTokenError < StandardError;
class InvalidTokenError < StandardError
end

def validate_token(env)
auth0_client_id = ENV['AUTH0_CLIENT_ID']
auth0_client_secret = ENV['AUTH0_CLIENT_SECRET']
authorization = env['HTTP_AUTHORIZATION']

raise InvalidTokenError if authorization.nil?

token = authorization.split(' ').last
decoded_token = JWT.decode(token,
JWT.base64url_decode(auth0_client_secret))

raise InvalidTokenError if auth0_client_id != decoded_token[0]['aud']
'You get this only if authenticated'
rescue JWT::DecodeError
raise InvalidTokenError
end
auth0_client_id = ENV['AUTH0_CLIENT_ID']
auth0_client_secret = ENV['AUTH0_CLIENT_SECRET']
authorization = env['HTTP_AUTHORIZATION']
raise InvalidTokenError if authorization.nil?
decoded_token = JWT.decode(authorization.split(' ').last, JWT.base64url_decode(auth0_client_secret))
raise InvalidTokenError if auth0_client_id != decoded_token[0]['aud']
'You get this only if authenticated'
rescue JWT::DecodeError
raise InvalidTokenError
end

error InvalidTokenError do
'Invalid token'
Expand Down
12 changes: 5 additions & 7 deletions examples/ruby-on-rails-api/Gemfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.5.1'
# Use sqlite3 as the database for Active Record
gem 'sqlite3', :groups => [:development, :test]
gem 'sqlite3', groups: [:development, :test]

gem 'pg'
# Use SCSS for stylesheets
Expand All @@ -23,16 +22,15 @@ gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.4.1'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.1', group: :doc
gem 'sdoc', '~> 0.4.1', group: :doc
# knock dependency
gem 'knock', '~> 1.4.2'

#Dot env
gem 'dotenv-rails', :groups => [:development, :test]

# Dot env
gem 'dotenv-rails', groups: [:development, :test]

# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring', group: :development
gem 'spring', group: :development

gem 'pry', group: [:development, :test]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Application Controller
class ApplicationController < ActionController::Base
include Knock::Authenticable

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Ping Controller
class PingController < ApplicationController

def ping
render text: "All good. You don't need to be authenticated to call this"
end

end
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Secured ping Controller
class SecuredPingController < ApplicationController
before_action :authenticate

def ping
render :json => {
:message => "All good. You only get this message if you're authenticated.",
:user => @current_user
}
render json: {
message: "All good. You only get this message if you're authenticated.",
user: @current_user
}
end

end
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Application Helper
module ApplicationHelper
end
16 changes: 8 additions & 8 deletions examples/ruby-on-rails-api/bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@
require 'pathname'

# path to your application root.
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)

Dir.chdir APP_ROOT do
# This script is a starting point to setup your application.
# Add necessary setup steps to this file:

puts "== Installing dependencies =="
system "gem install bundler --conservative"
system "bundle check || bundle install"
puts '== Installing dependencies =='
system 'gem install bundler --conservative'
system 'bundle check || bundle install'

# puts "\n== Copying sample files =="
# unless File.exist?("config/database.yml")
# system "cp config/database.yml.sample config/database.yml"
# end

puts "\n== Preparing database =="
system "bin/rake db:setup"
system 'bin/rake db:setup'

puts "\n== Removing old logs and tempfiles =="
system "rm -f log/*"
system "rm -rf tmp/cache"
system 'rm -f log/*'
system 'rm -rf tmp/cache'

puts "\n== Restarting application server =="
system "touch tmp/restart.txt"
system 'touch tmp/restart.txt'
end
2 changes: 1 addition & 1 deletion examples/ruby-on-rails-api/config.ru
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file is used by Rack-based servers to start the application.

require ::File.expand_path('../config/environment', __FILE__)
require ::File.expand_path('../config/environment', __FILE__)
run Rails.application
1 change: 0 additions & 1 deletion examples/ruby-on-rails-api/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

module Auth0RorapiSample
class Application < Rails::Application

# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
Expand Down
2 changes: 1 addition & 1 deletion examples/ruby-on-rails-api/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
config.eager_load = false

# Configure static asset server for tests with Cache-Control for performance.
config.serve_static_files = true
config.serve_static_files = true
config.static_cache_control = 'public, max-age=3600'

# Show full error reports and disable caching.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Be sure to restart your server when you modify this file.

Rails.application.config.action_dispatch.cookies_serializer = :json
Rails.application.config.action_dispatch.cookies_serializer = :json
10 changes: 3 additions & 7 deletions examples/ruby-on-rails-api/config/initializers/knock.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require 'base64'
Knock.setup do |config|

## Current user retrieval when validating token
## --------------------------------------------
##
Expand All @@ -14,7 +13,7 @@
# !!!
# This is only to make the example test cases pass, you should use a real
# user model in your app instead.
config.current_user_from_token = -> (claims) {{ id: claims['sub'] }}
config.current_user_from_token = -> (claims) { { id: claims['sub'] } }

## Expiration claim
## ----------------
Expand All @@ -24,7 +23,6 @@
## Default:
# config.token_lifetime = 1.day


## Audience claim
## --------------
##
Expand All @@ -37,7 +35,6 @@
## If using Auth0, uncomment the line below
config.token_audience = -> { Rails.application.secrets.auth0_client_id }


## Signature key
## -------------
##
Expand All @@ -47,11 +44,10 @@
# config.token_secret_signature_key = -> { Rails.application.secrets.secret_key_base }

## If using Auth0, uncomment the line below
#config.token_secret_signature_key = -> { JWT.base64url_decode Rails.application.secrets.auth0_client_secret }
config.token_secret_signature_key = -> {
# config.token_secret_signature_key = -> { JWT.base64url_decode Rails.application.secrets.auth0_client_secret }
config.token_secret_signature_key = lambda {
secret = Rails.application.secrets.auth0_client_secret
secret += '=' * (4 - secret.length.modulo(4))
Base64.decode64(secret.tr('-_', '+/'))
}

end
1 change: 0 additions & 1 deletion examples/ruby-on-rails-api/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Rails.application.routes.draw do

get 'ping' => 'ping#ping'
get 'secured/ping' => 'secured_ping#ping'
# The priority is based upon order of creation: first created -> highest priority.
Expand Down
1 change: 0 additions & 1 deletion examples/ruby-on-rails-api/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 0) do

end
4 changes: 1 addition & 3 deletions examples/ruby-on-rails-api/test/ping_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
require 'test_helper'

# Ping Controller Tests
class PingControllerTest < ActionController::TestCase

test 'responds with success' do
get :ping
assert_response :success
end

end
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'test_helper'

# Secure Ping Controller Test
class SecuredPingControllerTest < ActionController::TestCase

def with_a_valid_token
@user = { id: 1 }
@token = Knock::AuthToken.new(payload: { sub: @user[:id] }).token
Expand Down
19 changes: 11 additions & 8 deletions examples/ruby-on-rails-api/test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
# Active Support
class ActiveSupport
# Test Case
class TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
fixtures :all

class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
fixtures :all

# Add more helper methods to be used by all tests here...
# Add more helper methods to be used by all tests here...
end
end
Loading

0 comments on commit 73c62b8

Please sign in to comment.