oauth2-server 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
File without changes
data/README.md ADDED
File without changes
@@ -0,0 +1,11 @@
1
+ require 'oauth2/core'
2
+
3
+ module OAuth2
4
+
5
+ module Server
6
+ autoload :Flows, 'oauth2/server/flows'
7
+ autoload :Rails, 'oauth2/server/rails'
8
+ autoload :Request, 'oauth2/server/request'
9
+ end
10
+
11
+ end
@@ -0,0 +1,13 @@
1
+ module OAuth2
2
+
3
+ module Server
4
+
5
+ module Flows
6
+
7
+ autoload :WebServer, 'oauth2/server/flows/web_server'
8
+
9
+ end
10
+
11
+ end
12
+
13
+ end
@@ -0,0 +1,57 @@
1
+ require 'oauth2/attributes'
2
+
3
+ module OAuth2
4
+
5
+ module Server
6
+
7
+ module Flows
8
+
9
+ class WebServer
10
+
11
+ include OAuth2::Attributes
12
+
13
+ def create_ticket(&block)
14
+ if block_given?
15
+ callbacks[:create_ticket] = block
16
+ else
17
+ block.call
18
+ end
19
+ end
20
+
21
+ attributes :create_ticket,
22
+ :verify_resource_owner,
23
+ :render_authorize_form
24
+
25
+ # This method will create a ticket for the client and try to identify
26
+ # and verify the resource owner.
27
+ # If those two steps succeed, it displays the authorization form.
28
+ def request_authorization
29
+ create_ticket
30
+ verify_resource_owner or return
31
+ render_authorize_form
32
+ end
33
+
34
+ attributes :create_verification_code,
35
+ :authorized?
36
+ :redirect_back
37
+
38
+ # If the user authorized the client, we create a verification code.
39
+ # Then we redirect back with the verification code or an error message.
40
+ def grant_authorization
41
+ create_verification_code if authorized?
42
+ redirect_back
43
+ end
44
+
45
+ # TODO
46
+ def access_token
47
+ # TODO: verify that type == "web_sever"
48
+ # TODO: verify that redirect_uri == ticket.redirect_uri
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+
57
+ end
@@ -0,0 +1,142 @@
1
+ require 'oauth2/attributes'
2
+ require 'hmac-sha2'
3
+ require 'active_support/base64'
4
+
5
+ module OAuth2
6
+
7
+ module Server
8
+
9
+ class Request
10
+
11
+ include OAuth2::Attributes
12
+
13
+ attributes :host, :realm, :algorithms, :method,
14
+ :request_header, :request_uri,
15
+ :host_with_port, :secret, :access_token,
16
+ :access_token_expired?
17
+
18
+ class << self
19
+
20
+ def validate(*args)
21
+ request = new(*args)
22
+ request.validate
23
+ request
24
+ end
25
+
26
+ end
27
+
28
+ def initialize(attributes = {}, &block)
29
+ self.attributes.merge!(attributes)
30
+ end
31
+
32
+ def errors
33
+ @errors ||= []
34
+ end
35
+
36
+ alias_method :original_request_header, :request_header
37
+
38
+ # Overwrite attribute reader #request_header
39
+ # See OAuth2::Server::Attributes
40
+ def request_header
41
+ value = original_request_header
42
+
43
+ if value.is_a?(String)
44
+ value = Headers::Authorization.parse(value)
45
+ end
46
+
47
+ value
48
+ end
49
+
50
+ def type
51
+ return :bearer if bearer?
52
+ return :cryptographic if cryptographic?
53
+
54
+ :unknown
55
+ end
56
+
57
+ def bearer?
58
+ if request_header.attributes.values.compact.size != 1
59
+ return false
60
+ end
61
+
62
+ not request_header.token.nil?
63
+ end
64
+
65
+ def cryptographic?
66
+ required_attributes = %w{token nonce timestamp algorithm signature}
67
+ required_attributes.map! do |attribute|
68
+ request_header.send(attribute.to_sym).nil?
69
+ end
70
+
71
+ required_attributes.uniq == [false]
72
+ end
73
+
74
+ # Calculates the header signature as described on:
75
+ # http://tools.ietf.org/html/draft-hammer-oauth2-00#section-5.3.1
76
+ def calculate_signature
77
+ normalized_string = [
78
+ request_header.timestamp,
79
+ request_header.nonce,
80
+ request_header.algorithm,
81
+ method.upcase,
82
+ host_with_port,
83
+ request_uri
84
+ ].join(',')
85
+
86
+ digest = HMAC::SHA256.digest(secret, normalized_string)
87
+
88
+ ActiveSupport::Base64.encode64s(digest)
89
+ end
90
+
91
+ def validate_signature
92
+ if calculate_signature != request_header.signature
93
+ errors << :signature_invalid
94
+ return false
95
+ end
96
+
97
+ true
98
+ end
99
+
100
+ def validate_access_token
101
+ unless access_token
102
+ errors << :access_token_invalid
103
+ return false
104
+ end
105
+
106
+ if access_token_expired?
107
+ errors << :access_token_expired
108
+ return false
109
+ end
110
+
111
+ true
112
+ end
113
+
114
+ def validate_request_header
115
+ unless request_header
116
+ errors << :missing_authorization_header
117
+ return false
118
+ end
119
+
120
+ true
121
+ end
122
+
123
+ def validate
124
+ errors.clear
125
+
126
+ if validate_request_header
127
+ validate_access_token
128
+ validate_signature if type == :cryptographic
129
+ end
130
+
131
+ @valid = errors.empty?
132
+ end
133
+
134
+ def valid?
135
+ @valid
136
+ end
137
+
138
+ end
139
+
140
+ end
141
+
142
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oauth2-server
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
10
+ platform: ruby
11
+ authors:
12
+ - Alexander Flatter
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-07 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: ruby-hmac
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 0
30
+ - 0
31
+ version: 0.0.0
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: rspec
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :development
45
+ version_requirements: *id002
46
+ description: ""
47
+ email:
48
+ - aflatter@farbenmeer.net
49
+ executables: []
50
+
51
+ extensions: []
52
+
53
+ extra_rdoc_files: []
54
+
55
+ files:
56
+ - lib/oauth2/server/flows/web_server.rb
57
+ - lib/oauth2/server/flows.rb
58
+ - lib/oauth2/server/request.rb
59
+ - lib/oauth2/server.rb
60
+ - LICENSE
61
+ - README.md
62
+ has_rdoc: true
63
+ homepage: http://github.com/aflatter/oauth2
64
+ licenses: []
65
+
66
+ post_install_message:
67
+ rdoc_options: []
68
+
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 1
84
+ - 3
85
+ - 6
86
+ version: 1.3.6
87
+ requirements: []
88
+
89
+ rubyforge_project:
90
+ rubygems_version: 1.3.6
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: ""
94
+ test_files: []
95
+