Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

started separation of auth stuff into its own gem

  • Loading branch information...
commit b03a92df8f858af4ccce381c87432bad0920b71b 1 parent 6839d90
Mark Burns markburns authored
1  .rbenv-version
View
@@ -0,0 +1 @@
+1.9.3-p125
4 Gemfile
View
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in shutl_auth.gemspec
+gemspec
55 Gemfile.lock
View
@@ -0,0 +1,55 @@
+PATH
+ remote: .
+ specs:
+ shutl_auth (0.0.1)
+ rack-oauth2
+
+GEM
+ remote: https://rubygems.org/
+ specs:
+ activesupport (3.2.9)
+ i18n (~> 0.6)
+ multi_json (~> 1.0)
+ addressable (2.2.8)
+ attr_required (0.0.5)
+ columnize (0.3.6)
+ crack (0.3.1)
+ debugger (1.2.2)
+ columnize (>= 0.3.1)
+ debugger-linecache (~> 1.1.1)
+ debugger-ruby_core_source (~> 1.1.5)
+ debugger-linecache (1.1.2)
+ debugger-ruby_core_source (>= 1.1.1)
+ debugger-ruby_core_source (1.1.5)
+ diff-lcs (1.1.3)
+ httpclient (2.3.0.1)
+ i18n (0.6.1)
+ multi_json (1.5.0)
+ rack (1.4.1)
+ rack-oauth2 (1.0.0)
+ activesupport (>= 2.3)
+ attr_required (>= 0.0.5)
+ httpclient (>= 2.2.0.2)
+ i18n
+ multi_json (>= 1.3.6)
+ rack (>= 1.1)
+ rspec (2.11.0)
+ rspec-core (~> 2.11.0)
+ rspec-expectations (~> 2.11.0)
+ rspec-mocks (~> 2.11.0)
+ rspec-core (2.11.1)
+ rspec-expectations (2.11.3)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.11.3)
+ webmock (1.8.8)
+ addressable (~> 2.2.8)
+ crack (>= 0.1.7)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ debugger
+ rspec (~> 2.11.0)
+ shutl_auth!
+ webmock (~> 1.8.7)
22 LICENSE.txt
View
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Mark Burns
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 README.md
View
@@ -1,2 +1,29 @@
-shutl_auth
-==========
+# ShutlAuth
+
+TODO: Write a gem description
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ 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
1  Rakefile
View
@@ -0,0 +1 @@
+require "bundler/gem_tasks"
7 lib/shutl_auth.rb
View
@@ -0,0 +1,7 @@
+require "shutl_auth/version"
+require "rack/oauth2"
+require "shutl_auth/access_token_request"
+require "shutl_auth/authentication"
+
+module ShutlAuth
+end
30 lib/shutl_auth/access_token_request.rb
View
@@ -0,0 +1,30 @@
+module Shutl
+ module Auth
+ class AccessTokenRequest
+ def initialize
+ #TODO: handle the various exceptions that can be thrown by the OAuth2 gem
+ #and turn into Shutl specific exceptions
+ @client = Rack::OAuth2::Client.new(
+ identifier: Shutl.client_id,
+ secret: Shutl.client_secret,
+ host: uri.host,
+ port: uri.port,
+ scheme: uri.scheme
+ )
+ end
+
+ def access_token!
+ Shutl.retry_connection "Authentication Service Error" do
+ @client.access_token!
+ end
+ end
+
+ private
+
+ def uri
+ #TODO: standardize naming/config of shutl_rails/shutl_resource/shutl_auth
+ @uri ||= URI Shutl.authentication_service[:url]
+ end
+ end
+ end
+end
29 lib/shutl_auth/authentication.rb
View
@@ -0,0 +1,29 @@
+#can be included in for example a Rails controller to enable easily
+#authenticating requests
+#Depends on session being defined in the included object, and this being hash-like
+module Shutl
+ module Auth
+ module AuthenticatedRequest
+ def request_access_token
+ return if session[:access_token]
+
+ access_token_response = Shutl::Auth::AccessTokenRequest.new.access_token!
+ access_token_response.access_token
+ end
+
+ def access_token
+ session[:access_token] ||= request_access_token
+ end
+
+ def authenticated_request &blk
+ begin
+ yield
+ rescue Shutl::UnauthorizedAccess => e
+ session[:access_token] = nil
+ request_access_token
+ yield
+ end
+ end
+ end
+ end
+end
5 lib/shutl_auth/version.rb
View
@@ -0,0 +1,5 @@
+module Shutl
+ module Auth
+ VERSION = "0.0.1"
+ end
+end
27 shutl_auth.gemspec
View
@@ -0,0 +1,27 @@
+# -*- encoding: utf-8 -*-
+lib = File.expand_path('../lib', __FILE__)
+$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
+require 'shutl_auth/version'
+
+Gem::Specification.new do |gem|
+ gem.name = "shutl_auth"
+ gem.version = Shutl::Auth::VERSION
+ gem.authors = ["Mark Burns"]
+ gem.email = ["markthedeveloper@gmail.com"]
+ gem.description = %q{Library used for using Shutl OAuth2 bearer tokens}
+ gem.summary = %q{Used by various gems/services for communicating with shutl oauth server}
+ gem.homepage = ""
+
+ gem.files = `git ls-files`.split($/)
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
+ gem.require_paths = ["lib"]
+
+ gem.add_dependency 'rack-oauth2'
+
+ gem.add_development_dependency 'rspec', '~> 2.11.0'
+ gem.add_development_dependency 'debugger'
+ gem.add_development_dependency 'webmock', '~> 1.8.7'
+
+
+end
41 spec/access_token_request_spec.rb
View
@@ -0,0 +1,41 @@
+require File.expand_path('spec/spec_helper')
+
+describe Shutl::Auth::AccessTokenRequest do
+ before do
+ Shutl.config do |s|
+ s.client_id = 'asdf'
+ s.client_secret = 'asdf'
+ end
+ end
+
+
+ subject { Shutl::Auth::AccessTokenRequest.new }
+
+ let(:oauth_client) { mock 'oauth client' }
+
+ before do
+ Rack::OAuth2::Client.stub(:new).and_return oauth_client
+ end
+
+ context 'successful request to authentication service' do
+ before do
+ oauth_client.stub(:access_token!).and_return 'token response'
+ end
+
+ specify do
+ subject.access_token!.should == 'token response'
+ end
+ end
+
+ describe 'retries on network error' do
+ before do
+ oauth_client.should_receive(:access_token!).
+ exactly(3).times.and_raise Timeout::Error
+ end
+
+ specify do
+ subject.access_token!
+ end
+ end
+end
+
1  spec/spec_helper.rb
View
@@ -0,0 +1 @@
+require 'webmock/rspec'
Please sign in to comment.
Something went wrong with that request. Please try again.