Browse files

wip refactoring to allow for a simpler API and also only showing Shut…

…l::Auth specific errors
  • Loading branch information...
1 parent a3cff45 commit a34d300a86019b7ec4967dea104aac798b2cb132 @markburns markburns committed Jan 1, 2013
View
2 Gemfile.lock
@@ -43,6 +43,7 @@ GEM
rspec-expectations (2.11.3)
diff-lcs (~> 1.1.3)
rspec-mocks (2.11.3)
+ vcr (2.3.0)
webmock (1.8.8)
addressable (~> 2.2.8)
crack (>= 0.1.7)
@@ -54,4 +55,5 @@ DEPENDENCIES
debugger
rspec (~> 2.11.0)
shutl_auth!
+ vcr
webmock (~> 1.8.7)
View
34 README.md
@@ -1,29 +1,15 @@
# ShutlAuth
-TODO: Write a gem description
+You probably won't use this gem directly, as it is used by the
+[https://github.com/shutl/shutl_resource](shutl_resource) gem
-## Installation
+#Configuration
-Add this line to your application's Gemfile:
+```ruby
+Shutl::Auth.config do |c|
+ c.url = "<API_URL_GOES_HERE>"
+ c.client_id = "<CLIENT_ID>"
+ c.client_secret = "<CLIENT_SECRET>"
+end
+```
- gem 'shutl_auth'
-
-And then execute:
-
- $ bundle
-
-Or install it yourself as:
-
- $ gem install shutl_auth
-
-## Usage
-
-TODO: Write usage instructions here
-
-## Contributing
-
-1. Fork it
-2. Create your feature branch (`git checkout -b my-new-feature`)
-3. Commit your changes (`git commit -am 'Add some feature'`)
-4. Push to the branch (`git push origin my-new-feature`)
-5. Create new Pull Request
View
39 lib/shutl/auth/access_token_request.rb
@@ -1,7 +1,11 @@
module Shutl
module Auth
+ class Shutl::Error < ::StandardError; end
+ class InvalidUrl < Shutl::Error; end
+ class InvalidCredentials < Shutl::Error; end
+
def access_token!
- access_token_response!.token
+ access_token_response!.access_token
end
def access_token_response!
@@ -13,22 +17,45 @@ def access_token_response!
private
def client
- #TODO: handle the various exceptions that can be thrown by the OAuth2
- #gem and turn into Shutl specific exceptions
- Rack::OAuth2::Client.new(
+ Rack::OAuth2::Client.new \
identifier: Shutl::Auth.client_id,
secret: Shutl::Auth.client_secret,
token_endpoint: '/token',
host: uri.host,
port: uri.port,
scheme: uri.scheme
- )
+
+ rescue Rack::OAuth2::Client::Error => e
+ debugger
+ puts e.message
+ raise_invalid_credentials
+
+ rescue Exception => e
+ debugger
+ puts e.message
+
end
def uri
- URI Shutl::Auth.url
+ check URI Shutl::Auth.url
+
+ rescue URI::InvalidURIError
+ raise_invalid_uri
end
+ def check uri
+ return uri if uri and uri.host and uri.scheme
+
+ raise_invalid_uri
+ end
+
+ def raise_invalid_uri
+ raise Shutl::Auth::InvalidUrl, "Please set value of Shutl::Auth.url"
+ end
+
+ def raise_invalid_credentials
+ raise Shutl::Auth::InvalidCredentials, "Invalid credentials set, please see https://github.com/shutl/shutl_auth/blob/master/README.md"
+ end
extend self
end
end
View
3 shutl_auth.gemspec
@@ -23,6 +23,5 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'rspec', '~> 2.11.0'
gem.add_development_dependency 'debugger'
gem.add_development_dependency 'webmock', '~> 1.8.7'
-
-
+ gem.add_development_dependency 'vcr'
end
View
50 spec/integration/integration_spec.rb
@@ -1,3 +1,53 @@
+require 'spec_helper'
+
+
describe "Integration" do
+ subject { Shutl::Auth }
+
+ def set_auth
+ Shutl::Auth.config do |c|
+ c.url = "http://localhost:3000"
+ c.client_id = "QUOTE_SERVICE_CLIENT_ID"
+ c.client_secret = "QUOTE_SERVICE_CLIENT_SECRET"
+ end
+ end
+
+ before do
+ set_auth
+ end
+
+ context 'successful request to authentication service' do
+ let(:token) { 's_CagcDP8PdsGb1B0iyLvNtanSxqZeQDQtGiIYtctKzyLzxAymhe-zGJwUrjxKQpO9EUdizDT3tqLt-iFeHapg' }
+
+ specify do
+ VCR.use_cassette 'get_token' do
+ Shutl::Auth.access_token!.should == token
+ end
+ end
+
+ specify "with invalid auth service url" do
+ Shutl::Auth.url = ''
+
+ expect {Shutl::Auth.access_token!}.to raise_error Shutl::Auth::InvalidUrl
+
+ Shutl::Auth.url = 'http://'
+ expect {Shutl::Auth.access_token!}.to raise_error Shutl::Auth::InvalidUrl
+
+ Shutl::Auth.url = 'http://localhost:3000'
+
+ VCR.use_cassette 'get_token' do
+ Shutl::Auth.access_token!.should == token
+ end
+ end
+
+ specify "with invalid credentials" do
+ set_auth
+ Shutl::Auth.client_id = 'egg'
+
+ VCR.use_cassette 'invalid_credentials' do
+ expect { Shutl::Auth.access_token!}.to raise_error Shutl::Auth::InvalidCredentials
+ end
+ end
+ end
end
View
13 spec/spec_helper.rb
@@ -5,3 +5,16 @@ def self.notify *args
end
end
+
+require 'vcr'
+
+VCR.configure do |c|
+ c.cassette_library_dir = 'spec/vcr'
+ c.hook_into :webmock
+ c.allow_http_connections_when_no_cassette = false
+ c.default_cassette_options = {
+ record: ENV['VCR_RERECORD'].present? ? :all : :once
+ }
+end
+
+
View
59 spec/vcr/get_token.yml
@@ -0,0 +1,59 @@
+---
+http_interactions:
+- request:
+ method: post
+ uri: http://localhost:3000/token
+ body:
+ encoding: ASCII-8BIT
+ string: !binary |-
+ Z3JhbnRfdHlwZT1jbGllbnRfY3JlZGVudGlhbHM=
+ headers:
+ Authorization:
+ - Basic UVVPVEVfU0VSVklDRV9DTElFTlRfSUQ6UVVPVEVfU0VSVklDRV9DTElFTlRfU0VDUkVU
+ Content-Type:
+ - application/x-www-form-urlencoded
+ response:
+ status:
+ code: 200
+ message: !binary |-
+ T0sg
+ headers:
+ !binary "Q29udGVudC1UeXBl":
+ - !binary |-
+ YXBwbGljYXRpb24vanNvbg==
+ !binary "Q29udGVudC1MZW5ndGg=":
+ - !binary |-
+ MTUw
+ !binary "Q2FjaGUtQ29udHJvbA==":
+ - !binary |-
+ bm8tc3RvcmU=
+ !binary "UHJhZ21h":
+ - !binary |-
+ bm8tY2FjaGU=
+ !binary "WC1VYS1Db21wYXRpYmxl":
+ - !binary |-
+ SUU9RWRnZQ==
+ !binary "RXRhZw==":
+ - !binary |-
+ IjkzNmFlZDkyMzFjODkxMzE3YzMwNzQwMjUyNTBmZDczIg==
+ !binary "WC1SZXF1ZXN0LUlk":
+ - !binary |-
+ ZDRhMWViZWZlMWUzMzgyNjAyNjFlMDNkNzcyNmUwYWI=
+ !binary "WC1SdW50aW1l":
+ - !binary |-
+ MC4wMTQyMzE=
+ !binary "U2VydmVy":
+ - !binary |-
+ V0VCcmljay8xLjMuMSAoUnVieS8xLjkuMy8yMDEyLTAyLTE2KQ==
+ !binary "RGF0ZQ==":
+ - !binary |-
+ VHVlLCAwMSBKYW4gMjAxMyAwNDoxNjowMCBHTVQ=
+ !binary "Q29ubmVjdGlvbg==":
+ - !binary |-
+ S2VlcC1BbGl2ZQ==
+ body:
+ encoding: US-ASCII
+ string: ! '{"access_token":"s_CagcDP8PdsGb1B0iyLvNtanSxqZeQDQtGiIYtctKzyLzxAymhe-zGJwUrjxKQpO9EUdizDT3tqLt-iFeHapg","token_type":"bearer","expires_in":788939999}'
+ http_version:
+ recorded_at: Tue, 01 Jan 2013 04:16:00 GMT
+recorded_with: VCR 2.3.0
View
59 spec/vcr/invalid_credentials.yml
@@ -0,0 +1,59 @@
+---
+http_interactions:
+- request:
+ method: post
+ uri: http://localhost:3000/token
+ body:
+ encoding: ASCII-8BIT
+ string: !binary |-
+ Z3JhbnRfdHlwZT1jbGllbnRfY3JlZGVudGlhbHM=
+ headers:
+ Authorization:
+ - Basic ZWdnOlFVT1RFX1NFUlZJQ0VfQ0xJRU5UX1NFQ1JFVA==
+ Content-Type:
+ - application/x-www-form-urlencoded
+ response:
+ status:
+ code: 401
+ message: !binary |-
+ VW5hdXRob3JpemVkIA==
+ headers:
+ !binary "Q29udGVudC1UeXBl":
+ - !binary |-
+ YXBwbGljYXRpb24vanNvbg==
+ !binary "V3d3LUF1dGhlbnRpY2F0ZQ==":
+ - !binary |-
+ QmFzaWMgcmVhbG09Ik9BdXRoMiBUb2tlbiBFbmRwb2ludCI=
+ !binary "Q29udGVudC1MZW5ndGg=":
+ - !binary |-
+ MjQ2
+ !binary "WC1VYS1Db21wYXRpYmxl":
+ - !binary |-
+ SUU9RWRnZQ==
+ !binary "Q2FjaGUtQ29udHJvbA==":
+ - !binary |-
+ bm8tY2FjaGU=
+ !binary "WC1SZXF1ZXN0LUlk":
+ - !binary |-
+ ZWE4N2IzOTA4ZjM0YThlZDNiNWM4NzYxYjZkZGZiOGM=
+ !binary "WC1SdW50aW1l":
+ - !binary |-
+ MC4wMDk1NDQ=
+ !binary "U2VydmVy":
+ - !binary |-
+ V0VCcmljay8xLjMuMSAoUnVieS8xLjkuMy8yMDEyLTAyLTE2KQ==
+ !binary "RGF0ZQ==":
+ - !binary |-
+ VHVlLCAwMSBKYW4gMjAxMyAwNDozMzozMiBHTVQ=
+ !binary "Q29ubmVjdGlvbg==":
+ - !binary |-
+ S2VlcC1BbGl2ZQ==
+ body:
+ encoding: US-ASCII
+ string: ! '{"error":"invalid_client","error_description":"The client identifier
+ provided is invalid, the client failed to authenticate, the client did not
+ include its credentials, provided multiple client credentials, or used unsupported
+ credentials type."}'
+ http_version:
+ recorded_at: Tue, 01 Jan 2013 04:33:32 GMT
+recorded_with: VCR 2.3.0

0 comments on commit a34d300

Please sign in to comment.