Skip to content

Commit 27dc224

Browse files
committedJul 8, 2013
Use own HTTP Basic Authentication implementation.
HTTP Basic Authentication is basically a one-liner and it's a waste to depend on a Gem for this functionality. Another reason not to depend on HTTPAuth is that it's no longer maintained.
1 parent 347fd4b commit 27dc224

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed
 

‎lib/oauth2/strategy/assertion.rb

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
require 'httpauth'
21
require 'jwt'
32

43
module OAuth2

‎lib/oauth2/strategy/client_credentials.rb

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'httpauth'
1+
require 'base64'
22

33
module OAuth2
44
module Strategy
@@ -20,9 +20,17 @@ def authorize_url
2020
def get_token(params={}, opts={})
2121
request_body = opts.delete('auth_scheme') == 'request_body'
2222
params.merge!('grant_type' => 'client_credentials')
23-
params.merge!(request_body ? client_params : {:headers => {'Authorization' => HTTPAuth::Basic.pack_authorization(client_params['client_id'], client_params['client_secret'])}})
23+
params.merge!(request_body ? client_params : {:headers => {'Authorization' => authorization(client_params['client_id'], client_params['client_secret'])}})
2424
@client.get_token(params, opts.merge('refresh_token' => nil))
2525
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
2634
end
2735
end
2836
end

‎oauth2.gemspec

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ require 'oauth2/version'
66
Gem::Specification.new do |spec|
77
spec.add_development_dependency 'bundler', '~> 1.0'
88
spec.add_dependency 'faraday', '~> 0.8'
9-
spec.add_dependency 'httpauth', '~> 0.2'
109
spec.add_dependency 'multi_json', '~> 1.0'
1110
spec.add_dependency 'multi_xml', '~> 0.5'
1211
spec.add_dependency 'rack', '~> 1.2'

‎spec/oauth2/strategy/client_credentials_spec.rb

+12-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
OAuth2::Client.new('abc', 'def', :site => 'http://api.example.com') do |builder|
99
builder.adapter :test do |stub|
1010
stub.post('/oauth/token', {'grant_type' => 'client_credentials'}) do |env|
11-
client_id, client_secret = HTTPAuth::Basic.unpack_authorization(env[:request_headers]['Authorization'])
11+
client_id, client_secret = Base64.decode64(env[:request_headers]['Authorization'].split(' ', 2)[1]).split(':', 2)
1212
client_id == 'abc' && client_secret == 'def' or raise Faraday::Adapter::Test::Stubs::NotFound.new
1313
case @mode
1414
when "formencoded"
@@ -37,6 +37,17 @@
3737
end
3838
end
3939

40+
describe "#authorization" do
41+
it "generates an Authorization header value for HTTP Basic Authentication" do
42+
[
43+
['abc', 'def', 'Basic YWJjOmRlZg=='],
44+
['xxx', 'secret', 'Basic eHh4OnNlY3JldA==']
45+
].each do |client_id, client_secret, expected|
46+
expect(subject.authorization(client_id, client_secret)).to eq(expected)
47+
end
48+
end
49+
end
50+
4051
%w(json formencoded).each do |mode|
4152
%w(default basic_auth request_body).each do |auth_scheme|
4253
describe "#get_token (#{mode}) (#{auth_scheme})" do

0 commit comments

Comments
 (0)