Skip to content

Commit 0e8721e

Browse files
committed
Basic Auth0 strategy based on OAuth 2.0
- Require client_id, client_secret and domain on init - Use auth0 paths for OAuth - Handled non-http domain - Guardfile and test
1 parent 00b9552 commit 0e8721e

File tree

6 files changed

+92
-11
lines changed

6 files changed

+92
-11
lines changed

.rubocop.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Style/BlockLength:
2+
Exclude:
3+
- 'Rakefile'
4+
- '**/*.rake'
5+
- 'spec/**/*.rb'

Guardfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
guard :rspec, cmd: 'bundle exec rspec' do
22
watch(%r{^spec/.+_spec\.rb$})
3-
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
3+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
44
watch('spec/spec_helper.rb') { "spec" }
55
end

Rakefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,3 @@ task test: :spec
2626
task :guard do
2727
system 'bundle exec guard'
2828
end
29-
end

examples/sinatra/app.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
require 'omniauth-auth0'
33
require 'dotenv/load'
44

5-
use Rack::Session::Cookie
65
use OmniAuth::Builder do
7-
provider :auth0, ENV['CLIENT_ID'], ENV['CLIENT_SECRET']
6+
provider :auth0, ENV['CLIENT_ID'], ENV['CLIENT_SECRET'], ENV['DOMAIN']
87
end
98

9+
enable :sessions
1010
set :session_secret, ENV['SESSION_SECRET']
1111

1212
get '/' do

lib/omniauth/strategies/auth0.rb

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'base64'
2+
require 'uri'
23
require 'omniauth-oauth2'
34

45
module OmniAuth
@@ -7,17 +8,20 @@ module Strategies
78
class Auth0 < OmniAuth::Strategies::OAuth2
89
option :name, 'auth0'
910

10-
option :client_options, {
11-
authorize_url: '/authorize',
12-
token_url: '/oauth/token',
13-
userinfo_url: '/userinfo'
14-
}
15-
1611
args [
1712
:client_id,
18-
:client_secret
13+
:client_secret,
14+
:domain
1915
]
2016

17+
def client
18+
options.client_options.site = domain_url
19+
options.client_options.authorize_url = '/authorize'
20+
options.client_options.token_url = '/oauth/token'
21+
options.client_options.userinfo_url = '/userinfo'
22+
super
23+
end
24+
2125
uid { raw_info['sub'] }
2226

2327
extra do
@@ -27,6 +31,14 @@ class Auth0 < OmniAuth::Strategies::OAuth2
2731
info do
2832
{}
2933
end
34+
35+
private
36+
37+
def domain_url
38+
domain_url = URI(options.domain)
39+
domain_url = URI("https://#{domain_url}") if domain_url.scheme.nil?
40+
domain_url.to_s
41+
end
3042
end
3143
end
3244
end
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
require 'spec_helper'
2+
3+
RSpec.shared_examples 'site has valid domain url' do |url|
4+
it { expect(subject.site).to eq(url) }
5+
end
6+
7+
describe OmniAuth::Strategies::Auth0 do
8+
let(:client_id) { 'CLIENT_ID' }
9+
let(:client_secret) { 'CLIENT_SECRET' }
10+
let(:domain_url) { 'https://samples.auth0.com' }
11+
let(:app) do
12+
lambda do
13+
[200, {}, ['Hello.']]
14+
end
15+
end
16+
let(:auth0) do
17+
OmniAuth::Strategies::Auth0.new(app, client_id, client_secret, domain_url)
18+
end
19+
20+
describe 'client_options' do
21+
let(:subject) { auth0.client }
22+
23+
context 'domain with https' do
24+
let(:domain_url) { 'https://samples.auth0.com' }
25+
it_behaves_like 'site has valid domain url', 'https://samples.auth0.com'
26+
end
27+
28+
context 'domain with http' do
29+
let(:domain_url) { 'http://mydomain.com' }
30+
it_behaves_like 'site has valid domain url', 'http://mydomain.com'
31+
end
32+
33+
context 'domain with host only' do
34+
let(:domain_url) { 'samples.auth0.com' }
35+
it_behaves_like 'site has valid domain url', 'https://samples.auth0.com'
36+
end
37+
38+
it 'should have correct authorize path' do
39+
expect(subject.options[:authorize_url]).to eq('/authorize')
40+
end
41+
42+
it 'should have the correct userinfo path' do
43+
expect(subject.options[:userinfo_url]).to eq('/userinfo')
44+
end
45+
46+
it 'should have the correct token path' do
47+
expect(subject.options[:token_url]).to eq('/oauth/token')
48+
end
49+
end
50+
51+
describe 'options' do
52+
let(:subject) { auth0.options }
53+
54+
it 'should have the correct client_id' do
55+
expect(subject[:client_id]).to eq(client_id)
56+
end
57+
58+
it 'should have the correct client secret' do
59+
expect(subject[:client_secret]).to eq(client_secret)
60+
end
61+
it 'should have correct domain' do
62+
expect(subject[:domain]).to eq(domain_url)
63+
end
64+
end
65+
end

0 commit comments

Comments
 (0)