rack-oauth2 0.3.1 → 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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.4.0
@@ -0,0 +1,11 @@
1
+ module Rack
2
+ module OAuth2
3
+ class Client
4
+ class Grant
5
+ class AuthorizationCode < Grant
6
+ attr_required :code, :redirect_uri
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ module Rack
2
+ module OAuth2
3
+ class Client
4
+ class Grant
5
+ class ClientCredentials < Grant
6
+ end
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ module Rack
2
+ module OAuth2
3
+ class Client
4
+ class Grant
5
+ class Password < Grant
6
+ attr_required :username, :password
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,28 @@
1
+ module Rack
2
+ module OAuth2
3
+ class Client
4
+ class Grant
5
+ include AttrRequired, AttrOptional
6
+
7
+ def initialize(attributes = {})
8
+ required_attributes.each do |key|
9
+ self.send "#{key}=", attributes[key]
10
+ end
11
+ attr_missing!
12
+ end
13
+
14
+ def to_hash
15
+ required_attributes.inject({
16
+ :grant_type => self.class.name.split('::').last.underscore.to_sym
17
+ }) do |hash, key|
18
+ hash.merge! key => self.send(key)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ require 'rack/oauth2/client/grant/authorization_code'
27
+ require 'rack/oauth2/client/grant/password'
28
+ require 'rack/oauth2/client/grant/client_credentials'
@@ -0,0 +1,85 @@
1
+ module Rack
2
+ module OAuth2
3
+ class Client
4
+ include AttrRequired, AttrOptional
5
+ attr_required :identifier
6
+ attr_optional :secret, :redirect_uri, :scheme, :host, :response_type, :authorize_endpoint, :token_endpoint
7
+
8
+ class Exception < StandardError
9
+ attr_accessor :status, :response
10
+ def initialize(status, response)
11
+ @status = status
12
+ @response = response
13
+ super response[:error_description]
14
+ end
15
+ end
16
+
17
+ def initialize(attributes = {})
18
+ (required_attributes + optional_attributes).each do |key|
19
+ self.send "#{key}=", attributes[key]
20
+ end
21
+ @grant = Grant::ClientCredentials.new
22
+ @authorize_endpoint ||= '/oauth2/authorize'
23
+ @token_endpoint ||= '/oauth2/token'
24
+ attr_missing!
25
+ end
26
+
27
+ def authorize_url(response_type = :code, params = {})
28
+ absolute_url_for authorize_endpoint, params.merge(
29
+ :client_id => self.identifier,
30
+ :redirect_uri => self.redirect_uri,
31
+ :response_type => response_type
32
+ )
33
+ end
34
+
35
+ def authorization_code=(code)
36
+ @grant = Grant::AuthorizationCode.new(
37
+ :code => code,
38
+ :redirect_uri => self.redirect_uri
39
+ )
40
+ end
41
+
42
+ def resource_owner_credentials=(username, password)
43
+ @grant = Grant::ResourceOwnerCredentials.new(
44
+ :username => username,
45
+ :password => password
46
+ )
47
+ end
48
+
49
+ def access_token!
50
+ params = @grant.to_hash
51
+ params.merge!(
52
+ :client_id => self.identifier,
53
+ :client_secret => self.secret
54
+ )
55
+ handle_response do
56
+ RestClient.post absolute_url_for(token_endpoint), Util.compact_hash(params)
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ def absolute_url_for(endpoint, params = {})
63
+ _endpoint_ = Util.parse_uri endpoint
64
+ _endpoint_.scheme ||= 'https'
65
+ _endpoint_.host ||= self.host
66
+ _endpoint_.query = Util.compact_hash(params).to_query
67
+ _endpoint_.to_s
68
+ end
69
+
70
+ def handle_response
71
+ response = yield
72
+ JSON.parse(response.body).with_indifferent_access
73
+ rescue RestClient::Exception => e
74
+ error = if e.http_body
75
+ JSON.parse(e.http_body).with_indifferent_access
76
+ else
77
+ {}
78
+ end
79
+ raise Exception.new(e.http_code, error)
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ require 'rack/oauth2/client/grant'
@@ -1,4 +1,3 @@
1
- require 'rack/oauth2/server/util'
2
1
  require 'rack/oauth2/server/abstract'
3
2
  require 'rack/oauth2/server/authorize'
4
3
  require 'rack/oauth2/server/token'
