easy_auth-oauth2 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +51 -0
- data/Rakefile +7 -0
- data/app/models/identities/oauth2/base.rb +3 -0
- data/lib/easy_auth-oauth2.rb +1 -0
- data/lib/easy_auth/models/identities/oauth2.rb +4 -0
- data/lib/easy_auth/models/identities/oauth2/base.rb +99 -0
- data/lib/easy_auth/oauth2.rb +51 -0
- data/lib/easy_auth/oauth2/controllers.rb +4 -0
- data/lib/easy_auth/oauth2/controllers/sessions.rb +16 -0
- data/lib/easy_auth/oauth2/engine.rb +9 -0
- data/lib/easy_auth/oauth2/models.rb +4 -0
- data/lib/easy_auth/oauth2/models/account.rb +7 -0
- data/lib/easy_auth/oauth2/routes.rb +6 -0
- data/lib/easy_auth/oauth2/version.rb +5 -0
- metadata +196 -0
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# EasyAuth-Oauth2 #
|
2
|
+
|
3
|
+
[![Build Status](https://secure.travis-ci.org/dockyard/easy_auth-oauth2.png?branch=master)](http://travis-ci.org/dockyard/easy_auth-oauth2)
|
4
|
+
[![Dependency Status](https://gemnasium.com/dockyard/easy_auth-oauth2.png?travis)](https://gemnasium.com/dockyard/easy_auth-oauth2)
|
5
|
+
[![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/dockyard/easy_auth-oauth2)
|
6
|
+
|
7
|
+
Dead simple drop in authentication for Rails
|
8
|
+
|
9
|
+
## Installation ##
|
10
|
+
|
11
|
+
In your Gemfile add the following:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'easy_auth-oauth2'
|
15
|
+
```
|
16
|
+
|
17
|
+
After running Bundler you'll need to install the migrations
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
rake easy_auth-oauth2:install:migrations
|
21
|
+
```
|
22
|
+
|
23
|
+
Then run your migrations.
|
24
|
+
|
25
|
+
## Authors ##
|
26
|
+
|
27
|
+
[Brian Cardarella](http://twitter.com/bcardarella)
|
28
|
+
|
29
|
+
## Versioning ##
|
30
|
+
|
31
|
+
This gem follows [Semantic Versioning](http://semver.org)
|
32
|
+
|
33
|
+
## Want to help? ##
|
34
|
+
|
35
|
+
Stable branches are created based upon each minor version. Please make
|
36
|
+
pull requests to specific branches rather than master.
|
37
|
+
|
38
|
+
Please make sure you include tests!
|
39
|
+
|
40
|
+
Unles Rails drops support for Ruby 1.8.7 we will continue to use the
|
41
|
+
hash-rocket syntax. Please respect this.
|
42
|
+
|
43
|
+
Don't use tabs to indent, two spaces are the standard.
|
44
|
+
|
45
|
+
## Legal ##
|
46
|
+
|
47
|
+
[DockYard](http://dockyard.com), LLC © 2012
|
48
|
+
|
49
|
+
[@dockyard](http://twitter.com/dockyard)
|
50
|
+
|
51
|
+
[Licensed under the MIT license](http://www.opensource.org/licenses/mit-license.php)
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'easy_auth/oauth2'
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'oauth2'
|
2
|
+
|
3
|
+
module EasyAuth::Models::Identities::Oauth2::Base
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
extend ClassMethods
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def authenticate(controller)
|
12
|
+
callback_url = controller.oauth2_callback_url(:provider => provider)
|
13
|
+
code = controller.params[:code]
|
14
|
+
token = client.auth_code.get_token(code, token_options(callback_url))
|
15
|
+
user_info = get_user_info(token)
|
16
|
+
identity = self.find_or_initialize_by_username user_info['id'].to_s
|
17
|
+
identity.token = token.token
|
18
|
+
account = controller.current_account
|
19
|
+
|
20
|
+
if identity.new_record?
|
21
|
+
account = EasyAuth.account_model.create(EasyAuth.account_model.identity_username_attribute => identity.username) if account.nil?
|
22
|
+
identity.account = account
|
23
|
+
end
|
24
|
+
|
25
|
+
identity.save!
|
26
|
+
identity
|
27
|
+
end
|
28
|
+
|
29
|
+
def new_session(controller)
|
30
|
+
controller.redirect_to authenticate_url(controller.oauth2_callback_url(:provider => provider))
|
31
|
+
end
|
32
|
+
|
33
|
+
def get_access_token(identity)
|
34
|
+
::OAuth2::AccessToken.new client, identity.token
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def token_options(callback_url)
|
40
|
+
{ :redirect_uri => callback_url }
|
41
|
+
end
|
42
|
+
|
43
|
+
def get_user_info(token)
|
44
|
+
ActiveSupport::JSON.decode(token.get(user_info_url).body)
|
45
|
+
end
|
46
|
+
|
47
|
+
def provider
|
48
|
+
raise NotImplementedError
|
49
|
+
end
|
50
|
+
|
51
|
+
def client
|
52
|
+
@client ||= ::OAuth2::Client.new(client_id, secret, :site => site_url, :authorize_url => authorize_url, :token_url => token_url)
|
53
|
+
end
|
54
|
+
|
55
|
+
def authenticate_url(callback_url)
|
56
|
+
client.auth_code.authorize_url(:redirect_uri => callback_url, :scope => scope)
|
57
|
+
end
|
58
|
+
|
59
|
+
def user_info_url
|
60
|
+
raise NotImplementedError
|
61
|
+
end
|
62
|
+
|
63
|
+
def authorize_url
|
64
|
+
raise NotImplementedError
|
65
|
+
end
|
66
|
+
|
67
|
+
def token_url
|
68
|
+
raise NotImplementedError
|
69
|
+
end
|
70
|
+
|
71
|
+
def site_url
|
72
|
+
raise NotImplementedError
|
73
|
+
end
|
74
|
+
|
75
|
+
def scope
|
76
|
+
settings.scope
|
77
|
+
end
|
78
|
+
|
79
|
+
def client_id
|
80
|
+
settings.client_id
|
81
|
+
end
|
82
|
+
|
83
|
+
def secret
|
84
|
+
settings.secret
|
85
|
+
end
|
86
|
+
|
87
|
+
def settings
|
88
|
+
EasyAuth.oauth2[provider]
|
89
|
+
end
|
90
|
+
|
91
|
+
def provider
|
92
|
+
self.to_s.split('::').last.underscore.to_sym
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def get_access_token
|
97
|
+
self.class.get_access_token self
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'easy_auth'
|
2
|
+
require 'easy_auth/oauth2/engine'
|
3
|
+
require 'easy_auth/oauth2/version'
|
4
|
+
|
5
|
+
module EasyAuth
|
6
|
+
|
7
|
+
module Oauth2
|
8
|
+
extend ActiveSupport::Autoload
|
9
|
+
autoload :Controllers
|
10
|
+
autoload :Models
|
11
|
+
autoload :Routes
|
12
|
+
end
|
13
|
+
|
14
|
+
module Models
|
15
|
+
module Account
|
16
|
+
include EasyAuth::Oauth2::Models::Account
|
17
|
+
end
|
18
|
+
|
19
|
+
module Identities
|
20
|
+
autoload :Oauth2
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module Controllers::Sessions
|
25
|
+
include EasyAuth::Oauth2::Controllers::Sessions
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.oauth2_identity_model(params)
|
29
|
+
method_name = "oauth2_#{params[:provider]}_identity_model"
|
30
|
+
camelcased_provider_name = params[:provider].to_s.camelcase
|
31
|
+
if respond_to?(method_name)
|
32
|
+
send(method_name, params)
|
33
|
+
elsif eval("defined?(Identities::Oauth2::#{camelcased_provider_name})")
|
34
|
+
eval("Identities::Oauth2::#{camelcased_provider_name}")
|
35
|
+
else
|
36
|
+
camelcased_provider_name.constantize
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class << self
|
41
|
+
attr_accessor :oauth2
|
42
|
+
end
|
43
|
+
|
44
|
+
self.oauth2 = {}
|
45
|
+
|
46
|
+
def self.oauth2_client(provider, client_id, secret, scope = '')
|
47
|
+
oauth2[provider] = OpenStruct.new :client_id => client_id, :secret => secret, :scope => scope || ''
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
ActionDispatch::Routing::Mapper.send(:include, EasyAuth::Oauth2::Routes)
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module EasyAuth::Oauth2::Controllers::Sessions
|
2
|
+
|
3
|
+
private
|
4
|
+
|
5
|
+
def after_successful_sign_in_with_oauth2(identity)
|
6
|
+
send("after_successful_sign_in_with_oauth2_for_#{params[:provider]}", identity)
|
7
|
+
end
|
8
|
+
|
9
|
+
def after_successful_sign_in_url_with_oauth2(identity)
|
10
|
+
send("after_successful_sign_in_url_with_oauth2_for_#{params[:provider]}", identity)
|
11
|
+
end
|
12
|
+
|
13
|
+
def after_failed_sign_in_with_oauth2(identity)
|
14
|
+
send("after_failed_sign_in_with_oauth2_for_#{params[:provider]}", identity)
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,6 @@
|
|
1
|
+
module EasyAuth::Oauth2::Routes
|
2
|
+
def easy_auth_oauth2_routes
|
3
|
+
get '/sign_in/oauth2/:provider' => 'sessions#new', :as => :oauth2_sign_in, :defaults => { :identity => :oauth2 }
|
4
|
+
get '/sign_in/oauth2/:provider/callback' => 'sessions#create', :as => :oauth2_callback, :defaults => { :identity => :oauth2 }
|
5
|
+
end
|
6
|
+
end
|
metadata
ADDED
@@ -0,0 +1,196 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy_auth-oauth2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Brian Cardarella
|
9
|
+
- Dan McClain
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-11-05 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: easy_auth
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.2.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ~>
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: 0.2.0
|
31
|
+
- !ruby/object:Gem::Dependency
|
32
|
+
name: oauth2
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ~>
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: 0.8.0
|
39
|
+
type: :runtime
|
40
|
+
prerelease: false
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 0.8.0
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: sqlite3
|
65
|
+
requirement: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
type: :development
|
72
|
+
prerelease: false
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
- !ruby/object:Gem::Dependency
|
80
|
+
name: database_cleaner
|
81
|
+
requirement: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
type: :development
|
88
|
+
prerelease: false
|
89
|
+
version_requirements: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
- !ruby/object:Gem::Dependency
|
96
|
+
name: valid_attribute
|
97
|
+
requirement: !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ! '>='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
type: :development
|
104
|
+
prerelease: false
|
105
|
+
version_requirements: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: factory_girl
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ~>
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: 2.6.0
|
119
|
+
type: :development
|
120
|
+
prerelease: false
|
121
|
+
version_requirements: !ruby/object:Gem::Requirement
|
122
|
+
none: false
|
123
|
+
requirements:
|
124
|
+
- - ~>
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: 2.6.0
|
127
|
+
- !ruby/object:Gem::Dependency
|
128
|
+
name: bourne
|
129
|
+
requirement: !ruby/object:Gem::Requirement
|
130
|
+
none: false
|
131
|
+
requirements:
|
132
|
+
- - ! '>='
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
type: :development
|
136
|
+
prerelease: false
|
137
|
+
version_requirements: !ruby/object:Gem::Requirement
|
138
|
+
none: false
|
139
|
+
requirements:
|
140
|
+
- - ! '>='
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '0'
|
143
|
+
description: EasyAuth-Oauth2
|
144
|
+
email:
|
145
|
+
- brian@dockyard.com
|
146
|
+
- bcardarella@gmail.com
|
147
|
+
- rubygems@danmcclain.net
|
148
|
+
executables: []
|
149
|
+
extensions: []
|
150
|
+
extra_rdoc_files: []
|
151
|
+
files:
|
152
|
+
- app/models/identities/oauth2/base.rb
|
153
|
+
- lib/easy_auth/models/identities/oauth2/base.rb
|
154
|
+
- lib/easy_auth/models/identities/oauth2.rb
|
155
|
+
- lib/easy_auth/oauth2/controllers/sessions.rb
|
156
|
+
- lib/easy_auth/oauth2/controllers.rb
|
157
|
+
- lib/easy_auth/oauth2/engine.rb
|
158
|
+
- lib/easy_auth/oauth2/models/account.rb
|
159
|
+
- lib/easy_auth/oauth2/models.rb
|
160
|
+
- lib/easy_auth/oauth2/routes.rb
|
161
|
+
- lib/easy_auth/oauth2/version.rb
|
162
|
+
- lib/easy_auth/oauth2.rb
|
163
|
+
- lib/easy_auth-oauth2.rb
|
164
|
+
- Rakefile
|
165
|
+
- README.md
|
166
|
+
homepage: https://github.com/dockyard/easy_auth-oauth2
|
167
|
+
licenses: []
|
168
|
+
post_install_message:
|
169
|
+
rdoc_options: []
|
170
|
+
require_paths:
|
171
|
+
- lib
|
172
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
173
|
+
none: false
|
174
|
+
requirements:
|
175
|
+
- - ! '>='
|
176
|
+
- !ruby/object:Gem::Version
|
177
|
+
version: '0'
|
178
|
+
segments:
|
179
|
+
- 0
|
180
|
+
hash: -4384553740171571441
|
181
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
182
|
+
none: false
|
183
|
+
requirements:
|
184
|
+
- - ! '>='
|
185
|
+
- !ruby/object:Gem::Version
|
186
|
+
version: '0'
|
187
|
+
segments:
|
188
|
+
- 0
|
189
|
+
hash: -4384553740171571441
|
190
|
+
requirements: []
|
191
|
+
rubyforge_project:
|
192
|
+
rubygems_version: 1.8.23
|
193
|
+
signing_key:
|
194
|
+
specification_version: 3
|
195
|
+
summary: EasyAuth-Oauth2
|
196
|
+
test_files: []
|