signet-rails 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 +37 -0
- data/Rakefile +1 -0
- data/lib/signet/rails/builder.rb +74 -0
- data/lib/signet/rails/factory.rb +25 -0
- data/lib/signet/rails/handler.rb +90 -0
- data/lib/signet/rails/version.rb +5 -0
- data/lib/signet/rails/wrappers/active_record.rb +22 -0
- data/lib/signet/rails.rb +7 -0
- data/signet-rails.gemspec +25 -0
- metadata +122 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Paul Jolly
|
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,37 @@
|
|
1
|
+
# Signet::Rails
|
2
|
+
|
3
|
+
A basic Rails wrapper around the "Signet":https://github.com/google/signet gem
|
4
|
+
|
5
|
+
Work in progress
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'signet-rails'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
```bash
|
18
|
+
$ bundle
|
19
|
+
```
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
```bash
|
24
|
+
$ gem install signet-rails
|
25
|
+
```
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
TODO: Write usage instructions here
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
1. Fork it
|
34
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
36
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
37
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'signet/rails'
|
2
|
+
require 'signet/rails/wrappers/active_record'
|
3
|
+
|
4
|
+
module Signet
|
5
|
+
module Rails
|
6
|
+
class Builder < ::Rack::Builder
|
7
|
+
@@default_options = {}
|
8
|
+
|
9
|
+
def self.set_default_options opts = {}
|
10
|
+
# normalize to symbol version
|
11
|
+
n_opts = opts.inject({}) { |memo,(k,v)| memo[k.to_sym] = v; memo }
|
12
|
+
@@default_options = n_opts
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize(app, &block)
|
16
|
+
super
|
17
|
+
end
|
18
|
+
|
19
|
+
def provider(opts = {}, &block)
|
20
|
+
# normalize to symbol version
|
21
|
+
n_opts = opts.inject({}) { |memo,(k,v)| memo[k.to_sym] = v; memo }
|
22
|
+
combined_options = @@default_options.merge(n_opts)
|
23
|
+
|
24
|
+
# now set some defaults if they aren't already set
|
25
|
+
combined_options[:persist] ||= [:refresh_token, :access_token, :expires_in, :issued_at]
|
26
|
+
combined_options[:name] ||= :google
|
27
|
+
combined_options[:approval_prompt] ||= 'auto'
|
28
|
+
combined_options[:redirect_uri] = "http://localhost:3000/signet/#{combined_options[:name]}/callback"
|
29
|
+
combined_options[:authorization_uri] = 'https://accounts.google.com/o/oauth2/auth'
|
30
|
+
combined_options[:token_credential_uri] = 'https://accounts.google.com/o/oauth2/token'
|
31
|
+
combined_options[:extract_from_env] ||= lambda do |env, client|
|
32
|
+
u = nil
|
33
|
+
session = env['rack.session']
|
34
|
+
if !!session && !!session[:user_id]
|
35
|
+
begin
|
36
|
+
u = User.find(session[:user_id])
|
37
|
+
rescue ActiveRecord::RecordNotFound => e
|
38
|
+
end
|
39
|
+
end
|
40
|
+
u
|
41
|
+
end
|
42
|
+
combined_options[:handle_callback] ||= true
|
43
|
+
combined_options[:extract_by_oauth_id] ||= lambda do |id, client|
|
44
|
+
u = nil
|
45
|
+
begin
|
46
|
+
u = User.where(uid: id).first_or_initialize(
|
47
|
+
refresh_token: client.refresh_token
|
48
|
+
)
|
49
|
+
rescue ActiveRecord::RecordNotFound => e
|
50
|
+
end
|
51
|
+
u
|
52
|
+
end
|
53
|
+
|
54
|
+
def active_record_wrapper meth
|
55
|
+
lambda do |context, client|
|
56
|
+
y = meth.call context, client
|
57
|
+
w = Signet::Rails::Wrappers::ActiveRecord.new y, client
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
combined_options[:extract_by_oauth_id] = active_record_wrapper combined_options[:extract_by_oauth_id]
|
62
|
+
combined_options[:extract_from_env] = active_record_wrapper combined_options[:extract_from_env]
|
63
|
+
|
64
|
+
# TODO: check here we have the basics?
|
65
|
+
|
66
|
+
use Signet::Rails::Handler, combined_options, &block
|
67
|
+
end
|
68
|
+
|
69
|
+
def call(env)
|
70
|
+
to_app.call(env)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'signet/oauth_2'
|
2
|
+
|
3
|
+
module Signet
|
4
|
+
module Rails
|
5
|
+
|
6
|
+
class Factory
|
7
|
+
def self.create_from_env name, env, opt_hsh = {load_token: true}
|
8
|
+
# rework this to use singleton?
|
9
|
+
handler = env["signet.#{name.to_s}"]
|
10
|
+
|
11
|
+
client = Signet::OAuth2::Client.new handler.options
|
12
|
+
|
13
|
+
if opt_hsh[:load_token]
|
14
|
+
obj = handler.options[:extract_from_env].call env, client
|
15
|
+
handler.load_token_state obj, client
|
16
|
+
|
17
|
+
env["signet.#{name.to_s}.instance"] = obj
|
18
|
+
end
|
19
|
+
|
20
|
+
client
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'signet/oauth_2'
|
2
|
+
require 'signet/rails'
|
3
|
+
require 'rack/utils'
|
4
|
+
|
5
|
+
module Signet
|
6
|
+
module Rails
|
7
|
+
class Handler
|
8
|
+
def initialize(app, opts = {}, &block)
|
9
|
+
@app = app
|
10
|
+
@options = opts
|
11
|
+
end
|
12
|
+
|
13
|
+
def options
|
14
|
+
@options
|
15
|
+
end
|
16
|
+
|
17
|
+
def handle(env)
|
18
|
+
|
19
|
+
if "/signet/#{@options[:name]}/login" == env['PATH_INFO'] && 'GET' == env['REQUEST_METHOD']
|
20
|
+
client = Factory.create_from_env @options[:name], env
|
21
|
+
r = Rack::Response.new
|
22
|
+
|
23
|
+
# TODO: a better way of filtering down to the auth options
|
24
|
+
auth_options_whitelist = [:approval_prompt]
|
25
|
+
auth_options = @options.select { |k,_| auth_options_whitelist.include?(k) }
|
26
|
+
redirect_uri = client.authorization_uri(auth_options).to_s
|
27
|
+
r.redirect(redirect_uri)
|
28
|
+
r.finish
|
29
|
+
elsif "/signet/#{@options[:name]}/callback" == env['PATH_INFO']
|
30
|
+
client = Factory.create_from_env @options[:name], env, load_token: false
|
31
|
+
query_string_params = Rack::Utils.parse_query(env['QUERY_STRING'])
|
32
|
+
client.code = query_string_params['code']
|
33
|
+
fat = client.fetch_access_token!
|
34
|
+
|
35
|
+
if @options[:handle_callback]
|
36
|
+
# try to get the token store
|
37
|
+
obj = @options[:extract_by_oauth_id].call client.decoded_id_token['id'], client
|
38
|
+
persist_token_state obj, client
|
39
|
+
obj.persist
|
40
|
+
env["signet.#{options[:name]}.obj"] = obj.obj
|
41
|
+
|
42
|
+
else
|
43
|
+
# pass this on for handling by the rails app
|
44
|
+
env["signet.#{options[:name]}.client"] = client
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
[nil,nil,nil]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def persist_token_state wrapper, client
|
53
|
+
for i in @options[:persist]
|
54
|
+
if client.respond_to?(i) && wrapper.obj.respond_to?(i.to_s+'=')
|
55
|
+
wrapper.obj.method(i.to_s+'=').call(client.method(i).call)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def load_token_state wrapper, client
|
61
|
+
for i in @options[:persist]
|
62
|
+
if wrapper.obj.respond_to?(i) && client.respond_to?(i.to_s+'=')
|
63
|
+
client.method(i.to_s+'=').call(wrapper.obj.method(i).call)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def call(env)
|
69
|
+
# rework this to use singleton?
|
70
|
+
env["signet.#{@options[:name]}"] = self
|
71
|
+
|
72
|
+
status, headers, body = handle(env)
|
73
|
+
|
74
|
+
unless status
|
75
|
+
|
76
|
+
status, headers, body = @app.call(env)
|
77
|
+
|
78
|
+
obj = env["signet.#{@options[:name]}.instance"]
|
79
|
+
if !!obj
|
80
|
+
obj.persist
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
[status, headers, body]
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Signet
|
2
|
+
module Rails
|
3
|
+
module Wrappers
|
4
|
+
class ActiveRecord
|
5
|
+
def initialize obj, client
|
6
|
+
@obj = obj
|
7
|
+
@client = client
|
8
|
+
end
|
9
|
+
|
10
|
+
def obj
|
11
|
+
@obj
|
12
|
+
end
|
13
|
+
|
14
|
+
def persist
|
15
|
+
if @obj.changed?
|
16
|
+
@obj.save
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/signet/rails.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'signet/rails/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "signet-rails"
|
8
|
+
spec.version = Signet::Rails::VERSION
|
9
|
+
spec.authors = ["Paul Jolly"]
|
10
|
+
spec.email = ["paul@myitcv.org.uk"]
|
11
|
+
spec.description = %q{A wrapper around the Google Signet OAuth Library}
|
12
|
+
spec.summary = %q{Incorporate Signet goodness into Rails}
|
13
|
+
spec.homepage = "https://github.com/myitcv/signet-rails"
|
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", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "signet"
|
24
|
+
spec.add_development_dependency "rack"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: signet-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Paul Jolly
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-06-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: signet
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rack
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: A wrapper around the Google Signet OAuth Library
|
79
|
+
email:
|
80
|
+
- paul@myitcv.org.uk
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE.txt
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- lib/signet/rails.rb
|
91
|
+
- lib/signet/rails/builder.rb
|
92
|
+
- lib/signet/rails/factory.rb
|
93
|
+
- lib/signet/rails/handler.rb
|
94
|
+
- lib/signet/rails/version.rb
|
95
|
+
- lib/signet/rails/wrappers/active_record.rb
|
96
|
+
- signet-rails.gemspec
|
97
|
+
homepage: https://github.com/myitcv/signet-rails
|
98
|
+
licenses:
|
99
|
+
- MIT
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ! '>='
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
requirements: []
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 1.8.25
|
119
|
+
signing_key:
|
120
|
+
specification_version: 3
|
121
|
+
summary: Incorporate Signet goodness into Rails
|
122
|
+
test_files: []
|