Skip to content

Commit

Permalink
Added Draugiem strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
gacha committed May 16, 2011
1 parent f217073 commit 3da4ed8
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
1 change: 1 addition & 0 deletions oa-more/lib/omniauth/more.rb
Expand Up @@ -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
85 changes: 85 additions & 0 deletions oa-more/lib/omniauth/strategies/draugiem.rb
@@ -0,0 +1,85 @@
require 'omniauth/core'
require 'digest/md5'
require 'rest-client'
require 'multi_json'

module OmniAuth
module Strategies
class Draugiem
include OmniAuth::Strategy
attr_accessor :api_key, :app_id, :options

def initialize(app, api_key, app_id)
super(app, :draugiem)
@api_key = api_key
@app_id = app_id
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
user_info = if @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'],
'adult' => user['adult'],
'image' => user['img'],
'sex' => user['sex']
}
else
{}
end
OmniAuth::Utils.deep_merge(super, {
'uid' => @auth_data['uid'],
'user_info' => user_info,
'credentials' => {
'apikey' => @auth_data['apikey']
},
'extra' => { 'user_hash' => @auth_data }
})
end

private

def draugiem_authorize_params code
{
:action => 'authorize',
:app => @api_key,
:code => code
}
end
end
end
end
50 changes: 50 additions & 0 deletions oa-more/spec/omniauth/strategies/draugiem_spec.rb
@@ -0,0 +1,50 @@
require File.expand_path('../../../spec_helper', __FILE__)

describe 'OmniAuth::Strategies::Draugiem', :type => :strategy do

include OmniAuth::Test::StrategyTestCase

def strategy
[OmniAuth::Strategies::Draugiem, "abc",123]
end

it 'should initialize with api key and app id' do
lambda{OmniAuth::Strategies::Draugiem.new({},'abc','123')}.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'
debugger

last_request.env['omniauth.auth']['credentials']['apikey'].should == "123456789"
last_request.env['omniauth.auth']['user_info']['location'].should == "Durbe"
end
end
end
1 change: 1 addition & 0 deletions oa-more/spec/spec_helper.rb
Expand Up @@ -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|
Expand Down

0 comments on commit 3da4ed8

Please sign in to comment.