Skip to content

Loading…

New oa-more strategy for frype.com and draugiem.lv API #339

Merged
merged 5 commits into from

2 participants

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 16, 2011
  1. @gacha

    Added Draugiem strategy

    gacha committed
  2. @gacha

    added Basic usage

    gacha committed
  3. @gacha

    extracted method

    gacha committed
Commits on May 17, 2011
  1. @gacha
  2. @gacha

    updated hash valus

    gacha committed
This page is out of date. Refresh to see the latest.
View
1 oa-more/lib/omniauth/more.rb
@@ -6,5 +6,6 @@ module Strategies
autoload :Flickr, 'omniauth/strategies/flickr'
autoload :Yupoo, 'omniauth/strategies/yupoo'
autoload :Ign, 'omniauth/strategies/ign'
+ autoload :Draugiem, 'omniauth/strategies/draugiem'
end
end
View
104 oa-more/lib/omniauth/strategies/draugiem.rb
@@ -0,0 +1,104 @@
+require 'omniauth/core'
+require 'digest/md5'
+require 'rest-client'
+require 'multi_json'
+
+module OmniAuth
+ module Strategies
+ #
+ # Authenticate to draugiem.lv and frype.com and others.
+ #
+ # @example Basic Rails Usage
+ #
+ # Add this to config/initializers/omniauth.rb
+ #
+ # Rails.application.config.middleware.use OmniAuth::Builder do
+ # provider :draugiem, 'App id', 'API Key'
+ # end
+ #
+ # @example Basic Rack example
+ #
+ # use Rack::Session::Cookie
+ # use OmniAuth::Strategies::Draugiem, 'App id', 'API Key'
+ #
+ class Draugiem
+ include OmniAuth::Strategy
+ attr_accessor :app_id, :api_key
+
+ def initialize(app, app_id, api_key)
+ super(app, :draugiem)
+ @app_id = app_id
+ @api_key = api_key
+ end
+
+ protected
+
+ def request_phase
+ params = {
+ :app => @app_id,
+ :redirect => callback_url,
+ :hash => Digest::MD5.hexdigest("#{@api_key}#{callback_url}")
+ }
+ query_string = params.collect{ |key,value| "#{key}=#{Rack::Utils.escape(value)}" }.join('&')
+ redirect "http://api.draugiem.lv/authorize/?#{query_string}"
+ end
+
+ def callback_phase
+ if request.params['dr_auth_status'] == 'ok' && request.params['dr_auth_code']
+ response = RestClient.get('http://api.draugiem.lv/json/', { :params => draugiem_authorize_params(request.params['dr_auth_code']) })
+ auth = MultiJson.decode(response.to_s)
+ unless auth['error']
+ @auth_data = auth
+ super
+ else
+ fail!(auth['error']['code'].to_s,auth["error"]["description"].to_s)
+ end
+ else
+ fail!(:invalid_request)
+ end
+ rescue Exception => e
+ fail!(:invalid_response, e)
+ end
+
+ def auth_hash
+ OmniAuth::Utils.deep_merge(super, {
+ 'uid' => @auth_data['uid'],
+ 'user_info' => get_user_info,
+ 'credentials' => {
+ 'apikey' => @auth_data['apikey']
+ },
+ 'extra' => { 'user_hash' => @auth_data }
+ })
+ end
+
+ private
+
+ def get_user_info
+ if @auth_data['users'] && @auth_data['users'][@auth_data['uid']]
+ user = @auth_data['users'][@auth_data['uid']]
+ {
+ 'name' => "#{user['name']} #{user['surname']}",
+ 'nickname' => user['nick'],
+ 'first_name' => user['name'],
+ 'last_name' => user['surname'],
+ 'location' => user['place'],
+ 'age' => user['age'] =~ /^0-9$/ ? user['age'] : nil,
+ 'adult' => user['adult'] == '1' ? true : false,
+ 'image' => user['img'],
+ 'sex' => user['sex']
+ }
+ else
+ {}
+ end
+ end
+
+ def draugiem_authorize_params code
+ {
+ :action => 'authorize',
+ :app => @api_key,
+ :code => code
+ }
+ end
+ end
+ end
+end
View
51 oa-more/spec/omniauth/strategies/draugiem_spec.rb
@@ -0,0 +1,51 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+
+describe 'OmniAuth::Strategies::Draugiem', :type => :strategy do
+
+ include OmniAuth::Test::StrategyTestCase
+
+ def strategy
+ [OmniAuth::Strategies::Draugiem, '123', "abc"]
+ end
+
+ it 'should initialize with api key and app id' do
+ lambda{OmniAuth::Strategies::Draugiem.new({},'123','abc')}.should_not raise_error
+ end
+
+ describe '/auth/draugiem' do
+
+ it 'should redirect to api.draugiem.lv' do
+ get '/auth/draugiem'
+ last_response.should be_redirect
+ last_response.headers['Location'].should == "http://api.draugiem.lv/authorize/?app=123&redirect=http%3A%2F%2Fexample.org%2Fauth%2Fdraugiem%2Fcallback&hash=2d5698ba02ddd42c441998a5a2c2946b"
+ end
+
+ it 'should gather user data after success authorization' do
+ stub_request(:get, "http://api.draugiem.lv/json/?action=authorize&app=abc&code=123456").
+ to_return(:body => MultiJson.encode({
+ 'apikey'=>"123456789",
+ 'uid'=>"100",
+ 'language'=>"lv",
+ 'users'=>{
+ '100'=>{
+ 'uid'=>"100",
+ 'name'=>"John",
+ 'surname'=>"Lenon",
+ 'nick'=>"johnybravo",
+ 'place'=>"Durbe",
+ 'age'=>"false",
+ 'adult'=>"1",
+ 'img'=>"http://4.bp.blogspot.com/_ZmXOoYjxXog/Sg2jby1RFSI/AAAAAAAAE_Q/1LpfjimAz50/s400/JohnnyBravo3.gif",
+ 'sex'=>"M"
+ }
+ }
+ }))
+ get '/auth/draugiem/callback?dr_auth_status=ok&dr_auth_code=123456'
+
+ last_request.env['omniauth.auth']['credentials']['apikey'].should == "123456789"
+ last_request.env['omniauth.auth']['user_info']['location'].should == "Durbe"
+ last_request.env['omniauth.auth']['user_info']['age'].should be_nil
+ last_request.env['omniauth.auth']['user_info']['adult'].should be_true
+ end
+ end
+end
View
1 oa-more/spec/spec_helper.rb
@@ -3,6 +3,7 @@
require 'rspec'
require 'rack/test'
require 'webmock/rspec'
+$:<<File.join(File.dirname(__FILE__),'../lib/')
require 'omniauth/more'
Rspec.configure do |config|
Something went wrong with that request. Please try again.