motionauth-oauth2 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,58 @@
1
+ module OAuth2
2
+ class Response
3
+ attr_accessor :error, :options
4
+
5
+ # Adds a new content type parser.
6
+ #
7
+ # @param [Symbol] key A descriptive symbol key such as :json or :query.
8
+ # @param [Array] One or more mime types to which this parser applies.
9
+ # @yield [String] A block returning parsed content.
10
+ def self.register_parser(key, mime_types, &block)
11
+ key = key.to_sym
12
+ PARSERS[key] = block
13
+ Array(mime_types).each do |mime_type|
14
+ CONTENT_TYPES[mime_type] = key
15
+ end
16
+ end
17
+
18
+ # Procs that, when called, will parse a response body according
19
+ # to the specified format.
20
+ PARSERS = {
21
+ json: ->(body) { Utils.parse_json(body) rescue body }, # rubocop:disable RescueModifier
22
+ query: ->(body) { Utils.params_from_query(body) },
23
+ text: ->(body) { body }
24
+ }
25
+
26
+ # Content type assignments for various potential HTTP content types.
27
+ CONTENT_TYPES = {
28
+ "application/json" => :json,
29
+ "text/javascript" => :json,
30
+ "application/x-www-form-urlencoded" => :query,
31
+ "text/plain" => :text
32
+ }
33
+
34
+ # The parsed response body.
35
+ # Will attempt to parse application/x-www-form-urlencoded and
36
+ # application/json Content-Type response bodies
37
+ def parsed
38
+ return nil unless PARSERS.key?(parser)
39
+ @parsed ||= PARSERS[parser].call(body)
40
+ end
41
+
42
+ # Determines the parser that will be used to supply the content of #parsed
43
+ def parser
44
+ return options[:parse].to_sym if PARSERS.key?(options[:parse])
45
+ CONTENT_TYPES[content_type]
46
+ end
47
+ end
48
+ end
49
+
50
+ OAuth2::Response.register_parser(:xml, ["text/xml", "application/rss+xml", "application/rdf+xml", "application/atom+xml"]) do |body|
51
+ begin
52
+ # TODO: PARSE XML
53
+ # MultiXml.parse(body)
54
+ body
55
+ rescue
56
+ body
57
+ end
58
+ end
@@ -0,0 +1,59 @@
1
+ module OAuth2
2
+ module Strategy
3
+ # The Client Assertion Strategy
4
+ #
5
+ # @see http://tools.ietf.org/html/draft-ietf-oauth-v2-10#section-4.1.3
6
+ #
7
+ # Sample usage:
8
+ # client = OAuth2::Client.new(client_id, client_secret,
9
+ # :site => 'http://localhost:8080')
10
+ #
11
+ # params = {:hmac_secret => "some secret",
12
+ # # or :private_key => "private key string",
13
+ # :iss => "http://localhost:3001",
14
+ # :prn => "me@here.com",
15
+ # :exp => Time.now.utc.to_i + 3600}
16
+ #
17
+ # access = client.assertion.get_token(params)
18
+ # access.token # actual access_token string
19
+ # access.get("/api/stuff") # making api calls with access token in header
20
+ #
21
+ class Assertion < Base
22
+ # Not used for this strategy
23
+ #
24
+ # @raise [NotImplementedError]
25
+ def authorize_url
26
+ fail(NotImplementedError, "The authorization endpoint is not used in this strategy")
27
+ end
28
+
29
+ # Retrieve an access token given the specified client.
30
+ #
31
+ # @param [Hash] params assertion params
32
+ # pass either :hmac_secret or :private_key, but not both.
33
+ #
34
+ # params :hmac_secret, secret string.
35
+ # params :private_key, private key string.
36
+ #
37
+ # params :iss, issuer
38
+ # params :aud, audience, optional
39
+ # params :prn, principal, current user
40
+ # params :exp, expired at, in seconds, like Time.now.utc.to_i + 3600
41
+ #
42
+ # @param [Hash] opts options
43
+ def get_token(params = {}, opts = {})
44
+ hash = build_request(params)
45
+ @client.get_token(hash, opts.merge("refresh_token" => nil))
46
+ end
47
+
48
+ def build_request(params)
49
+ assertion = build_assertion(params)
50
+ {
51
+ grant_type: "assertion",
52
+ assertion_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
53
+ assertion: assertion,
54
+ scope: params[:scope]
55
+ }.merge(client_params)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,33 @@
1
+ module OAuth2
2
+ module Strategy
3
+ # The Authorization Code Strategy
4
+ #
5
+ # @see http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-4.1
6
+ class AuthCode < Base
7
+ # The required query parameters for the authorize URL
8
+ #
9
+ # @param [Hash] params additional query parameters
10
+ def authorize_params(params = {})
11
+ params.merge("response_type" => "code", "client_id" => @client.id)
12
+ end
13
+
14
+ # The authorization URL endpoint of the provider
15
+ #
16
+ # @param [Hash] params additional query parameters for the URL
17
+ def authorize_url(params = {})
18
+ @client.authorize_url(authorize_params.merge(params))
19
+ end
20
+
21
+ # Retrieve an access token given the specified validation code.
22
+ #
23
+ # @param [String] code The Authorization Code value
24
+ # @param [Hash] params additional params
25
+ # @param [Hash] opts options
26
+ # @note that you must also provide a :redirect_uri with most OAuth 2.0 providers
27
+ def get_token(code, params = {}, opts = {})
28
+ params = { "grant_type" => "authorization_code", "code" => code }.merge(client_params).merge(params)
29
+ @client.get_token(params, opts)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,16 @@
1
+ module OAuth2
2
+ module Strategy
3
+ class Base
4
+ def initialize(client)
5
+ @client = client
6
+ end
7
+
8
+ # The OAuth client_id and client_secret
9
+ #
10
+ # @return [Hash]
11
+ def client_params
12
+ { "client_id" => @client.id, "client_secret" => @client.secret }
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,30 @@
1
+ module OAuth2
2
+ module Strategy
3
+ # The Client Credentials Strategy
4
+ #
5
+ # @see http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-4.4
6
+ class ClientCredentials < Base
7
+ # Not used for this strategy
8
+ #
9
+ # @raise [NotImplementedError]
10
+ def authorize_url
11
+ fail(NotImplementedError, "The authorization endpoint is not used in this strategy")
12
+ end
13
+
14
+ # Retrieve an access token given the specified client.
15
+ #
16
+ # @param [Hash] params additional params
17
+ # @param [Hash] opts options
18
+ def get_token(params = {}, opts = {})
19
+ request_body = opts.delete("auth_scheme") == "request_body"
20
+ params.merge!("grant_type" => "client_credentials")
21
+ params.merge!(request_body ? client_params : {
22
+ headers: {
23
+ "Authorization" => authorization(client_params["client_id"], client_params["client_secret"])
24
+ }
25
+ })
26
+ @client.get_token(params, opts.merge("refresh_token" => nil))
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,29 @@
1
+ module OAuth2
2
+ module Strategy
3
+ # The Implicit Strategy
4
+ #
5
+ # @see http://tools.ietf.org/html/draft-ietf-oauth-v2-26#section-4.2
6
+ class Implicit < Base
7
+ # The required query parameters for the authorize URL
8
+ #
9
+ # @param [Hash] params additional query parameters
10
+ def authorize_params(params = {})
11
+ params.merge("response_type" => "token", "client_id" => @client.id)
12
+ end
13
+
14
+ # The authorization URL endpoint of the provider
15
+ #
16
+ # @param [Hash] params additional query parameters for the URL
17
+ def authorize_url(params = {})
18
+ @client.authorize_url(authorize_params.merge(params))
19
+ end
20
+
21
+ # Not used for this strategy
22
+ #
23
+ # @raise [NotImplementedError]
24
+ def get_token(*)
25
+ fail(NotImplementedError, "The token is accessed differently in this strategy")
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ module OAuth2
2
+ module Strategy
3
+ # The Resource Owner Password Credentials Authorization Strategy
4
+ #
5
+ # @see http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-4.3
6
+ class Password < Base
7
+ # Not used for this strategy
8
+ #
9
+ # @raise [NotImplementedError]
10
+ def authorize_url
11
+ fail(NotImplementedError, "The authorization endpoint is not used in this strategy")
12
+ end
13
+
14
+ # Retrieve an access token given the specified End User username and password.
15
+ #
16
+ # @param [String] username the End User username
17
+ # @param [String] password the End User password
18
+ # @param [Hash] params additional params
19
+ def get_token(username, password, params = {}, opts = {})
20
+ params = {
21
+ "grant_type" => "password",
22
+ "username" => username,
23
+ "password" => password
24
+ }.merge(client_params).merge(params)
25
+ @client.get_token(params, opts)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,15 @@
1
+ module OAuth2
2
+ class Version
3
+ MAJOR = 1
4
+ MINOR = 0
5
+ PATCH = 0
6
+ PRE = nil
7
+
8
+ class << self
9
+ # @return [String]
10
+ def to_s
11
+ [MAJOR, MINOR, PATCH, PRE].compact.join(".")
12
+ end
13
+ end
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,197 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: motionauth-oauth2
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Brian Pattison
8
+ - Michael Bleigh
9
+ - Erik Michaels-Ober
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2015-03-17 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: motion-cocoapods
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '1.7'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '1.7'
29
+ - !ruby/object:Gem::Dependency
30
+ name: motion-support
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - "~>"
34
+ - !ruby/object:Gem::Version
35
+ version: '0.2'
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '0.2'
43
+ - !ruby/object:Gem::Dependency
44
+ name: guard
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: 2.6.1
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: 2.6.1
57
+ - !ruby/object:Gem::Dependency
58
+ name: guard-motion
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '0.1'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - "~>"
69
+ - !ruby/object:Gem::Version
70
+ version: '0.1'
71
+ - !ruby/object:Gem::Dependency
72
+ name: motion_print
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '0.0'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - "~>"
83
+ - !ruby/object:Gem::Version
84
+ version: '0.0'
85
+ - !ruby/object:Gem::Dependency
86
+ name: motion-redgreen
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: '1.0'
92
+ type: :development
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - "~>"
97
+ - !ruby/object:Gem::Version
98
+ version: '1.0'
99
+ - !ruby/object:Gem::Dependency
100
+ name: RackMotion
101
+ requirement: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - "~>"
104
+ - !ruby/object:Gem::Version
105
+ version: '0.3'
106
+ type: :development
107
+ prerelease: false
108
+ version_requirements: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - "~>"
111
+ - !ruby/object:Gem::Version
112
+ version: '0.3'
113
+ - !ruby/object:Gem::Dependency
114
+ name: rake
115
+ requirement: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ type: :development
121
+ prerelease: false
122
+ version_requirements: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ - !ruby/object:Gem::Dependency
128
+ name: terminal-notifier-guard
129
+ requirement: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - "~>"
132
+ - !ruby/object:Gem::Version
133
+ version: '1.6'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - "~>"
139
+ - !ruby/object:Gem::Version
140
+ version: '1.6'
141
+ description: A RubyMotion wrapper for the OAuth 2.0 protocol built with a similar
142
+ style to the original OAuth spec.
143
+ email:
144
+ - brian@brianpattison.com
145
+ - michael@intridea.com
146
+ - sferik@gmail.com
147
+ executables: []
148
+ extensions: []
149
+ extra_rdoc_files: []
150
+ files:
151
+ - README.md
152
+ - lib/oauth2-cocoa/connection.rb
153
+ - lib/oauth2-cocoa/mac_token.rb
154
+ - lib/oauth2-cocoa/response.rb
155
+ - lib/oauth2-cocoa/strategy/assertion.rb
156
+ - lib/oauth2-cocoa/strategy/client_credentials.rb
157
+ - lib/oauth2-cocoa/utils.rb
158
+ - lib/oauth2.rb
159
+ - lib/oauth2/access_token.rb
160
+ - lib/oauth2/client.rb
161
+ - lib/oauth2/connection.rb
162
+ - lib/oauth2/error.rb
163
+ - lib/oauth2/mac_token.rb
164
+ - lib/oauth2/response.rb
165
+ - lib/oauth2/strategy/assertion.rb
166
+ - lib/oauth2/strategy/auth_code.rb
167
+ - lib/oauth2/strategy/base.rb
168
+ - lib/oauth2/strategy/client_credentials.rb
169
+ - lib/oauth2/strategy/implicit.rb
170
+ - lib/oauth2/strategy/password.rb
171
+ - lib/oauth2/version.rb
172
+ homepage: https://github.com/motionauth/oauth2
173
+ licenses:
174
+ - MIT
175
+ metadata: {}
176
+ post_install_message:
177
+ rdoc_options: []
178
+ require_paths:
179
+ - lib
180
+ required_ruby_version: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ version: '0'
185
+ required_rubygems_version: !ruby/object:Gem::Requirement
186
+ requirements:
187
+ - - ">="
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ requirements: []
191
+ rubyforge_project:
192
+ rubygems_version: 2.4.5
193
+ signing_key:
194
+ specification_version: 4
195
+ summary: A RubyMotion wrapper for the OAuth 2.0 protocol.
196
+ test_files: []
197
+ has_rdoc: