Permalink
Browse files

nk.pl OAuth2 Strategy for OmniAuth 1.0

  • Loading branch information...
arekk committed May 25, 2012
1 parent d48722d commit e9eabe5b91ad66287f035d7a1e895a6e9758238c
Showing with 134 additions and 0 deletions.
  1. +4 −0 Gemfile
  2. +13 −0 LICENSE
  3. +7 −0 Rakefile
  4. +2 −0 lib/omniauth-nk.rb
  5. +5 −0 lib/omniauth-nk/version.rb
  6. +78 −0 lib/omniauth/strategies/nk.rb
  7. +25 −0 omniauth-nk.gemspec
View
@@ -0,0 +1,4 @@
source "https://rubygems.org"
gemspec
View
13 LICENSE
@@ -0,0 +1,13 @@
Copyright 2012 Nasza Klasa Spółka z ograniczoną odpowiedzialnością
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
View
@@ -0,0 +1,7 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
task :default => :spec
View
@@ -0,0 +1,2 @@
require "omniauth-nk/version"
require 'omniauth/strategies/nk'
@@ -0,0 +1,5 @@
module Omniauth
module Nk
VERSION = "1.0"
end
end
@@ -0,0 +1,78 @@
require 'omniauth-oauth2'
require 'multi_json'
require 'oauth'
module OmniAuth
module Strategies
class Nk < OmniAuth::Strategies::OAuth2
DEFAULT_RESPONSE_TYPE = 'code'
DEFAULT_GRANT = 'authorization_code'
DEFAULT_SCOPE = 'BASIC_PROFILE_ROLE,EMAIL_PROFILE_ROLE'
option :name, "nk"
option :client_options, {
:site => 'https://nk.pl',
:authorize_url => '/oauth2/login',
:token_url => '/oauth2/token'
}
def callback_phase
if request.params['error'] || request.params['error_description']
fail!(request.params['error'], CallbackError.new(request.params['error'], request.params['error_description'], request.params['error_uri']))
end
super
end
def authorize_params
super.tap do |params|
params[:scope] ||= DEFAULT_SCOPE
params[:response_type] ||= DEFAULT_RESPONSE_TYPE
params[:client_id] = client.id
end
end
def token_params
super.tap do |params|
params[:scope] ||= DEFAULT_SCOPE
params[:grant_type] ||= DEFAULT_GRANT
params[:client_id] = client.id
params[:client_secret] = client.secret
end
end
uid do
raw_info['entry']['id']
end
info do
row = raw_info['entry']
{
:name => row['name']['formatted'],
:email => row['emails'].instance_of?(Array) ? row['emails'].last['value'] : nil,
:age => row['age'].to_i,
:gender => row['gender'],
:location => row['currentLocation']['region'],
:image => row['thumbnailUrl'],
}
end
extra do
{ :raw_info => raw_info }
end
def raw_info
if @raw_info.nil?
# OAuth is used to get user data
fields = %w(id name emails age gender currentLocation thumbnailUrl)
request_url = "http://opensocial.nk-net.pl/v09/social/rest/people/@me?fields=#{fields.join(',')}&nk_token=#{credentials['token']}"
consumer = OAuth::Consumer.new(options.client_id, options.client_secret, {:site => 'http://opensocial.nk-net.pl'})
@raw_info = MultiJson.decode(OAuth::AccessToken.new(consumer, credentials['token']).get(request_url).body.to_s)
end
@raw_info
end
end
end
end
View
@@ -0,0 +1,25 @@
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/omniauth-nk/version', __FILE__)
Gem::Specification.new do |gem|
gem.name = "omniauth-nk"
gem.version = Omniauth::Nk::VERSION
gem.authors = ["Arkadiusz Kuryłowicz, Nasza Klasa Spółka z ograniczoną odpowiedzialnością"]
gem.email = ["arkadiusz.kurylowicz@nasza-klasa.pl"]
gem.description = %q{OmniAuth Strategy for nk.pl using OAuth2}
gem.summary = %q{OmniAuth Strategy for nk.pl using OAuth2}
gem.homepage = "https://github.com/naszaklasa/omniauth-nk"
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
gem.files = `git ls-files`.split("\n")
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
gem.require_paths = ["lib"]
gem.add_dependency 'omniauth', '~> 1.0'
gem.add_dependency 'omniauth-oauth2', '~> 1.0'
gem.add_dependency 'multi_json', '~> 1.0'
gem.add_dependency 'oauth', '~> 0.4.6'
gem.add_development_dependency 'rake'
gem.add_development_dependency 'rspec', '~> 2.8'
end

0 comments on commit e9eabe5

Please sign in to comment.