Skip to content

Instantly share code, notes, and snippets.

@vshvedov
Created October 27, 2011 14:09
Show Gist options
  • Save vshvedov/1319641 to your computer and use it in GitHub Desktop.
Save vshvedov/1319641 to your computer and use it in GitHub Desktop.
ID.NET OmniAuth Strategy
require 'omniauth/oauth'
require 'multi_json'
module OmniAuth
module Strategies
class IdNet < OmniAuth::Strategies::OAuth2
def initialize(app, api_key = nil, secret_key = nil, options = {}, &block)
client_options = {
:site => IDNET_PROVIDER_URL,
:authorize_url => "#{IDNET_PROVIDER_URL}/oauth/authorize",
:access_token_url => "#{IDNET_PROVIDER_URL}/oauth/token"
}
super(app, :id_net, api_key, secret_key, client_options, &block)
end
protected
def user_data
@data ||= MultiJson.decode(@access_token.get("/api/profile").body)
end
def request_phase
options[:response_type] ||= 'code'
super
end
def callback_phase
options[:grant_type] ||= 'authorization_code'
super
end
def user_hash
user_data
end
def build_access_token
access_token = super
access_token.options[:header_format] = "OAuth %s"
access_token
end
def auth_hash
user_data
OmniAuth::Utils.deep_merge(super, {
'pid' => user_data['pid'],
'first_name' => user_data['first_name'],
'last_name' => user_data['last_name'],
'email' => user_data['email'],
'language' => user_data['language'],
'nickname' => user_data['nickname'],
'gender' => user_data['gender'],
'street_address' => user_data['street_address'],
'city' => user_data['city'],
'country' => user_data['country'],
'state_or_province' => user_data['state_or_province'],
'zip' => user_data['zip']
})
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment