1
0
Fork 0
mirror of https://github.com/omniauth/omniauth.git synced 2022-11-09 12:31:49 -05:00

moved Campfire and Basecamp from HTTP Basic to OAuth2

This commit is contained in:
James A. Rosen 2010-06-16 15:34:02 -04:00
parent b980b88ffc
commit 95e0e94f52
8 changed files with 140 additions and 104 deletions

View file

@ -3,8 +3,6 @@ require 'omniauth/core'
module OmniAuth
module Strategies
autoload :HttpBasic, 'omniauth/strategies/http_basic'
autoload :Basecamp, 'omniauth/strategies/basecamp'
autoload :Campfire, 'omniauth/strategies/campfire'
# autoload :Gowalla, 'omniauth/strategies/gowalla'
end
end

View file

@ -1,55 +0,0 @@
require 'omniauth/basic'
require 'nokogiri'
module OmniAuth
module Strategies
class Basecamp < HttpBasic
def initialize(app)
super(app, :basecamp, nil)
end
def endpoint
"http://#{request.params['user']}:#{request.params['password']}@#{request.params['subdomain']}.basecamphq.com/me.xml"
end
def perform_authentication(endpoint)
super(endpoint) rescue super(endpoint.sub('http','https'))
end
def auth_hash
doc = Nokogiri::XML.parse(@response.body)
OmniAuth::Utils.deep_merge(super, {
'uid' => doc.xpath('person/id').text,
'user_info' => user_info(doc),
'credentials' => {
'token' => doc.xpath('person/token').text
}
})
end
def user_info(doc)
hash = {
'nickname' => request.params['user'],
'first_name' => doc.xpath('person/first-name').text,
'last_name' => doc.xpath('person/last-name').text,
'email' => doc.xpath('person/email-address').text,
'image' => doc.xpath('person/avatar-url').text
}
hash['name'] = [hash['first_name'], hash['last_name']].join(' ').strip
hash.delete('image') if hash['image'].include?('missing/avatar.png')
hash
end
def get_credentials
OmniAuth::Form.build('Basecamp Authentication') do
text_field 'Subdomain', 'subdomain'
text_field 'Username', 'user'
password_field 'Password', 'password'
end.to_response
end
end
end
end

View file

@ -1,47 +0,0 @@
require 'omniauth/basic'
require 'multi_json'
module OmniAuth
module Strategies
class Campfire < HttpBasic
def initialize(app)
super(app, :campfire, nil)
end
def endpoint
"http://#{request.params['user']}:#{request.params['password']}@#{request.params['subdomain']}.campfirenow.com/users/me.json"
end
def perform_authentication(endpoint)
super(endpoint) rescue super(endpoint.sub('http','https'))
end
def auth_hash
user_hash = MultiJson.decode(@response.body)['user']
OmniAuth::Utils.deep_merge(super, {
'uid' => user_hash['id'],
'user_info' => user_info(user_hash),
'credentials' => {
'token' => user_hash['api_auth_token']
}
})
end
def user_info(hash)
{
'nickname' => request.params['user'],
'name' => hash['name'],
'email' => hash['email_address']
}
end
def get_credentials
OmniAuth::Form.build('Campfire Authentication') do
text_field 'Subdomain', 'subdomain'
text_field 'Username', 'user'
password_field 'Password', 'password'
end.to_response
end
end
end
end

View file

@ -9,5 +9,7 @@ module OmniAuth
autoload :LinkedIn, 'omniauth/strategies/linked_in'
autoload :Facebook, 'omniauth/strategies/facebook'
autoload :GitHub, 'omniauth/strategies/github'
autoload :Basecamp, 'omniauth/strategies/basecamp'
autoload :Campfire, 'omniauth/strategies/campfire'
end
end

View file

@ -0,0 +1,73 @@
require 'omniauth/oauth'
require 'nokogiri'
module OmniAuth
module Strategies
#
# Authenticate to Basecamp utilizing OAuth 2.0 and retrieve
# basic user information.
#
# Usage:
#
# use OmniAuth::Strategies::Basecamp, 'app_id', 'app_secret'
class Basecamp < OAuth2
BASECAMP_SUBDOMAIN_PARAMETER = 'subdomain'
def initialize(app, app_id, app_secret, options = {})
super(app, :campfire, app_id, app_secret, options)
end
protected
def request_phase
if env['REQUEST_METHOD'] == 'GET'
ask_for_basecamp_subdomain
else
super(options.merge(:site => basecamp_url))
end
end
def user_data
@data ||= MultiJson.decode(@access_token.get('/users/me.xml'))
end
def ask_for_basecamp_subdomain
OmniAuth::Form.build(title) do
text_field 'Basecamp Subdomain', BASECAMP_SUBDOMAIN_PARAMETER
end.to_response
end
def campfire_url
subdomain = request.params[BASECAMP_SUBDOMAIN_PARAMETER]
'http://#{subdomain}.basecamphq.com'
end
def auth_hash
doc = Nokogiri::XML.parse(@response.body)
OmniAuth::Utils.deep_merge(super, {
'uid' => doc.xpath('person/id').text,
'user_info' => user_info(doc),
'credentials' => {
'token' => doc.xpath('person/token').text
}
})
end
def user_info(hash)
hash = {
'first_name' => doc.xpath('person/first-name').text,
'last_name' => doc.xpath('person/last-name').text,
'email' => doc.xpath('person/email-address').text,
'image' => doc.xpath('person/avatar-url').text
}
hash['name'] = [hash['first_name'], hash['last_name']].join(' ').strip
hash.delete('image') if hash['image'].include?('missing/avatar.png')
hash
end
end
end
end

View file

@ -0,0 +1,65 @@
require 'omniauth/oauth'
require 'multi_json'
module OmniAuth
module Strategies
#
# Authenticate to Campfire utilizing OAuth 2.0 and retrieve
# basic user information.
#
# Usage:
#
# use OmniAuth::Strategies::Campfire, 'app_id', 'app_secret'
class Campfire < OAuth2
CAMPFIRE_SUBDOMAIN_PARAMETER = 'subdomain'
def initialize(app, app_id, app_secret, options = {})
super(app, :campfire, app_id, app_secret, options)
end
protected
def request_phase
if env['REQUEST_METHOD'] == 'GET'
ask_for_campfire_subdomain
else
super(options.merge(:site => campfire_url))
end
end
def user_data
@data ||= MultiJson.decode(@access_token.get('/users/me.json'))
end
def ask_for_campfire_subdomain
OmniAuth::Form.build(title) do
text_field 'Campfire Subdomain', CAMPFIRE_SUBDOMAIN_PARAMETER
end.to_response
end
def campfire_url
subdomain = request.params[CAMPFIRE_SUBDOMAIN_PARAMETER]
'http://#{subdomain}.campfirenow.com'
end
def auth_hash
user_hash = MultiJson.decode(@response.body)['user']
OmniAuth::Utils.deep_merge(super, {
'uid' => user_hash['id'],
'user_info' => user_info(user_hash),
'credentials' => {
'token' => user_hash['api_auth_token']
}
})
end
def user_info(hash)
{
'name' => hash['name'],
'email' => hash['email_address']
}
end
end
end
end