forked from omniauth/omniauth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters