rack-ninja_auth 0.3.2 → 0.4.0
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 +4 -4
- data/examples/gmail.rb +1 -1
- data/examples/routes.rb +28 -0
- data/lib/rack/ninja_auth/version.rb +1 -1
- data/lib/rack/ninja_auth.rb +12 -3
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 26ae06c994d3746b12aa3ab8ee7148f609e67058
|
4
|
+
data.tar.gz: b2816854ca67f13fbf820dd68813e148f9281173
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c6245f3cdeccab15c5bb9988d88e8f3f40ff7953b141ad046e67e81a81e8dbdd77ea97ad6288d42bffa74f3837e049fe7e1d38ae4c33bce795be3b20de16b06a
|
7
|
+
data.tar.gz: 5b2942e0cf1d97e471dab619283d4fbfe55b0efda9b296c263f2f9b1e73c4d5510a80c0f7e8f01707bea11292978126925b40acc6bf57a09dc5b92da2b9cfab3
|
data/examples/gmail.rb
CHANGED
@@ -13,7 +13,7 @@ $LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
|
|
13
13
|
require 'sinatra'
|
14
14
|
require 'rack/ninja_auth'
|
15
15
|
|
16
|
-
use Rack::NinjaAuth::Middleware, /@gmail.com$/
|
16
|
+
use Rack::NinjaAuth::Middleware, email_matcher: /@gmail.com$/
|
17
17
|
# use Rack::NinjaAuth::Middleware, /@gmail\./, './file/to/deliver/if/email/does/not/match.html'
|
18
18
|
|
19
19
|
get '/secured' do
|
data/examples/routes.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
|
2
|
+
# Make sure you run this with the appropriate environment variables set:
|
3
|
+
#
|
4
|
+
# ```
|
5
|
+
# NINJA_GOOGLE_CLIENT_ID=<Your google client id>
|
6
|
+
# NINJA_GOOGLE_CLIENT_SECRET=<Your google client secret>
|
7
|
+
# ruby gmail.rb
|
8
|
+
# ```
|
9
|
+
#
|
10
|
+
# Now you can visit `http://127.0.0.1:4567/secured` and will only access it if you validate with a google
|
11
|
+
# account that has an `@gmail.com` email address.
|
12
|
+
|
13
|
+
require 'sinatra'
|
14
|
+
require 'rack/ninja_auth'
|
15
|
+
|
16
|
+
use Rack::NinjaAuth::Middleware, email_matcher: /@gmail.com$/, secured_routes: %r{^/secured}
|
17
|
+
|
18
|
+
get '/secured' do
|
19
|
+
"You hit the secured app"
|
20
|
+
end
|
21
|
+
|
22
|
+
get '/secured/extra' do
|
23
|
+
"More from the secured app"
|
24
|
+
end
|
25
|
+
|
26
|
+
get '/' do
|
27
|
+
"You can get this always"
|
28
|
+
end
|
data/lib/rack/ninja_auth.rb
CHANGED
@@ -17,17 +17,18 @@ module Rack
|
|
17
17
|
provider :google_oauth2, ENV["NINJA_GOOGLE_CLIENT_ID"], ENV["NINJA_GOOGLE_CLIENT_SECRET"]
|
18
18
|
end
|
19
19
|
|
20
|
-
def initialize(app, email_matcher
|
20
|
+
def initialize(app, email_matcher: //, secured_routes: //, not_allowed_file: nil)
|
21
21
|
puts "Please set NINJA_GOOGLE_CLIENT_ID and NINJA_GOOGLE_CLIENT_SECRET to use NinjaAuth" unless ENV["NINJA_GOOGLE_CLIENT_ID"] && ENV["NINJA_GOOGLE_CLIENT_SECRET"]
|
22
22
|
@main_app = app
|
23
23
|
@email_matcher = email_matcher
|
24
|
+
@secured_route_matcher = secured_routes
|
24
25
|
@not_allowed_file = not_allowed_file || ::File.expand_path('../../../views/401.html', __FILE__)
|
25
26
|
super()
|
26
27
|
end
|
27
28
|
|
28
29
|
before do
|
29
30
|
@hit_real_app = false
|
30
|
-
if is_authenticated?
|
31
|
+
if is_authenticated? || !is_protected_request?
|
31
32
|
res = @main_app.call(request.env)
|
32
33
|
@hit_real_app = true
|
33
34
|
headers res[1]
|
@@ -51,7 +52,7 @@ module Rack
|
|
51
52
|
after do
|
52
53
|
if !@hit_real_app && status == 404
|
53
54
|
halt(403) unless env['rack-accept.request'].media_type?('text/html')
|
54
|
-
session[:redirect_to] =
|
55
|
+
session[:redirect_to] = is_internal_request? ? '/' : env['REQUEST_URI']
|
55
56
|
redirect '/auth/google_oauth2'
|
56
57
|
end
|
57
58
|
end
|
@@ -61,6 +62,14 @@ module Rack
|
|
61
62
|
def is_authenticated?
|
62
63
|
!session[:user].nil?
|
63
64
|
end
|
65
|
+
|
66
|
+
def is_protected_request?
|
67
|
+
is_internal_request? || env['PATH_INFO'].match(@secured_route_matcher)
|
68
|
+
end
|
69
|
+
|
70
|
+
def is_internal_request?
|
71
|
+
env['REQUEST_URI'] =~ %r{^/auth/}
|
72
|
+
end
|
64
73
|
end
|
65
74
|
end
|
66
75
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-ninja_auth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- JP Hastings-Spital
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-09-
|
11
|
+
date: 2015-09-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sinatra
|
@@ -125,6 +125,7 @@ files:
|
|
125
125
|
- README.md
|
126
126
|
- Rakefile
|
127
127
|
- examples/gmail.rb
|
128
|
+
- examples/routes.rb
|
128
129
|
- lib/rack/ninja_auth.rb
|
129
130
|
- lib/rack/ninja_auth/version.rb
|
130
131
|
- rack-ninja_auth.gemspec
|