Skip to content

Commit 9579ba8

Browse files
author
Jan Xie
committedJan 18, 2012
initialize with a working base
0 parents  commit 9579ba8

File tree

13 files changed

+198
-0
lines changed

13 files changed

+198
-0
lines changed
 

‎.gitignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
*.gem
2+
*.rbc
3+
.bundle
4+
.config
5+
.yardoc
6+
Gemfile.lock
7+
InstalledFiles
8+
_yardoc
9+
coverage
10+
doc/
11+
lib/bundler/man
12+
pkg
13+
rdoc
14+
spec/reports
15+
test/tmp
16+
test/version_tmp
17+
tmp

‎.rspec

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--color
2+
--format=nested
3+
--backtrace

‎Gemfile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
source 'http://rubygems.org'
2+
3+
platforms :jruby do
4+
gem 'jruby-openssl', '~> 0.7'
5+
end
6+
7+
gemspec
8+
9+
group :development, :test do
10+
gem 'guard'
11+
gem 'guard-rspec'
12+
gem 'guard-bundler'
13+
gem 'growl'
14+
gem 'rb-fsevent'
15+
end

‎Guardfile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
guard 'rspec', :version => 2 do
2+
watch(%r{^spec/.+_spec\.rb$})
3+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
4+
watch('spec/spec_helper.rb') { "spec" }
5+
end
6+
7+
8+
guard 'bundler' do
9+
watch('Gemfile')
10+
watch(/^.+\.gemspec/)
11+
end

‎LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2011-2012 Jan Xie <jan.h.xie@gmail.com>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

‎README.rdoc

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
= OmniAuth::HttpBasic
2+
3+
OmniAuth stratgies for APIs that have HTTP Basic authentication.
4+
5+
This strategy is intended for developer's convinience, it will NOT set any uid or auth_hash data like a user strategy. So in most cases you need to create your own strategy by subclassing this strategy, and set uid and auth_hash there.
6+
7+
== Subclass Example (Recommended)
8+
9+
== Usage (Not Recommended)
10+
11+
Install manually or using Bundler:
12+
13+
gem 'omniauth-http-basic'
14+
15+
Add :http_basic provider to omniauth builder:
16+
17+
use OmniAuth::Builder do
18+
provider :http_basic, "https://example.com/user_info"
19+
# provider ...
20+
end
21+

‎Rakefile

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env rake
2+
3+
require 'bundler/gem_tasks'
4+
require 'rspec/core/rake_task'
5+
6+
desc 'Default: run specs'
7+
task :default => :spec
8+
9+
desc 'Run specs'
10+
RSpec::Core::RakeTask.new

‎lib/omniauth-http-basic.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require 'omniauth-http-basic/version'
2+
require 'omniauth/strategies/http_basic'

‎lib/omniauth-http-basic/version.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module OmniAuth
2+
module HttpBasic
3+
VERSION = "1.0.0"
4+
end
5+
end

‎lib/omniauth/strategies/http_basic.rb

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require 'omniauth'
2+
require 'net/http'
3+
4+
module OmniAuth
5+
module Strategies
6+
class HttpBasic
7+
include OmniAuth::Strategy
8+
9+
args [:endpoint]
10+
11+
option :name, "http_basic"
12+
option :title, "Http Basic"
13+
option :headers, {}
14+
15+
def request_phase
16+
OmniAuth::Form.build(:title => options.title, :url => callback_path) do
17+
text_field 'Username', 'username'
18+
password_field 'Password', 'password'
19+
end.to_response
20+
end
21+
22+
def callback_phase
23+
raise MissingCredentialsError.new("Missing login credentials") unless request['username'] && request['password']
24+
25+
begin
26+
uri = URI(options.endpoint)
27+
req = Net::HTTP::Get.new(uri.request_uri)
28+
req.basic_auth request['username'], request['password']
29+
30+
res = Net::HTTP.start(uri.host, uri.port) {|http| http.request(req) }
31+
return fail!(:invalid_credentials) if res.code.to_i >= 400
32+
33+
super
34+
rescue Exception => e
35+
fail!(:http_basic_error, e)
36+
end
37+
end
38+
39+
end
40+
end
41+
end

‎omniauth-http-basic.gemspec

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# encoding: utf-8
2+
require File.expand_path('../lib/omniauth-http-basic/version', __FILE__)
3+
4+
Gem::Specification.new do |gem|
5+
gem.add_dependency 'omniauth', '~> 1.0'
6+
gem.add_dependency 'faraday', '~> 0.7.5'
7+
8+
gem.add_development_dependency 'rspec', '~> 2.7'
9+
gem.add_development_dependency 'rack-test'
10+
gem.add_development_dependency 'simplecov'
11+
gem.add_development_dependency 'webmock'
12+
gem.add_development_dependency 'yard'
13+
14+
gem.authors = ['Jan Xie']
15+
gem.email = ['jan.h.xie@gmail.com']
16+
gem.description = %q{HTTP Basic strategies for OmniAuth.}
17+
gem.summary = gem.description
18+
gem.homepage = 'http://github.com/janx/omniauth-http-basic'
19+
20+
gem.name = 'omniauth-http-basic'
21+
gem.require_paths = ['lib']
22+
gem.files = `git ls-files`.split("\n")
23+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
24+
gem.version = OmniAuth::HttpBasic::VERSION
25+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require File.expand_path('../../../spec_helper', __FILE__)
2+
3+
describe OmniAuth::Strategies::HttpBasic do
4+
def app; lambda {|env| [200, {}, ["Hello HttpBasic!"]]}; end
5+
6+
let(:fresh_strategy) { Class.new OmniAuth::Strategies::HttpBasic }
7+
subject { fresh_strategy }
8+
9+
it 'should be initialized with authentication endpoint' do
10+
instance = subject.new(app, "http://www.example.com/httpauth")
11+
instance.options.endpoint.should == "http://www.example.com/httpauth"
12+
end
13+
end

‎spec/spec_helper.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
$:.unshift File.expand_path('..', __FILE__)
2+
$:.unshift File.expand_path('../../lib', __FILE__)
3+
4+
require 'simplecov'
5+
SimpleCov.start
6+
require 'rspec'
7+
require 'rack/test'
8+
require 'webmock/rspec'
9+
require 'omniauth'
10+
require 'omniauth-http-basic'
11+
12+
RSpec.configure do |config|
13+
config.include WebMock::API
14+
config.include Rack::Test::Methods
15+
config.extend OmniAuth::Test::StrategyMacros, :type => :strategy
16+
end

0 commit comments

Comments
 (0)