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.
Merge branch 'master' of github.com:intridea/omniauth
- Loading branch information
Showing
5 changed files
with
155 additions
and
3 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
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,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+"×tamp=" + 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 |
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,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 |