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
|
end
|
||||||
|
|
||||||
def get_credentials
|
def get_credentials
|
||||||
OmniAuth::Form.build(title) do
|
OmniAuth::Form.build(:title => title) do
|
||||||
text_field 'Username', 'username'
|
text_field 'Username', 'username'
|
||||||
password_field 'Password', 'password'
|
password_field 'Password', 'password'
|
||||||
end.to_response
|
end.to_response
|
||||||
|
|
|
@ -72,12 +72,28 @@ module OmniAuth
|
||||||
display: block;
|
display: block;
|
||||||
margin: 20px auto 0;
|
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
|
CSS
|
||||||
|
|
||||||
def initialize(title=nil)
|
attr_accessor :options
|
||||||
title ||= "Authentication Info Required"
|
|
||||||
|
def initialize(options = {})
|
||||||
|
options[:title] ||= "Authentication Info Required"
|
||||||
|
self.options = options
|
||||||
|
|
||||||
@html = ""
|
@html = ""
|
||||||
header(title)
|
header(options[:title])
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.build(title=nil, &block)
|
def self.build(title=nil, &block)
|
||||||
|
@ -106,6 +122,21 @@ module OmniAuth
|
||||||
input_field('password', name)
|
input_field('password', name)
|
||||||
self
|
self
|
||||||
end
|
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)
|
def header(title)
|
||||||
@html << <<-HTML
|
@html << <<-HTML
|
||||||
|
@ -117,7 +148,7 @@ module OmniAuth
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>#{title}</h1>
|
<h1>#{title}</h1>
|
||||||
<form method='post' noValidate='noValidate'>
|
<form method='post' #{"action='#{options[:url]}' " if options[:url]}noValidate='noValidate'>
|
||||||
HTML
|
HTML
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
|
@ -45,7 +45,7 @@ module OmniAuth
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_credentials
|
def get_credentials
|
||||||
OmniAuth::Form.build(options[:title] || "LDAP Authentication") do
|
OmniAuth::Form.build(:title => (options[:title] || "LDAP Authentication")) do
|
||||||
text_field 'Login', 'username'
|
text_field 'Login', 'username'
|
||||||
password_field 'Password', 'password'
|
password_field 'Password', 'password'
|
||||||
end.to_response
|
end.to_response
|
||||||
|
|
|
@ -4,7 +4,7 @@ Bundler.setup
|
||||||
require 'rake'
|
require 'rake'
|
||||||
|
|
||||||
require 'mg'
|
require 'mg'
|
||||||
MG.new('oa-more.gemspec')
|
MG.new('oa-identity.gemspec')
|
||||||
|
|
||||||
require 'rspec/core/rake_task'
|
require 'rspec/core/rake_task'
|
||||||
RSpec::Core::RakeTask.new(:spec) do |s|
|
RSpec::Core::RakeTask.new(:spec) do |s|
|
||||||
|
|
|
@ -5,18 +5,38 @@ module OmniAuth
|
||||||
class Identity
|
class Identity
|
||||||
include OmniAuth::Strategy
|
include OmniAuth::Strategy
|
||||||
|
|
||||||
def initialize(app, options = {})
|
def initialize(app, options = {}, &block)
|
||||||
super(app, :identity, options)
|
options[:key] ||= 'nickname'
|
||||||
|
super(app, :identity, options, &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def request_phase
|
def request_phase
|
||||||
OmniAuth::Form.build(options[:title] || 'Identify Yourself') do
|
OmniAuth::Form.build(:title => (self.options[:title] || 'Identify Yourself'), :url => callback_path) do
|
||||||
text_field 'Screen Name', 'nickname'
|
text_field 'Login', 'key'
|
||||||
password_field 'Password', 'password'
|
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.to_response
|
||||||
end
|
end
|
||||||
|
|
||||||
def callback_phase
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,16 +1,59 @@
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
|
module OmniAuth
|
||||||
|
module Identity
|
||||||
|
class Model
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe OmniAuth::Strategies::Identity do
|
describe OmniAuth::Strategies::Identity do
|
||||||
def app
|
def app
|
||||||
Rack::Builder.new do
|
b = Rack::Builder.new
|
||||||
use Rack::Session::Cookie
|
b.use Rack::Session::Cookie
|
||||||
use OmniAuth::Strategies::Identity, @options
|
b.use OmniAuth::Strategies::Identity, @options
|
||||||
run @app || lambda{|env| [200, {}, env.inspect]}
|
b.run @rack_app || lambda{|env| [@status || 404, {'env' => env}, ["Boing"]]}
|
||||||
end.to_app
|
b.to_app
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should display a form' do
|
before(:each) do
|
||||||
get '/auth/identity'
|
OmniAuth.config.on_failure = lambda{|env| [500, {}, [env['omniauth.error.type'].to_s]]}
|
||||||
last_response.body.should be_include("<form")
|
@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
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
require 'rubygems'
|
require 'rubygems'
|
||||||
|
require 'bundler'
|
||||||
|
Bundler.setup
|
||||||
|
|
||||||
require 'rspec'
|
require 'rspec'
|
||||||
require 'rspec/autorun'
|
require 'rspec/autorun'
|
||||||
require 'rack/test'
|
require 'rack/test'
|
||||||
|
|
|
@ -9,7 +9,7 @@ module OmniAuth
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_identifier
|
def get_identifier
|
||||||
OmniAuth::Form.build('Google Apps Authentication') do
|
OmniAuth::Form.build(:title => 'Google Apps Authentication') do
|
||||||
label_field('Google Apps Domain', 'domain')
|
label_field('Google Apps Domain', 'domain')
|
||||||
input_field('url', 'domain')
|
input_field('url', 'domain')
|
||||||
end.to_response
|
end.to_response
|
||||||
|
|
|
@ -75,7 +75,7 @@ module OmniAuth
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_identifier
|
def get_identifier
|
||||||
OmniAuth::Form.build('OpenID Authentication') do
|
OmniAuth::Form.build(:title => 'OpenID Authentication') do
|
||||||
label_field('OpenID Identifier', IDENTIFIER_URL_PARAMETER)
|
label_field('OpenID Identifier', IDENTIFIER_URL_PARAMETER)
|
||||||
input_field('url', IDENTIFIER_URL_PARAMETER)
|
input_field('url', IDENTIFIER_URL_PARAMETER)
|
||||||
end.to_response
|
end.to_response
|
||||||
|
|
Loading…
Reference in a new issue