mirror of
https://github.com/omniauth/omniauth.git
synced 2022-11-09 12:31:49 -05:00
Starting work on identity.
This commit is contained in:
parent
4d11f31318
commit
7e3358157e
9 changed files with 119 additions and 22 deletions
|
@ -27,7 +27,7 @@ module OmniAuth
|
|||
end
|
||||
|
||||
def get_credentials
|
||||
OmniAuth::Form.build(title) do
|
||||
OmniAuth::Form.build(:title => title) do
|
||||
text_field 'Username', 'username'
|
||||
password_field 'Password', 'password'
|
||||
end.to_response
|
||||
|
|
|
@ -72,12 +72,28 @@ module OmniAuth
|
|||
display: block;
|
||||
margin: 20px auto 0;
|
||||
}
|
||||
|
||||
fieldset {
|
||||
border: 1px solid #ccc;
|
||||
border-left: 0;
|
||||
border-right: 0;
|
||||
padding: 10px 0;
|
||||
}
|
||||
|
||||
fieldset input {
|
||||
width: 260px;
|
||||
font-size: 16px;
|
||||
}
|
||||
CSS
|
||||
|
||||
def initialize(title=nil)
|
||||
title ||= "Authentication Info Required"
|
||||
attr_accessor :options
|
||||
|
||||
def initialize(options = {})
|
||||
options[:title] ||= "Authentication Info Required"
|
||||
self.options = options
|
||||
|
||||
@html = ""
|
||||
header(title)
|
||||
header(options[:title])
|
||||
end
|
||||
|
||||
def self.build(title=nil, &block)
|
||||
|
@ -106,6 +122,21 @@ module OmniAuth
|
|||
input_field('password', name)
|
||||
self
|
||||
end
|
||||
|
||||
def button(text)
|
||||
@html << "\n<button type='submit'>#{text}</button>"
|
||||
end
|
||||
|
||||
def html(html)
|
||||
@html << html
|
||||
end
|
||||
|
||||
def fieldset(legend, options = {}, &block)
|
||||
@html << "\n<fieldset#{" style='#{options[:style]}'" if options[:style]}#{" id='#{options[:id]}'" if options[:id]}>\n <legend>#{legend}</legend>\n"
|
||||
self.instance_eval &block
|
||||
@html << "\n</fieldset>"
|
||||
self
|
||||
end
|
||||
|
||||
def header(title)
|
||||
@html << <<-HTML
|
||||
|
@ -117,7 +148,7 @@ module OmniAuth
|
|||
</head>
|
||||
<body>
|
||||
<h1>#{title}</h1>
|
||||
<form method='post' noValidate='noValidate'>
|
||||
<form method='post' #{"action='#{options[:url]}' " if options[:url]}noValidate='noValidate'>
|
||||
HTML
|
||||
self
|
||||
end
|
||||
|
|
|
@ -45,7 +45,7 @@ module OmniAuth
|
|||
end
|
||||
|
||||
def get_credentials
|
||||
OmniAuth::Form.build(options[:title] || "LDAP Authentication") do
|
||||
OmniAuth::Form.build(:title => (options[:title] || "LDAP Authentication")) do
|
||||
text_field 'Login', 'username'
|
||||
password_field 'Password', 'password'
|
||||
end.to_response
|
||||
|
|
|
@ -4,7 +4,7 @@ Bundler.setup
|
|||
require 'rake'
|
||||
|
||||
require 'mg'
|
||||
MG.new('oa-more.gemspec')
|
||||
MG.new('oa-identity.gemspec')
|
||||
|
||||
require 'rspec/core/rake_task'
|
||||
RSpec::Core::RakeTask.new(:spec) do |s|
|
||||
|
|
|
@ -5,18 +5,38 @@ module OmniAuth
|
|||
class Identity
|
||||
include OmniAuth::Strategy
|
||||
|
||||
def initialize(app, options = {})
|
||||
super(app, :identity, options)
|
||||
def initialize(app, options = {}, &block)
|
||||
options[:key] ||= 'nickname'
|
||||
super(app, :identity, options, &block)
|
||||
end
|
||||
|
||||
def request_phase
|
||||
OmniAuth::Form.build(options[:title] || 'Identify Yourself') do
|
||||
text_field 'Screen Name', 'nickname'
|
||||
OmniAuth::Form.build(:title => (self.options[:title] || 'Identify Yourself'), :url => callback_path) do
|
||||
text_field 'Login', 'key'
|
||||
password_field 'Password', 'password'
|
||||
|
||||
html <<-HTML
|
||||
<a id='toggle_register' href='javascript:document.getElementById("toggle_register").style.display="none";document.getElementById("register").style.display = "block"'>Create a New Identity</a>
|
||||
HTML
|
||||
|
||||
fieldset("Create Identity", :id => 'register', :style => 'display:none;') do
|
||||
password_field 'Repeat Password', 'register[password_confirmation]'
|
||||
text_field 'E-Mail', 'register[email]'
|
||||
text_field 'Name', 'register[name]'
|
||||
end
|
||||
end.to_response
|
||||
end
|
||||
|
||||
|
||||
def callback_phase
|
||||
if auth_hash
|
||||
super
|
||||
else
|
||||
fail!(:invalid_credentials)
|
||||
end
|
||||
end
|
||||
|
||||
def auth_hash
|
||||
@hash ||= OmniAuth::Identity::Model.identify(options[:key],request.params['key'],request.params['password'])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,16 +1,59 @@
|
|||
require 'spec_helper'
|
||||
|
||||
module OmniAuth
|
||||
module Identity
|
||||
class Model
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe OmniAuth::Strategies::Identity do
|
||||
def app
|
||||
Rack::Builder.new do
|
||||
use Rack::Session::Cookie
|
||||
use OmniAuth::Strategies::Identity, @options
|
||||
run @app || lambda{|env| [200, {}, env.inspect]}
|
||||
end.to_app
|
||||
b = Rack::Builder.new
|
||||
b.use Rack::Session::Cookie
|
||||
b.use OmniAuth::Strategies::Identity, @options
|
||||
b.run @rack_app || lambda{|env| [@status || 404, {'env' => env}, ["Boing"]]}
|
||||
b.to_app
|
||||
end
|
||||
|
||||
it 'should display a form' do
|
||||
get '/auth/identity'
|
||||
last_response.body.should be_include("<form")
|
||||
before(:each) do
|
||||
OmniAuth.config.on_failure = lambda{|env| [500, {}, [env['omniauth.error.type'].to_s]]}
|
||||
@options = {:key => 'nickname'}
|
||||
end
|
||||
|
||||
context 'request phase' do
|
||||
it 'should display a form' do
|
||||
get '/auth/identity'
|
||||
last_response.body.should be_include("<form")
|
||||
end
|
||||
|
||||
it 'should request the specified key' do
|
||||
pending "Let's get the basics working first."
|
||||
@options = {:key => 'email'}
|
||||
get '/auth/identity'
|
||||
last_response.body.should be_include("Email")
|
||||
end
|
||||
end
|
||||
|
||||
context 'callback phase' do
|
||||
context 'with an existing user' do
|
||||
it 'should return the user info if a correct password is specified' do
|
||||
OmniAuth::Identity::Model.should_receive(:identify).
|
||||
with('nickname','existing','correct').
|
||||
and_return({'uid' => '123abc'})
|
||||
post '/auth/identity/callback', :key => 'existing', :password => 'correct'
|
||||
last_response.headers['env']['omniauth.auth']['uid'].should == '123abc'
|
||||
end
|
||||
|
||||
it 'should fail with :invalid_credentials if no user exists' do
|
||||
OmniAuth::Identity::Model.should_receive(:identify).
|
||||
with('nickname','existing','wrong').
|
||||
and_return(nil)
|
||||
post '/auth/identity/callback', :key => 'existing', :password => 'wrong'
|
||||
|
||||
last_response.status.should == 500
|
||||
last_response.body.should == 'invalid_credentials'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
require 'rubygems'
|
||||
require 'bundler'
|
||||
Bundler.setup
|
||||
|
||||
require 'rspec'
|
||||
require 'rspec/autorun'
|
||||
require 'rack/test'
|
||||
|
|
|
@ -9,7 +9,7 @@ module OmniAuth
|
|||
end
|
||||
|
||||
def get_identifier
|
||||
OmniAuth::Form.build('Google Apps Authentication') do
|
||||
OmniAuth::Form.build(:title => 'Google Apps Authentication') do
|
||||
label_field('Google Apps Domain', 'domain')
|
||||
input_field('url', 'domain')
|
||||
end.to_response
|
||||
|
|
|
@ -75,7 +75,7 @@ module OmniAuth
|
|||
end
|
||||
|
||||
def get_identifier
|
||||
OmniAuth::Form.build('OpenID Authentication') do
|
||||
OmniAuth::Form.build(:title => 'OpenID Authentication') do
|
||||
label_field('OpenID Identifier', IDENTIFIER_URL_PARAMETER)
|
||||
input_field('url', IDENTIFIER_URL_PARAMETER)
|
||||
end.to_response
|
||||
|
|
Loading…
Reference in a new issue