@@ -0,0 +1,47 @@
1
+ module Rack
2
+ module OAuth2
3
+ module Util
4
+ class << self
5
+ def compact_hash(hash)
6
+ hash.reject do |key, value|
7
+ value.blank?
8
+ end
9
+ end
10
+
11
+ def parse_uri(uri)
12
+ case uri
13
+ when URI::Generic
14
+ uri
15
+ when String
16
+ URI.parse(uri)
17
+ else
18
+ raise "Invalid format of URI is given."
19
+ end
20
+ end
21
+
22
+ def redirect_uri(base_uri, location, params)
23
+ redirect_uri = parse_uri base_uri
24
+ case location
25
+ when :query
26
+ redirect_uri.query = [redirect_uri.query, Util.compact_hash(params).to_query].compact.join('&')
27
+ when :fragment
28
+ redirect_uri.fragment = Util.compact_hash(params).to_query
29
+ end
30
+ redirect_uri.to_s
31
+ end
32
+
33
+ def uri_match?(base, given)
34
+ base = parse_uri(base)
35
+ given = parse_uri(given)
36
+ base.path = '/' if base.path.blank?
37
+ given.path = '/' if given.path.blank?
38
+ [:scheme, :host, :port].all? do |key|
39
+ base.send(key) == given.send(key)
40
+ end && /^#{base.path}/ =~ given.path
41
+ rescue
42
+ false
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
data/lib/rack/oauth2.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  require 'rack'
2
2
  require 'json'
3
+ require 'restclient'
3
4
  require 'active_support/core_ext'
4
5
  require 'attr_required'
5
6
  require 'attr_optional'
6
- require 'rack/oauth2/server'
7
+ require 'rack/oauth2/util'
8
+ require 'rack/oauth2/server'
9
+ require 'rack/oauth2/client'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-oauth2
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 3
9
- - 1
10
- version: 0.3.1
8
+ - 4
9
+ - 0
10
+ version: 0.4.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - nov matake
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-07 00:00:00 +09:00
18
+ date: 2011-03-10 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -157,6 +157,11 @@ files:
157
157
  - Rakefile
158
158
  - VERSION
159
159
  - lib/rack/oauth2.rb
160
+ - lib/rack/oauth2/client.rb
161
+ - lib/rack/oauth2/client/grant.rb
162
+ - lib/rack/oauth2/client/grant/authorization_code.rb
163
+ - lib/rack/oauth2/client/grant/client_credentials.rb
164
+ - lib/rack/oauth2/client/grant/password.rb
160
165
  - lib/rack/oauth2/server.rb
161
166
  - lib/rack/oauth2/server/abstract.rb
162
167
  - lib/rack/oauth2/server/abstract/error.rb
@@ -175,7 +180,7 @@ files:
175
180
  - lib/rack/oauth2/server/token/error.rb
176
181
  - lib/rack/oauth2/server/token/password.rb
177
182
  - lib/rack/oauth2/server/token/refresh_token.rb
178
- - lib/rack/oauth2/server/util.rb
183
+ - lib/rack/oauth2/util.rb
179
184
  - rack-oauth2.gemspec
180
185
  - spec/rack/oauth2/server/abstract/error_spec.rb
181
186
  - spec/rack/oauth2/server/authorize/code_spec.rb
@@ -1,49 +0,0 @@
1
- module Rack
2
- module OAuth2
3
- module Server
4
- module Util
5
- class << self
6
- def compact_hash(hash)
7
- hash.reject do |key, value|
8
- value.blank?
9
- end
10
- end
11
-
12
- def parse_uri(uri)
13
- case uri
14
- when URI::Generic
15
- uri
16
- when String
17
- URI.parse(uri)
18
- else
19
- raise "Invalid format of URI is given."
20
- end
21
- end
22
-
23
- def redirect_uri(base_uri, location, params)
24
- redirect_uri = parse_uri base_uri
25
- case location
26
- when :query
27
- redirect_uri.query = [redirect_uri.query, Util.compact_hash(params).to_query].compact.join('&')
28
- when :fragment
29
- redirect_uri.fragment = Util.compact_hash(params).to_query
30
- end
31
- redirect_uri.to_s
32
- end
33
-
34
- def uri_match?(base, given)
35
- base = parse_uri(base)
36
- given = parse_uri(given)
37
- base.path = '/' if base.path.blank?
38
- given.path = '/' if given.path.blank?
39
- [:scheme, :host, :port].all? do |key|
40
- base.send(key) == given.send(key)
41
- end && /^#{base.path}/ =~ given.path
42
- rescue
43
- false
44
- end
45
- end
46
- end
47
- end
48
- end
49
- end