devise-auth0 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/CHANGELOG +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +41 -0
- data/Rakefile +2 -0
- data/devise-auth0.gemspec +23 -0
- data/lib/devise/auth0.rb +10 -0
- data/lib/devise/auth0/failure_app.rb +21 -0
- data/lib/devise/auth0/version.rb +5 -0
- data/lib/devise/models/auth0_authenticatable.rb +35 -0
- data/lib/devise/strategies/auth0_authenticatable.rb +46 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 09d2c52435f4724971a7010be7afdeb821f2c179
|
4
|
+
data.tar.gz: fefa985762c18269d3df02d61d2522a0842fdf62
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e250b8ae551213bf6f78900f838db70d369530068485b19e92710b93a5a6fcfef56e50674b167d7d1bbde974bb81a5d8713a14aa9fd47c4a5ad59ee0c603a240
|
7
|
+
data.tar.gz: fc954f7f8ecab7a2b0604e343b26bc332c5fd3ed9c38498ba7806f80c4ef77abf2463e77f303010ed68210c29111679c2697f26b9d7fc999608c44f31a9e564e
|
data/.gitignore
ADDED
data/CHANGELOG
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Derek Kastner
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Devise::Auth0
|
2
|
+
|
3
|
+
Allow signed-in auth0 users to areas protected by devise.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'devise-auth0'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install devise-auth0
|
20
|
+
|
21
|
+
## Configuration
|
22
|
+
|
23
|
+
In `config/initializers/devise.rb`:
|
24
|
+
|
25
|
+
``` ruby
|
26
|
+
require 'devise/strategies/auth0_authenticatable'
|
27
|
+
|
28
|
+
Devise.setup do |config|
|
29
|
+
config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com'
|
30
|
+
|
31
|
+
require 'devise/orm/active_record'
|
32
|
+
|
33
|
+
# this lets you use the login_as helper in tests
|
34
|
+
config.skip_session_storage = [:auth0_authenticatable] unless Rails.env.test?
|
35
|
+
|
36
|
+
config.warden do |manager|
|
37
|
+
manager.strategies.add(:auth0_authenticatable, Devise::Strategies::Auth0Authenticatable)
|
38
|
+
manager.default_strategies(scope: :user).unshift :auth0_authenticatable
|
39
|
+
end
|
40
|
+
end
|
41
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'devise/auth0/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "devise-auth0"
|
8
|
+
spec.version = Devise::Auth0::VERSION
|
9
|
+
spec.authors = ["Derek Kastner"]
|
10
|
+
spec.email = ["dkastner@gmail.com"]
|
11
|
+
spec.summary = %q{Integrate devise with auth0}
|
12
|
+
spec.description = %q{Allow logged-in auth0 users to access protected areas}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
data/lib/devise/auth0.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require "devise/auth0/version"
|
2
|
+
require 'devise/models/auth0_authenticatable'
|
3
|
+
require 'devise/strategies/auth0_authenticatable'
|
4
|
+
|
5
|
+
module Devise
|
6
|
+
module Auth0
|
7
|
+
CLIENT_ID = ENV.fetch 'AUTH0_CLIENT_ID'
|
8
|
+
SECRET = Base64.decode64 ENV.fetch('AUTH0_SECRET').gsub('-', '+').gsub('_','/')
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Devise
|
2
|
+
module Auth0
|
3
|
+
|
4
|
+
class FailureApp < Devise::FailureApp
|
5
|
+
def respond
|
6
|
+
if request.format == :json
|
7
|
+
json_failure
|
8
|
+
else
|
9
|
+
super
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def json_failure
|
14
|
+
self.status = 401
|
15
|
+
self.content_type = 'application/json'
|
16
|
+
self.response_body = { error: warden.message }.to_json
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'devise'
|
2
|
+
|
3
|
+
module Devise
|
4
|
+
module Models
|
5
|
+
|
6
|
+
module Auth0Authenticatable
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
class MissingAuth0Id < StandardError; end
|
10
|
+
class MissingAccount < StandardError; end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
|
14
|
+
def find_or_sync_auth0(info)
|
15
|
+
unless uid = info['user_id']
|
16
|
+
raise MissingAuth0Id.new(info),
|
17
|
+
"Expected auth0_user_id, got none inside of #{@info.inspect}"
|
18
|
+
end
|
19
|
+
|
20
|
+
if user = User.find_by(auth0_user_id: uid)
|
21
|
+
# cool
|
22
|
+
elsif user = User.find_by(email: info['email'])
|
23
|
+
name = user.name || info['name']
|
24
|
+
user.update! auth0_user_id: uid, name: name
|
25
|
+
end
|
26
|
+
|
27
|
+
user
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'devise'
|
2
|
+
|
3
|
+
module Devise
|
4
|
+
module Strategies
|
5
|
+
|
6
|
+
class Auth0Authenticatable < Base
|
7
|
+
|
8
|
+
def authenticate!
|
9
|
+
token = env['HTTP_AUTHORIZATION'].to_s.gsub('Bearer ', '')
|
10
|
+
|
11
|
+
begin
|
12
|
+
decoded_token, header = JWT.decode(token, Devise::Auth0::SECRET)
|
13
|
+
rescue JWT::DecodeError
|
14
|
+
Rails.logger.warn 'Unreadable Auth0 token'
|
15
|
+
fail! 'Unreadable Auth0 token'
|
16
|
+
return
|
17
|
+
end
|
18
|
+
|
19
|
+
if not decoded_token.is_a?(Hash)
|
20
|
+
Rails.logger.warn "Unexpected Auth0 token structure: expected Hash, got #{decoded_token.inspect}"
|
21
|
+
fail! "Unexpected Auth0 token structure: expected Hash, got #{decoded_token.inspect}"
|
22
|
+
return
|
23
|
+
end
|
24
|
+
|
25
|
+
if decoded_token['aud'] == Auth0::CLIENT_ID
|
26
|
+
user = mapping.to.find_or_sync_auth0(decoded_token)
|
27
|
+
success! user
|
28
|
+
return
|
29
|
+
end
|
30
|
+
|
31
|
+
Rails.logger.info "Invalid token"
|
32
|
+
fail! 'Invalid token'
|
33
|
+
end
|
34
|
+
|
35
|
+
def store?
|
36
|
+
false
|
37
|
+
end
|
38
|
+
|
39
|
+
def valid?
|
40
|
+
env['HTTP_AUTHORIZATION'].present?
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: devise-auth0
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Derek Kastner
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: Allow logged-in auth0 users to access protected areas
|
42
|
+
email:
|
43
|
+
- dkastner@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- CHANGELOG
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- devise-auth0.gemspec
|
55
|
+
- lib/devise/auth0.rb
|
56
|
+
- lib/devise/auth0/failure_app.rb
|
57
|
+
- lib/devise/auth0/version.rb
|
58
|
+
- lib/devise/models/auth0_authenticatable.rb
|
59
|
+
- lib/devise/strategies/auth0_authenticatable.rb
|
60
|
+
homepage: ''
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata: {}
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 2.2.2
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Integrate devise with auth0
|
84
|
+
test_files: []
|