oauth2 1.4.7 → 2.0.2
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +114 -22
- data/CONTRIBUTING.md +18 -0
- data/LICENSE +1 -1
- data/README.md +383 -138
- data/SECURITY.md +20 -0
- data/lib/oauth2/access_token.rb +32 -21
- data/lib/oauth2/authenticator.rb +12 -5
- data/lib/oauth2/client.rb +142 -81
- data/lib/oauth2/error.rb +29 -18
- data/lib/oauth2/response.rb +63 -19
- data/lib/oauth2/snaky_hash.rb +8 -0
- data/lib/oauth2/strategy/assertion.rb +66 -39
- data/lib/oauth2/strategy/auth_code.rb +15 -2
- data/lib/oauth2/strategy/base.rb +2 -0
- data/lib/oauth2/strategy/client_credentials.rb +3 -1
- data/lib/oauth2/strategy/implicit.rb +10 -1
- data/lib/oauth2/strategy/password.rb +3 -1
- data/lib/oauth2/version.rb +1 -59
- data/lib/oauth2.rb +21 -1
- metadata +75 -97
- data/lib/oauth2/mac_token.rb +0 -130
- data/spec/helper.rb +0 -37
- data/spec/oauth2/access_token_spec.rb +0 -216
- data/spec/oauth2/authenticator_spec.rb +0 -84
- data/spec/oauth2/client_spec.rb +0 -506
- data/spec/oauth2/mac_token_spec.rb +0 -117
- data/spec/oauth2/response_spec.rb +0 -90
- data/spec/oauth2/strategy/assertion_spec.rb +0 -58
- data/spec/oauth2/strategy/auth_code_spec.rb +0 -107
- data/spec/oauth2/strategy/base_spec.rb +0 -5
- data/spec/oauth2/strategy/client_credentials_spec.rb +0 -69
- data/spec/oauth2/strategy/implicit_spec.rb +0 -26
- data/spec/oauth2/strategy/password_spec.rb +0 -55
- data/spec/oauth2/version_spec.rb +0 -23
data/lib/oauth2/response.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
2
4
|
require 'multi_xml'
|
3
5
|
require 'rack'
|
4
6
|
|
@@ -6,20 +8,17 @@ module OAuth2
|
|
6
8
|
# OAuth2::Response class
|
7
9
|
class Response
|
8
10
|
attr_reader :response
|
9
|
-
attr_accessor :
|
11
|
+
attr_accessor :options
|
10
12
|
|
11
13
|
# Procs that, when called, will parse a response body according
|
12
14
|
# to the specified format.
|
13
15
|
@@parsers = {
|
14
|
-
:
|
15
|
-
:
|
16
|
-
:text => lambda { |body| body },
|
16
|
+
query: ->(body) { Rack::Utils.parse_query(body) },
|
17
|
+
text: ->(body) { body },
|
17
18
|
}
|
18
19
|
|
19
20
|
# Content type assignments for various potential HTTP content types.
|
20
21
|
@@content_types = {
|
21
|
-
'application/json' => :json,
|
22
|
-
'text/javascript' => :json,
|
23
22
|
'application/x-www-form-urlencoded' => :query,
|
24
23
|
'text/plain' => :text,
|
25
24
|
}
|
@@ -45,7 +44,7 @@ module OAuth2
|
|
45
44
|
# :json, or :automatic (determined by Content-Type response header)
|
46
45
|
def initialize(response, opts = {})
|
47
46
|
@response = response
|
48
|
-
@options = {:
|
47
|
+
@options = {parse: :automatic}.merge(opts)
|
49
48
|
end
|
50
49
|
|
51
50
|
# The HTTP response headers
|
@@ -63,29 +62,74 @@ module OAuth2
|
|
63
62
|
response.body || ''
|
64
63
|
end
|
65
64
|
|
66
|
-
# The parsed
|
67
|
-
#
|
68
|
-
#
|
65
|
+
# The {#response} {#body} as parsed by {#parser}.
|
66
|
+
#
|
67
|
+
# @return [Object] As returned by {#parser} if it is #call-able.
|
68
|
+
# @return [nil] If the {#parser} is not #call-able.
|
69
69
|
def parsed
|
70
|
-
return
|
70
|
+
return @parsed if defined?(@parsed)
|
71
|
+
|
72
|
+
@parsed =
|
73
|
+
if parser.respond_to?(:call)
|
74
|
+
case parser.arity
|
75
|
+
when 0
|
76
|
+
parser.call
|
77
|
+
when 1
|
78
|
+
parser.call(body)
|
79
|
+
else
|
80
|
+
parser.call(body, response)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
@parsed = OAuth2::SnakyHash.new(@parsed) if @parsed.is_a?(Hash)
|
71
85
|
|
72
|
-
@parsed
|
86
|
+
@parsed
|
73
87
|
end
|
74
88
|
|
75
89
|
# Attempts to determine the content type of the response.
|
76
90
|
def content_type
|
77
|
-
|
91
|
+
return nil unless response.headers
|
92
|
+
|
93
|
+
((response.headers.values_at('content-type', 'Content-Type').compact.first || '').split(';').first || '').strip.downcase
|
78
94
|
end
|
79
95
|
|
80
|
-
# Determines the parser
|
96
|
+
# Determines the parser (a Proc or other Object which responds to #call)
|
97
|
+
# that will be passed the {#body} (and optional {#response}) to supply
|
98
|
+
# {#parsed}.
|
99
|
+
#
|
100
|
+
# The parser can be supplied as the +:parse+ option in the form of a Proc
|
101
|
+
# (or other Object responding to #call) or a Symbol. In the latter case,
|
102
|
+
# the actual parser will be looked up in {@@parsers} by the supplied Symbol.
|
103
|
+
#
|
104
|
+
# If no +:parse+ option is supplied, the lookup Symbol will be determined
|
105
|
+
# by looking up {#content_type} in {@@content_types}.
|
106
|
+
#
|
107
|
+
# If {#parser} is a Proc, it will be called with no arguments, just
|
108
|
+
# {#body}, or {#body} and {#response}, depending on the Proc's arity.
|
109
|
+
#
|
110
|
+
# @return [Proc, #call] If a parser was found.
|
111
|
+
# @return [nil] If no parser was found.
|
81
112
|
def parser
|
82
|
-
return
|
113
|
+
return @parser if defined?(@parser)
|
114
|
+
|
115
|
+
@parser =
|
116
|
+
if options[:parse].respond_to?(:call)
|
117
|
+
options[:parse]
|
118
|
+
elsif options[:parse]
|
119
|
+
@@parsers[options[:parse].to_sym]
|
120
|
+
end
|
83
121
|
|
84
|
-
@@content_types[content_type]
|
122
|
+
@parser ||= @@parsers[@@content_types[content_type]]
|
85
123
|
end
|
86
124
|
end
|
87
125
|
end
|
88
126
|
|
89
|
-
OAuth2::Response.register_parser(:xml, ['text/xml', 'application/rss+xml', 'application/rdf+xml', 'application/atom+xml']) do |body|
|
90
|
-
MultiXml.parse(body)
|
127
|
+
OAuth2::Response.register_parser(:xml, ['text/xml', 'application/rss+xml', 'application/rdf+xml', 'application/atom+xml', 'application/xml']) do |body|
|
128
|
+
MultiXml.parse(body)
|
129
|
+
end
|
130
|
+
|
131
|
+
OAuth2::Response.register_parser(:json, ['application/json', 'text/javascript', 'application/hal+json', 'application/vnd.collection+json', 'application/vnd.api+json', 'application/problem+json']) do |body|
|
132
|
+
body = body.dup.force_encoding(::Encoding::ASCII_8BIT) if body.respond_to?(:force_encoding)
|
133
|
+
|
134
|
+
::JSON.parse(body)
|
91
135
|
end
|
@@ -1,22 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'jwt'
|
2
4
|
|
3
5
|
module OAuth2
|
4
6
|
module Strategy
|
5
7
|
# The Client Assertion Strategy
|
6
8
|
#
|
7
|
-
# @see
|
9
|
+
# @see https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-10#section-4.1.3
|
8
10
|
#
|
9
11
|
# Sample usage:
|
10
12
|
# client = OAuth2::Client.new(client_id, client_secret,
|
11
|
-
# :site => 'http://localhost:8080'
|
13
|
+
# :site => 'http://localhost:8080',
|
14
|
+
# :auth_scheme => :request_body)
|
15
|
+
#
|
16
|
+
# claim_set = {
|
17
|
+
# :iss => "http://localhost:3001",
|
18
|
+
# :aud => "http://localhost:8080/oauth2/token",
|
19
|
+
# :sub => "me@example.com",
|
20
|
+
# :exp => Time.now.utc.to_i + 3600,
|
21
|
+
# }
|
12
22
|
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
# :exp => Time.now.utc.to_i + 3600}
|
23
|
+
# encoding = {
|
24
|
+
# :algorithm => 'HS256',
|
25
|
+
# :key => 'secret_key',
|
26
|
+
# }
|
18
27
|
#
|
19
|
-
# access = client.assertion.get_token(
|
28
|
+
# access = client.assertion.get_token(claim_set, encoding)
|
20
29
|
# access.token # actual access_token string
|
21
30
|
# access.get("/api/stuff") # making api calls with access token in header
|
22
31
|
#
|
@@ -30,45 +39,63 @@ module OAuth2
|
|
30
39
|
|
31
40
|
# Retrieve an access token given the specified client.
|
32
41
|
#
|
33
|
-
# @param [Hash]
|
34
|
-
#
|
42
|
+
# @param [Hash] claims the hash representation of the claims that should be encoded as a JWT (JSON Web Token)
|
43
|
+
#
|
44
|
+
# For reading on JWT and claim keys:
|
45
|
+
# @see https://github.com/jwt/ruby-jwt
|
46
|
+
# @see https://datatracker.ietf.org/doc/html/rfc7519#section-4.1
|
47
|
+
# @see https://datatracker.ietf.org/doc/html/rfc7523#section-3
|
48
|
+
# @see https://www.iana.org/assignments/jwt/jwt.xhtml
|
49
|
+
#
|
50
|
+
# There are many possible claim keys, and applications may ask for their own custom keys.
|
51
|
+
# Some typically required ones:
|
52
|
+
# :iss (issuer)
|
53
|
+
# :aud (audience)
|
54
|
+
# :sub (subject) -- formerly :prn https://datatracker.ietf.org/doc/html/draft-ietf-oauth-json-web-token-06#appendix-F
|
55
|
+
# :exp, (expiration time) -- in seconds, e.g. Time.now.utc.to_i + 3600
|
56
|
+
#
|
57
|
+
# Note that this method does *not* validate presence of those four claim keys indicated as required by RFC 7523.
|
58
|
+
# There are endpoints that may not conform with this RFC, and this gem should still work for those use cases.
|
59
|
+
#
|
60
|
+
# @param [Hash] encoding_opts a hash containing instructions on how the JWT should be encoded
|
61
|
+
# @option algorithm [String] the algorithm with which you would like the JWT to be encoded
|
62
|
+
# @option key [Object] the key with which you would like to encode the JWT
|
35
63
|
#
|
36
|
-
#
|
37
|
-
#
|
64
|
+
# These two options are passed directly to `JWT.encode`. For supported encoding arguments:
|
65
|
+
# @see https://github.com/jwt/ruby-jwt#algorithms-and-usage
|
66
|
+
# @see https://datatracker.ietf.org/doc/html/rfc7518#section-3.1
|
38
67
|
#
|
39
|
-
#
|
40
|
-
#
|
41
|
-
#
|
42
|
-
# params :exp, expired at, in seconds, like Time.now.utc.to_i + 3600
|
68
|
+
# The object type of `:key` may depend on the value of `:algorithm`. Sample arguments:
|
69
|
+
# get_token(claim_set, {:algorithm => 'HS256', :key => 'secret_key'})
|
70
|
+
# get_token(claim_set, {:algorithm => 'RS256', :key => OpenSSL::PKCS12.new(File.read('my_key.p12'), 'not_secret')})
|
43
71
|
#
|
44
|
-
# @param [Hash]
|
45
|
-
|
46
|
-
|
47
|
-
|
72
|
+
# @param [Hash] request_opts options that will be used to assemble the request
|
73
|
+
# @option request_opts [String] :scope the url parameter `scope` that may be required by some endpoints
|
74
|
+
# @see https://datatracker.ietf.org/doc/html/rfc7521#section-4.1
|
75
|
+
#
|
76
|
+
# @param [Hash] response_opts this will be merged with the token response to create the AccessToken object
|
77
|
+
# @see the access_token_opts argument to Client#get_token
|
78
|
+
|
79
|
+
def get_token(claims, encoding_opts, request_opts = {}, response_opts = {})
|
80
|
+
assertion = build_assertion(claims, encoding_opts)
|
81
|
+
params = build_request(assertion, request_opts)
|
82
|
+
|
83
|
+
@client.get_token(params, response_opts.merge('refresh_token' => nil))
|
48
84
|
end
|
49
85
|
|
50
|
-
|
51
|
-
|
86
|
+
private
|
87
|
+
|
88
|
+
def build_request(assertion, request_opts = {})
|
52
89
|
{
|
53
|
-
:
|
54
|
-
:
|
55
|
-
|
56
|
-
:scope => params[:scope],
|
57
|
-
}
|
90
|
+
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
|
91
|
+
assertion: assertion,
|
92
|
+
}.merge(request_opts)
|
58
93
|
end
|
59
94
|
|
60
|
-
def build_assertion(
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
:prn => params[:prn],
|
65
|
-
:exp => params[:exp],
|
66
|
-
}
|
67
|
-
if params[:hmac_secret]
|
68
|
-
JWT.encode(claims, params[:hmac_secret], 'HS256')
|
69
|
-
elsif params[:private_key]
|
70
|
-
JWT.encode(claims, params[:private_key], 'RS256')
|
71
|
-
end
|
95
|
+
def build_assertion(claims, encoding_opts)
|
96
|
+
raise ArgumentError.new(message: 'Please provide an encoding_opts hash with :algorithm and :key') if !encoding_opts.is_a?(Hash) || (%i[algorithm key] - encoding_opts.keys).any?
|
97
|
+
|
98
|
+
JWT.encode(claims, encoding_opts[:key], encoding_opts[:algorithm])
|
72
99
|
end
|
73
100
|
end
|
74
101
|
end
|
@@ -1,8 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module OAuth2
|
2
4
|
module Strategy
|
3
5
|
# The Authorization Code Strategy
|
4
6
|
#
|
5
|
-
# @see http://
|
7
|
+
# @see http://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-15#section-4.1
|
6
8
|
class AuthCode < Base
|
7
9
|
# The required query parameters for the authorize URL
|
8
10
|
#
|
@@ -15,6 +17,7 @@ module OAuth2
|
|
15
17
|
#
|
16
18
|
# @param [Hash] params additional query parameters for the URL
|
17
19
|
def authorize_url(params = {})
|
20
|
+
assert_valid_params(params)
|
18
21
|
@client.authorize_url(authorize_params.merge(params))
|
19
22
|
end
|
20
23
|
|
@@ -26,8 +29,18 @@ module OAuth2
|
|
26
29
|
# @note that you must also provide a :redirect_uri with most OAuth 2.0 providers
|
27
30
|
def get_token(code, params = {}, opts = {})
|
28
31
|
params = {'grant_type' => 'authorization_code', 'code' => code}.merge(@client.redirection_params).merge(params)
|
32
|
+
params_dup = params.dup
|
33
|
+
params.each_key do |key|
|
34
|
+
params_dup[key.to_s] = params_dup.delete(key) if key.is_a?(Symbol)
|
35
|
+
end
|
36
|
+
|
37
|
+
@client.get_token(params_dup, opts)
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
29
41
|
|
30
|
-
|
42
|
+
def assert_valid_params(params)
|
43
|
+
raise(ArgumentError, 'client_secret is not allowed in authorize URL query params') if params.key?(:client_secret) || params.key?('client_secret')
|
31
44
|
end
|
32
45
|
end
|
33
46
|
end
|
data/lib/oauth2/strategy/base.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module OAuth2
|
2
4
|
module Strategy
|
3
5
|
# The Client Credentials Strategy
|
4
6
|
#
|
5
|
-
# @see http://
|
7
|
+
# @see http://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-15#section-4.4
|
6
8
|
class ClientCredentials < Base
|
7
9
|
# Not used for this strategy
|
8
10
|
#
|
@@ -1,8 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module OAuth2
|
2
4
|
module Strategy
|
3
5
|
# The Implicit Strategy
|
4
6
|
#
|
5
|
-
# @see http://
|
7
|
+
# @see http://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-26#section-4.2
|
6
8
|
class Implicit < Base
|
7
9
|
# The required query parameters for the authorize URL
|
8
10
|
#
|
@@ -15,6 +17,7 @@ module OAuth2
|
|
15
17
|
#
|
16
18
|
# @param [Hash] params additional query parameters for the URL
|
17
19
|
def authorize_url(params = {})
|
20
|
+
assert_valid_params(params)
|
18
21
|
@client.authorize_url(authorize_params.merge(params))
|
19
22
|
end
|
20
23
|
|
@@ -24,6 +27,12 @@ module OAuth2
|
|
24
27
|
def get_token(*)
|
25
28
|
raise(NotImplementedError, 'The token is accessed differently in this strategy')
|
26
29
|
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def assert_valid_params(params)
|
34
|
+
raise(ArgumentError, 'client_secret is not allowed in authorize URL query params') if params.key?(:client_secret) || params.key?('client_secret')
|
35
|
+
end
|
27
36
|
end
|
28
37
|
end
|
29
38
|
end
|
@@ -1,8 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module OAuth2
|
2
4
|
module Strategy
|
3
5
|
# The Resource Owner Password Credentials Authorization Strategy
|
4
6
|
#
|
5
|
-
# @see http://
|
7
|
+
# @see http://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-15#section-4.3
|
6
8
|
class Password < Base
|
7
9
|
# Not used for this strategy
|
8
10
|
#
|
data/lib/oauth2/version.rb
CHANGED
@@ -2,64 +2,6 @@
|
|
2
2
|
|
3
3
|
module OAuth2
|
4
4
|
module Version
|
5
|
-
VERSION =
|
6
|
-
|
7
|
-
module_function
|
8
|
-
|
9
|
-
# The major version
|
10
|
-
#
|
11
|
-
# @return [Integer]
|
12
|
-
def major
|
13
|
-
1
|
14
|
-
end
|
15
|
-
|
16
|
-
# The minor version
|
17
|
-
#
|
18
|
-
# @return [Integer]
|
19
|
-
def minor
|
20
|
-
4
|
21
|
-
end
|
22
|
-
|
23
|
-
# The patch version
|
24
|
-
#
|
25
|
-
# @return [Integer]
|
26
|
-
def patch
|
27
|
-
7
|
28
|
-
end
|
29
|
-
|
30
|
-
# The pre-release version, if any
|
31
|
-
#
|
32
|
-
# @return [String, NilClass]
|
33
|
-
def pre
|
34
|
-
nil
|
35
|
-
end
|
36
|
-
|
37
|
-
# The version number as a hash
|
38
|
-
#
|
39
|
-
# @return [Hash]
|
40
|
-
def to_h
|
41
|
-
{
|
42
|
-
:major => major,
|
43
|
-
:minor => minor,
|
44
|
-
:patch => patch,
|
45
|
-
:pre => pre,
|
46
|
-
}
|
47
|
-
end
|
48
|
-
|
49
|
-
# The version number as an array
|
50
|
-
#
|
51
|
-
# @return [Array]
|
52
|
-
def to_a
|
53
|
-
[major, minor, patch, pre].compact
|
54
|
-
end
|
55
|
-
|
56
|
-
# The version number as a string
|
57
|
-
#
|
58
|
-
# @return [String]
|
59
|
-
def to_s
|
60
|
-
v = [major, minor, patch].compact.join('.')
|
61
|
-
v += "-#{pre}" if pre
|
62
|
-
v
|
63
|
-
end
|
5
|
+
VERSION = '2.0.2'.freeze
|
64
6
|
end
|
65
7
|
end
|
data/lib/oauth2.rb
CHANGED
@@ -1,4 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# includes modules from stdlib
|
4
|
+
require 'cgi'
|
5
|
+
require 'time'
|
6
|
+
|
7
|
+
# third party gems
|
8
|
+
require 'rash'
|
9
|
+
require 'version_gem'
|
10
|
+
|
11
|
+
# includes gem files
|
12
|
+
require 'oauth2/version'
|
1
13
|
require 'oauth2/error'
|
14
|
+
require 'oauth2/snaky_hash'
|
2
15
|
require 'oauth2/authenticator'
|
3
16
|
require 'oauth2/client'
|
4
17
|
require 'oauth2/strategy/base'
|
@@ -8,5 +21,12 @@ require 'oauth2/strategy/password'
|
|
8
21
|
require 'oauth2/strategy/client_credentials'
|
9
22
|
require 'oauth2/strategy/assertion'
|
10
23
|
require 'oauth2/access_token'
|
11
|
-
require 'oauth2/mac_token'
|
12
24
|
require 'oauth2/response'
|
25
|
+
|
26
|
+
# The namespace of this library
|
27
|
+
module OAuth2
|
28
|
+
end
|
29
|
+
|
30
|
+
OAuth2::Version.class_eval do
|
31
|
+
extend VersionGem::Basic
|
32
|
+
end
|