Skip to content

Commit

Permalink
Merge branch 'master' of github.com:intridea/omniauth
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed May 10, 2011
2 parents 7b2f240 + ebe28f3 commit 261bc83
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 3 deletions.
8 changes: 5 additions & 3 deletions oa-core/lib/omniauth/form.rb
Expand Up @@ -90,13 +90,14 @@ class Form

def initialize(options = {})
options[:title] ||= "Authentication Info Required"
options[:header_info] ||= ""
self.options = options

@html = ""
header(options[:title])
header(options[:title],options[:header_info])
end

def self.build(title=nil, &block)
def self.build(title=nil,&block)
form = OmniAuth::Form.new(title)
form.instance_eval(&block)
end
Expand Down Expand Up @@ -138,13 +139,14 @@ def fieldset(legend, options = {}, &block)
self
end

def header(title)
def header(title,header_info)
@html << <<-HTML
<!DOCTYPE html>
<html>
<head>
<title>#{title}</title>
#{css}
#{header_info}
</head>
<body>
<h1>#{title}</h1>
Expand Down
1 change: 1 addition & 0 deletions oa-more/lib/omniauth/more.rb
Expand Up @@ -5,5 +5,6 @@ module Strategies
autoload :WindowsLive, 'omniauth/strategies/windows_live'
autoload :Flickr, 'omniauth/strategies/flickr'
autoload :Yupoo, 'omniauth/strategies/yupoo'
autoload :Ign, 'omniauth/strategies/ign'
end
end
93 changes: 93 additions & 0 deletions oa-more/lib/omniauth/strategies/ign.rb
@@ -0,0 +1,93 @@
require 'omniauth/core'
require 'openssl'

module OmniAuth
module Strategies
class Ign
include OmniAuth::Strategy
IDENTIFIER_URL_PARAMETER = ""

class CallbackError < StandardError
attr_accessor :error, :error_reason
def initialize(error, error_reason)
self.error = error
self.error_reason = error_reason
end
end

def initialize(app, api_key, hostname=nil, options = {})
options[:name] ||= "ign"
super(app, :ign)
@api_key = api_key
@hostname = hostname
end

protected

def request_phase
OmniAuth::Form.build(:title => 'IGN Authentication', :header_info=>js) do
label_field('Identifying you with the IGN server', IDENTIFIER_URL_PARAMETER)
end.to_response
end

def callback_phase
signature = OpenSSL::HMAC.hexdigest('sha1', @api_key, ("#{request.params["username"]}::#{request.params["timestamp"]}"))

raise CallbackError.new("Invalid Signature","The supplied and calculated signature did not match, user not approved.") if signature != request.params["signature"]

super
rescue CallbackError => e
fail!(:invalid_response, e)
end

def auth_hash
OmniAuth::Utils.deep_merge(super, {
'uid' => "ign-" + request.params["username"],
'credentials' => { 'token' => request.params["signature"] },
'user_info' => user_info,
'extra' => { 'user_hash' => request.params }
})
end

def user_info
{
'nickname' => request.params["username"],
}
end

def js
@js = <<-JS
$(document).ready(function() {
$.ajax({
url: "http://#{@hostname}/users/current.json?callback=z33k",
type: "get",
dataType:"jsonp",
success: function(data) {
if(typeof data.error == 'undefined'){
// There is a current My IGN user
var username = data.my_ign_username;
var signature = data.signature;
var timestamp = data.timestamp;
window.location = "/auth/ign/callback?username=" +username+"&signature="+signature+"&timestamp=" + timestamp;
}
else{
nouser();
}
}
});
return false;
});
function nouser() {
var url = "http://my.ign.com/login?r="+window.location;
top.location = url;
window.location = url;
}
JS
"\n<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js' type='text/javascript'></script>" +
"\n<script type='text/javascript'>#{@js}</script>" +
"\n<style type='text/css'>button {visibility:hidden;}</style>"
end

end
end
end
1 change: 1 addition & 0 deletions oa-openid/lib/omniauth/openid.rb
Expand Up @@ -55,5 +55,6 @@ module OpenID; end
module Strategies
autoload :OpenID, 'omniauth/strategies/open_id'
autoload :GoogleApps, 'omniauth/strategies/google_apps'
autoload :Steam, 'omniauth/strategies/steam'
end
end
55 changes: 55 additions & 0 deletions oa-openid/lib/omniauth/strategies/steam.rb
@@ -0,0 +1,55 @@
require 'omniauth/openid'
module OmniAuth
module Strategies
class Steam < OmniAuth::Strategies::OpenID
def initialize(app, store = nil, api_key = nil, options = {}, &block)
options[:identifier] ||= "http://steamcommunity.com/openid"
options[:name] ||= 'steam'
@api_key = api_key
super(app, store, options, &block)
end

def user_info(response=nil)
player = user_hash['response']['players']['player'].first
nickname = player["personaname"]
name = player["realname"]
url = player["profileurl"]
country = player["loccountrycode"]
state = player["locstatecode"]
city = player["loccityid"]

{
'nickname' => nickname,
'name' => name,
'url' => url,
'location' => "#{city}, #{state}, #{country}"
}
end

def user_hash
# Steam provides no information back on a openid response other than a 64bit user id
# Need to use this information and make a API call to get user information from steam.
if @api_key
unless @user_hash
uri = URI.parse("http://api.steampowered.com/")
req = Net::HTTP::Get.new("#{uri.path}ISteamUser/GetPlayerSummaries/v0001/?key=#{@api_key}&steamids=#{@openid_response.display_identifier.split("/").last}")
res = Net::HTTP.start(uri.host, uri.port) {|http|
http.request(req)
}
end
@user_hash ||= MultiJson.decode(res.body)
else
{}
end
end

def auth_hash
OmniAuth::Utils.deep_merge(super, {
'uid' => @openid_response.display_identifier.split("/").last,
'user_info' => user_info,
'extra' => {'user_hash' => user_hash}
})
end
end
end
end

0 comments on commit 261bc83

Please sign in to comment.