bc-require-google-auth 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +41 -0
- data/Rakefile +1 -0
- data/bc-require-google-auth.gemspec +23 -0
- data/lib/bc/require_google_auth.rb +74 -0
- data/lib/bc/require_google_auth/version.rb +5 -0
- metadata +101 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Stephen Crosby
|
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
|
+
# Bc::RequireGoogleAuth
|
2
|
+
|
3
|
+
This gem is a Rack middleware that requires users to authenticate
|
4
|
+
through google in order to access the protected portions of the
|
5
|
+
application.
|
6
|
+
|
7
|
+
Currently, access is controlled with a hardcoded list of google account
|
8
|
+
email addresses in the middlware initializer.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
gem 'bc-require-google-auth'
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install bc-require-google-auth
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
require 'bc/require_google_auth'
|
28
|
+
|
29
|
+
use Bc::RequireGoogleAuth, allowed_paths: [ "/" ], authorized_emails: [
|
30
|
+
"stephen@brandedcrate.com",
|
31
|
+
"otherallowedemail@gmail.com"
|
32
|
+
]
|
33
|
+
```
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
1. Fork it ( http://github.com/brandedcrate/bc-require-google-auth/fork )
|
38
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
39
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
40
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
41
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -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 'bc/require_google_auth/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "bc-require-google-auth"
|
8
|
+
spec.version = Bc::RequireGoogleAuth::VERSION
|
9
|
+
spec.authors = ["Stephen Crosby"]
|
10
|
+
spec.email = ["stephen@brandedcrate.com"]
|
11
|
+
spec.summary = %q{Rack middleware to require google auth}
|
12
|
+
spec.description = %q{Forces users to login through google OAuth2 using Rack::Session and OmniAuth}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'bc/require_google_auth/version'
|
2
|
+
|
3
|
+
module Bc
|
4
|
+
class RequireGoogleAuth
|
5
|
+
|
6
|
+
DEFAULT_ALLOWED_PATHS = [
|
7
|
+
"/auth/google_oauth2/callback",
|
8
|
+
"/auth/google_oauth2"
|
9
|
+
].freeze
|
10
|
+
|
11
|
+
DEFAULT_SESSION_KEY = 'bc.auth'.freeze
|
12
|
+
|
13
|
+
DEFAULT_AFTER_AUTH_PATH = '/'.freeze
|
14
|
+
|
15
|
+
OMNIAUTH_SESSION_KEY = 'omniauth.auth'.freeze
|
16
|
+
|
17
|
+
def initialize(app, opts={})
|
18
|
+
@allowed_paths = opts[:allowed_paths] || DEFAULT_ALLOWED_PATHS
|
19
|
+
@session_key = opts[:session_key] || DEFAULT_SESSION_KEY
|
20
|
+
@authorized_emails = opts[:authorized_emails]
|
21
|
+
@after_auth_path = opts[:after_auth_path] || DEFAULT_AFTER_AUTH_PATH
|
22
|
+
@app = app
|
23
|
+
end
|
24
|
+
|
25
|
+
def auth_callback?(req)
|
26
|
+
return false unless req.path == '/auth/google_oauth2/callback'
|
27
|
+
return false unless req.env[OMNIAUTH_SESSION_KEY]
|
28
|
+
return false unless req.env[OMNIAUTH_SESSION_KEY][:info]
|
29
|
+
return true
|
30
|
+
end
|
31
|
+
|
32
|
+
def allowed_path?(req)
|
33
|
+
@allowed_paths.include?(req.path)
|
34
|
+
end
|
35
|
+
|
36
|
+
def authorized_session?(req)
|
37
|
+
!!req.session[@session_key]
|
38
|
+
end
|
39
|
+
|
40
|
+
def authorized_email?(req)
|
41
|
+
@authorized_emails.include?(req.env[OMNIAUTH_SESSION_KEY][:info][:email])
|
42
|
+
end
|
43
|
+
|
44
|
+
def handle_unauthorized
|
45
|
+
res = Rack::Response.new
|
46
|
+
res.redirect '/auth/google_oauth2', status=302
|
47
|
+
res.finish
|
48
|
+
end
|
49
|
+
|
50
|
+
def handle_auth_callback(req)
|
51
|
+
if authorized_email?(req)
|
52
|
+
req.session[@session_key] = req.env[OMNIAUTH_SESSION_KEY][:info]
|
53
|
+
else
|
54
|
+
req.session.delete(@session_key)
|
55
|
+
end
|
56
|
+
|
57
|
+
res = Rack::Response.new
|
58
|
+
res.redirect @after_auth_path, status=302
|
59
|
+
res.finish
|
60
|
+
end
|
61
|
+
|
62
|
+
def call(env)
|
63
|
+
req = Rack::Request.new(env)
|
64
|
+
|
65
|
+
if auth_callback?(req)
|
66
|
+
handle_auth_callback(req)
|
67
|
+
elsif authorized_session?(req) || allowed_path?(req)
|
68
|
+
@app.call(env)
|
69
|
+
else
|
70
|
+
handle_unauthorized
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bc-require-google-auth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Stephen Crosby
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2014-02-24 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: bundler
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: rake
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id002
|
48
|
+
description: Forces users to login through google OAuth2 using Rack::Session and OmniAuth
|
49
|
+
email:
|
50
|
+
- stephen@brandedcrate.com
|
51
|
+
executables: []
|
52
|
+
|
53
|
+
extensions: []
|
54
|
+
|
55
|
+
extra_rdoc_files: []
|
56
|
+
|
57
|
+
files:
|
58
|
+
- .gitignore
|
59
|
+
- Gemfile
|
60
|
+
- LICENSE.txt
|
61
|
+
- README.md
|
62
|
+
- Rakefile
|
63
|
+
- bc-require-google-auth.gemspec
|
64
|
+
- lib/bc/require_google_auth.rb
|
65
|
+
- lib/bc/require_google_auth/version.rb
|
66
|
+
homepage: ""
|
67
|
+
licenses:
|
68
|
+
- MIT
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
hash: 3
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
hash: 3
|
89
|
+
segments:
|
90
|
+
- 0
|
91
|
+
version: "0"
|
92
|
+
requirements: []
|
93
|
+
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 1.8.24
|
96
|
+
signing_key:
|
97
|
+
specification_version: 3
|
98
|
+
summary: Rack middleware to require google auth
|
99
|
+
test_files: []
|
100
|
+
|
101
|
+
has_rdoc:
|