playlyfe 0.1.1 → 0.2.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/lib/access_token.rb +173 -0
- data/lib/base.rb +16 -0
- data/lib/client.rb +151 -0
- data/lib/client_credentials.rb +37 -0
- data/lib/error.rb +24 -0
- data/lib/playlyfe.rb +8 -1
- data/lib/response.rb +81 -0
- data/lib/strategy.rb +73 -0
- data/test/test.rb +10 -9
- metadata +33 -9
data/lib/access_token.rb
ADDED
@@ -0,0 +1,173 @@
|
|
1
|
+
module OAuth2
|
2
|
+
class AccessToken
|
3
|
+
attr_reader :client, :token, :expires_in, :expires_at, :params
|
4
|
+
attr_accessor :options, :refresh_token
|
5
|
+
|
6
|
+
class << self
|
7
|
+
# Initializes an AccessToken from a Hash
|
8
|
+
#
|
9
|
+
# @param [Client] the OAuth2::Client instance
|
10
|
+
# @param [Hash] a hash of AccessToken property values
|
11
|
+
# @return [AccessToken] the initalized AccessToken
|
12
|
+
def from_hash(client, hash)
|
13
|
+
new(client, hash.delete('access_token') || hash.delete(:access_token), hash)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Initializes an AccessToken from a key/value application/x-www-form-urlencoded string
|
17
|
+
#
|
18
|
+
# @param [Client] client the OAuth2::Client instance
|
19
|
+
# @param [String] kvform the application/x-www-form-urlencoded string
|
20
|
+
# @return [AccessToken] the initalized AccessToken
|
21
|
+
def from_kvform(client, kvform)
|
22
|
+
from_hash(client, Rack::Utils.parse_query(kvform))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Initalize an AccessToken
|
27
|
+
#
|
28
|
+
# @param [Client] client the OAuth2::Client instance
|
29
|
+
# @param [String] token the Access Token value
|
30
|
+
# @param [Hash] opts the options to create the Access Token with
|
31
|
+
# @option opts [String] :refresh_token (nil) the refresh_token value
|
32
|
+
# @option opts [FixNum, String] :expires_in (nil) the number of seconds in which the AccessToken will expire
|
33
|
+
# @option opts [FixNum, String] :expires_at (nil) the epoch time in seconds in which AccessToken will expire
|
34
|
+
# @option opts [Symbol] :mode (:header) the transmission mode of the Access Token parameter value
|
35
|
+
# one of :header, :body or :query
|
36
|
+
# @option opts [String] :header_format ('Bearer %s') the string format to use for the Authorization header
|
37
|
+
# @option opts [String] :param_name ('access_token') the parameter name to use for transmission of the
|
38
|
+
# Access Token value in :body or :query transmission mode
|
39
|
+
def initialize(client, token, opts = {})
|
40
|
+
@client = client
|
41
|
+
@token = token.to_s
|
42
|
+
[:refresh_token, :expires_in, :expires_at].each do |arg|
|
43
|
+
instance_variable_set("@#{arg}", opts.delete(arg) || opts.delete(arg.to_s))
|
44
|
+
end
|
45
|
+
@expires_in ||= opts.delete('expires')
|
46
|
+
@expires_in &&= @expires_in.to_i
|
47
|
+
@expires_at &&= @expires_at.to_i
|
48
|
+
@expires_at ||= Time.now.to_i + @expires_in if @expires_in
|
49
|
+
@options = {:mode => opts.delete(:mode) || :header,
|
50
|
+
:header_format => opts.delete(:header_format) || 'Bearer %s',
|
51
|
+
:param_name => opts.delete(:param_name) || 'access_token'}
|
52
|
+
@params = opts
|
53
|
+
end
|
54
|
+
|
55
|
+
# Indexer to additional params present in token response
|
56
|
+
#
|
57
|
+
# @param [String] key entry key to Hash
|
58
|
+
def [](key)
|
59
|
+
@params[key]
|
60
|
+
end
|
61
|
+
|
62
|
+
# Whether or not the token expires
|
63
|
+
#
|
64
|
+
# @return [Boolean]
|
65
|
+
def expires?
|
66
|
+
!!@expires_at # rubocop:disable DoubleNegation
|
67
|
+
end
|
68
|
+
|
69
|
+
# Whether or not the token is expired
|
70
|
+
#
|
71
|
+
# @return [Boolean]
|
72
|
+
def expired?
|
73
|
+
expires? && (expires_at < Time.now.to_i)
|
74
|
+
end
|
75
|
+
|
76
|
+
# Refreshes the current Access Token
|
77
|
+
#
|
78
|
+
# @return [AccessToken] a new AccessToken
|
79
|
+
# @note options should be carried over to the new AccessToken
|
80
|
+
def refresh!(params = {})
|
81
|
+
fail('A refresh_token is not available') unless refresh_token
|
82
|
+
params.merge!(:client_id => @client.id,
|
83
|
+
:client_secret => @client.secret,
|
84
|
+
:grant_type => 'refresh_token',
|
85
|
+
:refresh_token => refresh_token)
|
86
|
+
new_token = @client.get_token(params)
|
87
|
+
new_token.options = options
|
88
|
+
new_token.refresh_token = refresh_token unless new_token.refresh_token
|
89
|
+
new_token
|
90
|
+
end
|
91
|
+
|
92
|
+
# Convert AccessToken to a hash which can be used to rebuild itself with AccessToken.from_hash
|
93
|
+
#
|
94
|
+
# @return [Hash] a hash of AccessToken property values
|
95
|
+
def to_hash
|
96
|
+
params.merge(:access_token => token, :refresh_token => refresh_token, :expires_at => expires_at)
|
97
|
+
end
|
98
|
+
|
99
|
+
# Make a request with the Access Token
|
100
|
+
#
|
101
|
+
# @param [Symbol] verb the HTTP request method
|
102
|
+
# @param [String] path the HTTP URL path of the request
|
103
|
+
# @param [Hash] opts the options to make the request with
|
104
|
+
# @see Client#request
|
105
|
+
def request(verb, path, opts = {}, &block)
|
106
|
+
self.token = opts
|
107
|
+
@client.request(verb, path, opts, &block)
|
108
|
+
end
|
109
|
+
|
110
|
+
# Make a GET request with the Access Token
|
111
|
+
#
|
112
|
+
# @see AccessToken#request
|
113
|
+
def get(path, opts = {}, &block)
|
114
|
+
request(:get, path, opts, &block)
|
115
|
+
end
|
116
|
+
|
117
|
+
# Make a POST request with the Access Token
|
118
|
+
#
|
119
|
+
# @see AccessToken#request
|
120
|
+
def post(path, opts = {}, &block)
|
121
|
+
request(:post, path, opts, &block)
|
122
|
+
end
|
123
|
+
|
124
|
+
# Make a PUT request with the Access Token
|
125
|
+
#
|
126
|
+
# @see AccessToken#request
|
127
|
+
def put(path, opts = {}, &block)
|
128
|
+
request(:put, path, opts, &block)
|
129
|
+
end
|
130
|
+
|
131
|
+
# Make a PATCH request with the Access Token
|
132
|
+
#
|
133
|
+
# @see AccessToken#request
|
134
|
+
def patch(path, opts = {}, &block)
|
135
|
+
request(:patch, path, opts, &block)
|
136
|
+
end
|
137
|
+
|
138
|
+
# Make a DELETE request with the Access Token
|
139
|
+
#
|
140
|
+
# @see AccessToken#request
|
141
|
+
def delete(path, opts = {}, &block)
|
142
|
+
request(:delete, path, opts, &block)
|
143
|
+
end
|
144
|
+
|
145
|
+
# Get the headers hash (includes Authorization token)
|
146
|
+
def headers
|
147
|
+
{'Authorization' => options[:header_format] % token}
|
148
|
+
end
|
149
|
+
|
150
|
+
private
|
151
|
+
|
152
|
+
def token=(opts) # rubocop:disable MethodLength
|
153
|
+
case options[:mode]
|
154
|
+
when :header
|
155
|
+
opts[:headers] ||= {}
|
156
|
+
opts[:headers].merge!(headers)
|
157
|
+
when :query
|
158
|
+
opts[:params] ||= {}
|
159
|
+
opts[:params][options[:param_name]] = token
|
160
|
+
when :body
|
161
|
+
opts[:body] ||= {}
|
162
|
+
if opts[:body].is_a?(Hash)
|
163
|
+
opts[:body][options[:param_name]] = token
|
164
|
+
else
|
165
|
+
opts[:body] << "&#{options[:param_name]}=#{token}"
|
166
|
+
end
|
167
|
+
# @todo support for multi-part (file uploads)
|
168
|
+
else
|
169
|
+
fail("invalid :mode option of #{options[:mode]}")
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
data/lib/base.rb
ADDED
@@ -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
|
data/lib/client.rb
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'logger'
|
3
|
+
|
4
|
+
module OAuth2
|
5
|
+
# The OAuth2::Client class
|
6
|
+
class Client
|
7
|
+
attr_reader :id, :secret, :site
|
8
|
+
attr_accessor :options
|
9
|
+
attr_writer :connection
|
10
|
+
|
11
|
+
# Instantiate a new OAuth 2.0 client using the
|
12
|
+
# Client ID and Client Secret registered to your
|
13
|
+
# application.
|
14
|
+
#
|
15
|
+
# @param [String] client_id the client_id value
|
16
|
+
# @param [String] client_secret the client_secret value
|
17
|
+
# @param [Hash] opts the options to create the client with
|
18
|
+
# @option opts [String] :site the OAuth2 provider site host
|
19
|
+
# @option opts [String] :authorize_url ('/oauth/authorize') absolute or relative URL path to the Authorization endpoint
|
20
|
+
# @option opts [String] :token_url ('/oauth/token') absolute or relative URL path to the Token endpoint
|
21
|
+
# @option opts [Symbol] :token_method (:post) HTTP method to use to request token (:get or :post)
|
22
|
+
# @option opts [Hash] :connection_opts ({}) Hash of connection options to pass to initialize Faraday with
|
23
|
+
# @option opts [FixNum] :max_redirects (5) maximum number of redirects to follow
|
24
|
+
# @option opts [Boolean] :raise_errors (true) whether or not to raise an OAuth2::Error
|
25
|
+
# on responses with 400+ status codes
|
26
|
+
# @yield [builder] The Faraday connection builder
|
27
|
+
def initialize(client_id, client_secret, options = {}, &block)
|
28
|
+
opts = options.dup
|
29
|
+
@id = client_id
|
30
|
+
@secret = client_secret
|
31
|
+
@site = opts.delete(:site)
|
32
|
+
ssl = opts.delete(:ssl)
|
33
|
+
@options = {:authorize_url => '/oauth/authorize',
|
34
|
+
:token_url => '/oauth/token',
|
35
|
+
:token_method => :post,
|
36
|
+
:connection_opts => {},
|
37
|
+
:connection_build => block,
|
38
|
+
:max_redirects => 5,
|
39
|
+
:raise_errors => true}.merge(opts)
|
40
|
+
@options[:connection_opts][:ssl] = ssl if ssl
|
41
|
+
end
|
42
|
+
|
43
|
+
# Set the site host
|
44
|
+
#
|
45
|
+
# @param [String] the OAuth2 provider site host
|
46
|
+
def site=(value)
|
47
|
+
@connection = nil
|
48
|
+
@site = value
|
49
|
+
end
|
50
|
+
|
51
|
+
# The Faraday connection object
|
52
|
+
def connection
|
53
|
+
@connection ||= begin
|
54
|
+
conn = Faraday.new(site, options[:connection_opts])
|
55
|
+
conn.build do |b|
|
56
|
+
options[:connection_build].call(b)
|
57
|
+
end if options[:connection_build]
|
58
|
+
conn
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# The authorize endpoint URL of the OAuth2 provider
|
63
|
+
#
|
64
|
+
# @param [Hash] params additional query parameters
|
65
|
+
def authorize_url(params = nil)
|
66
|
+
connection.build_url(options[:authorize_url], params).to_s
|
67
|
+
end
|
68
|
+
|
69
|
+
# The token endpoint URL of the OAuth2 provider
|
70
|
+
#
|
71
|
+
# @param [Hash] params additional query parameters
|
72
|
+
def token_url(params = nil)
|
73
|
+
connection.build_url(options[:token_url], params).to_s
|
74
|
+
end
|
75
|
+
|
76
|
+
# Makes a request relative to the specified site root.
|
77
|
+
#
|
78
|
+
# @param [Symbol] verb one of :get, :post, :put, :delete
|
79
|
+
# @param [String] url URL path of request
|
80
|
+
# @param [Hash] opts the options to make the request with
|
81
|
+
# @option opts [Hash] :params additional query parameters for the URL of the request
|
82
|
+
# @option opts [Hash, String] :body the body of the request
|
83
|
+
# @option opts [Hash] :headers http request headers
|
84
|
+
# @option opts [Boolean] :raise_errors whether or not to raise an OAuth2::Error on 400+ status
|
85
|
+
# code response for this request. Will default to client option
|
86
|
+
# @option opts [Symbol] :parse @see Response::initialize
|
87
|
+
# @yield [req] The Faraday request
|
88
|
+
def request(verb, url, opts = {}) # rubocop:disable CyclomaticComplexity, MethodLength
|
89
|
+
connection.response :logger, ::Logger.new($stdout) if ENV['OAUTH_DEBUG'] == 'true'
|
90
|
+
|
91
|
+
url = connection.build_url(url, opts[:params]).to_s
|
92
|
+
|
93
|
+
response = connection.run_request(verb, url, opts[:body], opts[:headers]) do |req|
|
94
|
+
yield(req) if block_given?
|
95
|
+
end
|
96
|
+
response = Response.new(response, :parse => opts[:parse])
|
97
|
+
|
98
|
+
case response.status
|
99
|
+
when 301, 302, 303, 307
|
100
|
+
opts[:redirect_count] ||= 0
|
101
|
+
opts[:redirect_count] += 1
|
102
|
+
return response if opts[:redirect_count] > options[:max_redirects]
|
103
|
+
if response.status == 303
|
104
|
+
verb = :get
|
105
|
+
opts.delete(:body)
|
106
|
+
end
|
107
|
+
request(verb, response.headers['location'], opts)
|
108
|
+
when 200..299, 300..399
|
109
|
+
# on non-redirecting 3xx statuses, just return the response
|
110
|
+
response
|
111
|
+
when 400..599
|
112
|
+
error = Error.new(response)
|
113
|
+
fail(error) if opts.fetch(:raise_errors, options[:raise_errors])
|
114
|
+
response.error = error
|
115
|
+
response
|
116
|
+
else
|
117
|
+
error = Error.new(response)
|
118
|
+
fail(error, "Unhandled status code value of #{response.status}")
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
# Initializes an AccessToken by making a request to the token endpoint
|
123
|
+
#
|
124
|
+
# @param [Hash] params a Hash of params for the token endpoint
|
125
|
+
# @param [Hash] access token options, to pass to the AccessToken object
|
126
|
+
# @param [Class] class of access token for easier subclassing OAuth2::AccessToken
|
127
|
+
# @return [AccessToken] the initalized AccessToken
|
128
|
+
def get_token(params, access_token_opts = {}, access_token_class = AccessToken)
|
129
|
+
opts = {:raise_errors => options[:raise_errors], :parse => params.delete(:parse)}
|
130
|
+
if options[:token_method] == :post
|
131
|
+
headers = params.delete(:headers)
|
132
|
+
opts[:body] = params
|
133
|
+
opts[:headers] = {'Content-Type' => 'application/x-www-form-urlencoded'}
|
134
|
+
opts[:headers].merge!(headers) if headers
|
135
|
+
else
|
136
|
+
opts[:params] = params
|
137
|
+
end
|
138
|
+
response = request(options[:token_method], token_url, opts)
|
139
|
+
error = Error.new(response)
|
140
|
+
fail(error) if options[:raise_errors] && !(response.parsed.is_a?(Hash) && response.parsed['access_token'])
|
141
|
+
access_token_class.from_hash(self, response.parsed.merge(access_token_opts))
|
142
|
+
end
|
143
|
+
|
144
|
+
# The Client Credentials strategy
|
145
|
+
#
|
146
|
+
# @see http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-4.4
|
147
|
+
def client_credentials
|
148
|
+
@client_credentials ||= OAuth2::Strategy::ClientCredentials.new(self)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'base64'
|
2
|
+
|
3
|
+
module OAuth2
|
4
|
+
module Strategy
|
5
|
+
# The Client Credentials Strategy
|
6
|
+
#
|
7
|
+
# @see http://tools.ietf.org/html/draft-ietf-oauth-v2-15#section-4.4
|
8
|
+
class ClientCredentials < Base
|
9
|
+
# Not used for this strategy
|
10
|
+
#
|
11
|
+
# @raise [NotImplementedError]
|
12
|
+
def authorize_url
|
13
|
+
fail(NotImplementedError, 'The authorization endpoint is not used in this strategy')
|
14
|
+
end
|
15
|
+
|
16
|
+
# Retrieve an access token given the specified client.
|
17
|
+
#
|
18
|
+
# @param [Hash] params additional params
|
19
|
+
# @param [Hash] opts options
|
20
|
+
def get_token(params = {}, opts = {})
|
21
|
+
request_body = opts.delete('auth_scheme') == 'request_body'
|
22
|
+
params.merge!('grant_type' => 'client_credentials')
|
23
|
+
params.merge!(request_body ? client_params : {:headers => {'Authorization' => authorization(client_params['client_id'], client_params['client_secret'])}})
|
24
|
+
@client.get_token(params, opts.merge('refresh_token' => nil))
|
25
|
+
end
|
26
|
+
|
27
|
+
# Returns the Authorization header value for Basic Authentication
|
28
|
+
#
|
29
|
+
# @param [String] The client ID
|
30
|
+
# @param [String] the client secret
|
31
|
+
def authorization(client_id, client_secret)
|
32
|
+
'Basic ' + Base64.encode64(client_id + ':' + client_secret).gsub("\n", '')
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
data/lib/error.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
module OAuth2
|
2
|
+
class Error < StandardError
|
3
|
+
attr_reader :response, :code, :description
|
4
|
+
|
5
|
+
# standard error values include:
|
6
|
+
# :invalid_request, :invalid_client, :invalid_token, :invalid_grant, :unsupported_grant_type, :invalid_scope
|
7
|
+
def initialize(response)
|
8
|
+
response.error = self
|
9
|
+
@response = response
|
10
|
+
|
11
|
+
message = []
|
12
|
+
|
13
|
+
if response.parsed.is_a?(Hash)
|
14
|
+
@code = response.parsed['error']
|
15
|
+
@description = response.parsed['error_description']
|
16
|
+
message << "#{@code}: #{@description}"
|
17
|
+
end
|
18
|
+
|
19
|
+
message << response.body
|
20
|
+
|
21
|
+
super(message.join("\n"))
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/playlyfe.rb
CHANGED
@@ -1,5 +1,11 @@
|
|
1
|
-
require 'oauth2'
|
2
1
|
require 'json'
|
2
|
+
require 'error'
|
3
|
+
require 'client'
|
4
|
+
require 'base'
|
5
|
+
require 'strategy'
|
6
|
+
require 'client_credentials'
|
7
|
+
require 'access_token'
|
8
|
+
require 'response'
|
3
9
|
|
4
10
|
class Playlyfe
|
5
11
|
@@client = nil
|
@@ -64,6 +70,7 @@ class Playlyfe
|
|
64
70
|
end
|
65
71
|
|
66
72
|
def self.post(options = {})
|
73
|
+
options[:player] ||= ''
|
67
74
|
opts = {}
|
68
75
|
opts[:headers] ||= {'Content-Type' => 'application/json'}
|
69
76
|
opts[:body] = JSON.generate(options[:body])
|
data/lib/response.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
module OAuth2
|
2
|
+
# OAuth2::Response class
|
3
|
+
class Response
|
4
|
+
attr_reader :response
|
5
|
+
attr_accessor :error, :options
|
6
|
+
|
7
|
+
# Adds a new content type parser.
|
8
|
+
#
|
9
|
+
# @param [Symbol] key A descriptive symbol key such as :json or :query.
|
10
|
+
# @param [Array] One or more mime types to which this parser applies.
|
11
|
+
# @yield [String] A block returning parsed content.
|
12
|
+
def self.register_parser(key, mime_types, &block)
|
13
|
+
key = key.to_sym
|
14
|
+
PARSERS[key] = block
|
15
|
+
Array(mime_types).each do |mime_type|
|
16
|
+
CONTENT_TYPES[mime_type] = key
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Initializes a Response instance
|
21
|
+
#
|
22
|
+
# @param [Faraday::Response] response The Faraday response instance
|
23
|
+
# @param [Hash] opts options in which to initialize the instance
|
24
|
+
# @option opts [Symbol] :parse (:automatic) how to parse the response body. one of :query (for x-www-form-urlencoded),
|
25
|
+
# :json, or :automatic (determined by Content-Type response header)
|
26
|
+
def initialize(response, opts = {})
|
27
|
+
@response = response
|
28
|
+
@options = {:parse => :automatic}.merge(opts)
|
29
|
+
end
|
30
|
+
|
31
|
+
# The HTTP response headers
|
32
|
+
def headers
|
33
|
+
response.headers
|
34
|
+
end
|
35
|
+
|
36
|
+
# The HTTP response status code
|
37
|
+
def status
|
38
|
+
response.status
|
39
|
+
end
|
40
|
+
|
41
|
+
# The HTTP response body
|
42
|
+
def body
|
43
|
+
response.body || ''
|
44
|
+
end
|
45
|
+
|
46
|
+
# Procs that, when called, will parse a response body according
|
47
|
+
# to the specified format.
|
48
|
+
PARSERS = {
|
49
|
+
:json => lambda { |body| JSON.parse(body) rescue body }, # rubocop:disable RescueModifier
|
50
|
+
#:query => lambda { |body| Rack::Utils.parse_query(body) },
|
51
|
+
:text => lambda { |body| body }
|
52
|
+
}
|
53
|
+
|
54
|
+
# Content type assignments for various potential HTTP content types.
|
55
|
+
CONTENT_TYPES = {
|
56
|
+
'application/json' => :json,
|
57
|
+
'text/javascript' => :json,
|
58
|
+
'application/x-www-form-urlencoded' => :query,
|
59
|
+
'text/plain' => :text
|
60
|
+
}
|
61
|
+
|
62
|
+
# The parsed response body.
|
63
|
+
# Will attempt to parse application/x-www-form-urlencoded and
|
64
|
+
# application/json Content-Type response bodies
|
65
|
+
def parsed
|
66
|
+
return nil unless PARSERS.key?(parser)
|
67
|
+
@parsed ||= PARSERS[parser].call(body)
|
68
|
+
end
|
69
|
+
|
70
|
+
# Attempts to determine the content type of the response.
|
71
|
+
def content_type
|
72
|
+
((response.headers.values_at('content-type', 'Content-Type').compact.first || '').split(';').first || '').strip
|
73
|
+
end
|
74
|
+
|
75
|
+
# Determines the parser that will be used to supply the content of #parsed
|
76
|
+
def parser
|
77
|
+
return options[:parse].to_sym if PARSERS.key?(options[:parse])
|
78
|
+
CONTENT_TYPES[content_type]
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
data/lib/strategy.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'jwt'
|
2
|
+
|
3
|
+
module OAuth2
|
4
|
+
module Strategy
|
5
|
+
# The Client Assertion Strategy
|
6
|
+
#
|
7
|
+
# @see http://tools.ietf.org/html/draft-ietf-oauth-v2-10#section-4.1.3
|
8
|
+
#
|
9
|
+
# Sample usage:
|
10
|
+
# client = OAuth2::Client.new(client_id, client_secret,
|
11
|
+
# :site => 'http://localhost:8080')
|
12
|
+
#
|
13
|
+
# params = {:hmac_secret => "some secret",
|
14
|
+
# # or :private_key => "private key string",
|
15
|
+
# :iss => "http://localhost:3001",
|
16
|
+
# :prn => "me@here.com",
|
17
|
+
# :exp => Time.now.utc.to_i + 3600}
|
18
|
+
#
|
19
|
+
# access = client.assertion.get_token(params)
|
20
|
+
# access.token # actual access_token string
|
21
|
+
# access.get("/api/stuff") # making api calls with access token in header
|
22
|
+
#
|
23
|
+
class Assertion < Base
|
24
|
+
# Not used for this strategy
|
25
|
+
#
|
26
|
+
# @raise [NotImplementedError]
|
27
|
+
def authorize_url
|
28
|
+
fail(NotImplementedError, 'The authorization endpoint is not used in this strategy')
|
29
|
+
end
|
30
|
+
|
31
|
+
# Retrieve an access token given the specified client.
|
32
|
+
#
|
33
|
+
# @param [Hash] params assertion params
|
34
|
+
# pass either :hmac_secret or :private_key, but not both.
|
35
|
+
#
|
36
|
+
# params :hmac_secret, secret string.
|
37
|
+
# params :private_key, private key string.
|
38
|
+
#
|
39
|
+
# params :iss, issuer
|
40
|
+
# params :aud, audience, optional
|
41
|
+
# params :prn, principal, current user
|
42
|
+
# params :exp, expired at, in seconds, like Time.now.utc.to_i + 3600
|
43
|
+
#
|
44
|
+
# @param [Hash] opts options
|
45
|
+
def get_token(params = {}, opts = {})
|
46
|
+
hash = build_request(params)
|
47
|
+
@client.get_token(hash, opts.merge('refresh_token' => nil))
|
48
|
+
end
|
49
|
+
|
50
|
+
def build_request(params)
|
51
|
+
assertion = build_assertion(params)
|
52
|
+
{:grant_type => 'assertion',
|
53
|
+
:assertion_type => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
|
54
|
+
:assertion => assertion,
|
55
|
+
:scope => params[:scope]
|
56
|
+
}.merge(client_params)
|
57
|
+
end
|
58
|
+
|
59
|
+
def build_assertion(params)
|
60
|
+
claims = {:iss => params[:iss],
|
61
|
+
:aud => params[:aud],
|
62
|
+
:prn => params[:prn],
|
63
|
+
:exp => params[:exp]
|
64
|
+
}
|
65
|
+
if params[:hmac_secret]
|
66
|
+
JWT.encode(claims, params[:hmac_secret], 'HS256')
|
67
|
+
elsif params[:private_key]
|
68
|
+
JWT.encode(claims, params[:private_key], 'RS256')
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
data/test/test.rb
CHANGED
@@ -5,8 +5,8 @@ class PlaylyfeTest < Test::Unit::TestCase
|
|
5
5
|
|
6
6
|
def test_start
|
7
7
|
Playlyfe.start(
|
8
|
-
client_id: "
|
9
|
-
client_secret: "
|
8
|
+
client_id: "NjZlZWFiMWEtN2ZiOC00YmVmLTk2YWMtNDEyYTNmMjQ5NDBl",
|
9
|
+
client_secret: "NGRhMWJlYzUtODJiNS00ZTdkLTgzNTctMTQyMGEzNTljN2MwMTU2MGM3NTAtZjZiZS0xMWUzLTgxZjYtNmZhMGYyMzRkZGU3"
|
10
10
|
)
|
11
11
|
puts Playlyfe.token
|
12
12
|
|
@@ -14,17 +14,18 @@ class PlaylyfeTest < Test::Unit::TestCase
|
|
14
14
|
assert_not_nil players["data"]
|
15
15
|
assert_not_nil players["data"][0]
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
assert_equal player["
|
17
|
+
player_id = 'student1'
|
18
|
+
player = Playlyfe.get(url: '/player', player: player_id)
|
19
|
+
assert_equal player["id"], "student1"
|
20
|
+
assert_equal player["alias"], "Student1"
|
20
21
|
assert_equal player["enabled"], true
|
21
22
|
|
22
23
|
no_player = Playlyfe.get(url: '/player')
|
23
24
|
assert_nil no_player
|
24
25
|
|
25
|
-
Playlyfe.get(url: "/definitions/processes", player:
|
26
|
-
Playlyfe.get(url: "/definitions/teams", player:
|
27
|
-
Playlyfe.get(url: "/processes", player:
|
28
|
-
Playlyfe.get(url: "/teams", player:
|
26
|
+
Playlyfe.get(url: "/definitions/processes", player: player_id)
|
27
|
+
Playlyfe.get(url: "/definitions/teams", player: player_id)
|
28
|
+
Playlyfe.get(url: "/processes", player: player_id)
|
29
|
+
Playlyfe.get(url: "/teams", player: player_id)
|
29
30
|
end
|
30
31
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: playlyfe
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,30 +9,46 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-06-
|
12
|
+
date: 2014-06-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
15
|
+
name: faraday
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
|
-
- -
|
19
|
+
- - '='
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: 0.
|
21
|
+
version: '0.8'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
25
|
none: false
|
26
26
|
requirements:
|
27
|
-
- -
|
27
|
+
- - '='
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: 0.
|
29
|
+
version: '0.8'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: jwt
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - '='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - '='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.0'
|
30
46
|
- !ruby/object:Gem::Dependency
|
31
47
|
name: json
|
32
48
|
requirement: !ruby/object:Gem::Requirement
|
33
49
|
none: false
|
34
50
|
requirements:
|
35
|
-
- -
|
51
|
+
- - '='
|
36
52
|
- !ruby/object:Gem::Version
|
37
53
|
version: 1.8.1
|
38
54
|
type: :runtime
|
@@ -40,7 +56,7 @@ dependencies:
|
|
40
56
|
version_requirements: !ruby/object:Gem::Requirement
|
41
57
|
none: false
|
42
58
|
requirements:
|
43
|
-
- -
|
59
|
+
- - '='
|
44
60
|
- !ruby/object:Gem::Version
|
45
61
|
version: 1.8.1
|
46
62
|
description: This gem can be used to interact with the playlyfe gamification platform
|
@@ -52,6 +68,13 @@ extra_rdoc_files: []
|
|
52
68
|
files:
|
53
69
|
- Rakefile
|
54
70
|
- lib/playlyfe.rb
|
71
|
+
- lib/access_token.rb
|
72
|
+
- lib/base.rb
|
73
|
+
- lib/client.rb
|
74
|
+
- lib/client_credentials.rb
|
75
|
+
- lib/error.rb
|
76
|
+
- lib/response.rb
|
77
|
+
- lib/strategy.rb
|
55
78
|
- test/test.rb
|
56
79
|
homepage: https://github.com/pyros2097/playlyfe-ruby-sdk
|
57
80
|
licenses:
|
@@ -80,3 +103,4 @@ specification_version: 3
|
|
80
103
|
summary: The playlyfe ruby sdk
|
81
104
|
test_files:
|
82
105
|
- test/test.rb
|
106
|
+
has_rdoc:
|