From fc735077d2d5b5dc6f08c7faddb4dd9d446cea30 Mon Sep 17 00:00:00 2001 From: Paul Chilton Date: Thu, 28 Oct 2010 21:43:49 +1300 Subject: [PATCH] SmugMug provider as OAuth strategy --- oa-oauth/lib/omniauth/oauth.rb | 1 + oa-oauth/lib/omniauth/strategies/smug_mug.rb | 39 +++++++++++++++++++ .../spec/omniauth/strategies/smug_mug_spec.rb | 13 +++++++ 3 files changed, 53 insertions(+) create mode 100644 oa-oauth/lib/omniauth/strategies/smug_mug.rb create mode 100644 oa-oauth/spec/omniauth/strategies/smug_mug_spec.rb diff --git a/oa-oauth/lib/omniauth/oauth.rb b/oa-oauth/lib/omniauth/oauth.rb index ed4438b..6afc16b 100644 --- a/oa-oauth/lib/omniauth/oauth.rb +++ b/oa-oauth/lib/omniauth/oauth.rb @@ -17,5 +17,6 @@ module OmniAuth autoload :Dopplr, 'omniauth/strategies/dopplr' autoload :Meetup, 'omniauth/strategies/meetup' autoload :SoundCloud, 'omniauth/strategies/sound_cloud' + autoload :SmugMug, 'omniauth/strategies/smug_mug' end end diff --git a/oa-oauth/lib/omniauth/strategies/smug_mug.rb b/oa-oauth/lib/omniauth/strategies/smug_mug.rb new file mode 100644 index 0000000..de3fec1 --- /dev/null +++ b/oa-oauth/lib/omniauth/strategies/smug_mug.rb @@ -0,0 +1,39 @@ +require 'omniauth/oauth' +require 'multi_json' + +module OmniAuth + module Strategies + # + # Authenticate to SmugMug via OAuth and retrieve basic user information. + # Usage: + # use OmniAuth::Strategies::SmugMug, 'consumerkey', 'consumersecret' + # + class SmugMug < OmniAuth::Strategies::OAuth + def initialize(app, consumer_key, consumer_secret) + super(app, :smugmug, consumer_key, consumer_secret, + :site => 'http://api.smugmug.com', + :request_token_path => "/services/oauth/getRequestToken.mg", + :access_token_path => "/services/oauth/getAccessToken.mg", + :authorize_path => "/services/oauth/authorize.mg") + end + + def auth_hash + OmniAuth::Utils.deep_merge(super, { + 'uid' => user_hash['id'], + 'user_info' => user_info, + 'extra' => { 'user_hash' => user_hash } + }) + end + + # user info according to schema + def user_info + { 'nickname' => user_hash['NickName'] } + end + + # info as supplied by SmugMug + def user_hash + @user_hash ||= MultiJson.decode(@access_token.get('/services/api/json/1.2.2/?method=smugmug.auth.checkAccessToken').body)['Auth']['User'] + end + end + end +end diff --git a/oa-oauth/spec/omniauth/strategies/smug_mug_spec.rb b/oa-oauth/spec/omniauth/strategies/smug_mug_spec.rb new file mode 100644 index 0000000..2dad427 --- /dev/null +++ b/oa-oauth/spec/omniauth/strategies/smug_mug_spec.rb @@ -0,0 +1,13 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') + +describe OmniAuth::Strategies::SmugMug do + + it 'should subclass OAuth' do + OmniAuth::Strategies::SmugMug.should < OmniAuth::Strategies::OAuth + end + + it 'should initialize with just consumer key and secret' do + lambda{OmniAuth::Strategies::SmugMug.new({},'abc','def')}.should_not raise_error + end + +end \ No newline at end of file