devise_loginza 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in devise_loginza.gemspec
4
+ gemspec
data/README.markdown ADDED
@@ -0,0 +1,23 @@
1
+ Devise << Loginze
2
+ =================
3
+
4
+ http://loginza.ru/api-overview
5
+
6
+ Dependencies
7
+ ------------
8
+ httparty
9
+ json
10
+ devise
11
+
12
+ Installation
13
+ -----------
14
+ Add to Gemfile
15
+ gem "devise_loginza", :git => "git://github.com/pronix/devise_loginza.git"
16
+
17
+ Usage
18
+ -----
19
+ Frame:
20
+ <%= loginza_frame_tag(loginza_user_session_url, { :providers => [:google, :yandex, :mailru, :vkontakte, :facebook, :twitter] },
21
+ { :style => "width:400px;height:350px;" }) %>
22
+ Button:
23
+ <%= loginza_button_tag("Login via OpenID", loginza_user_session_url, :providers => [ :google, :facebook, :twitter ]) %>
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,10 @@
1
+ class Devise::LoginzaSessionsController < Devise::SessionsController
2
+ def loginza
3
+ if signed_in?(resource_name)
4
+ redirect_to after_sign_in_path_for(resource_name)
5
+ else
6
+ redirect_to root_url
7
+ end
8
+ end
9
+
10
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "devise_loginza/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "devise_loginza"
7
+ s.version = DeviseLoginza::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Maxim"]
10
+ s.email = ["parallel588@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Devise << Loginza}
13
+ s.description = %q{Devise << Loginza}
14
+
15
+ s.rubyforge_project = "devise_loginza"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency "json", ">= 1.5.1"
23
+ s.add_dependency "devise"
24
+ s.add_dependency "httparty", ">= 0.7.4"
25
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+ require 'httparty'
3
+ require 'json'
4
+
5
+ module DeviseLoginza
6
+
7
+ class Loginza
8
+ include HTTParty
9
+
10
+ base_uri 'http://loginza.ru'
11
+
12
+ class << self
13
+
14
+ def auth(token)
15
+ response = get('/api/authinfo', :query => { :token => token})
16
+
17
+ raise ServiceUnavailableError, "The Loginza service is temporarily unavailable. (4XX)" if response.code >= 400
18
+ @data = JSON.parse(response.body)
19
+
20
+ {
21
+ :auth_state => (@data.keys.join =~ /error/ ? :error : :ok),
22
+ :data => @data
23
+ }
24
+ rescue Exception => e
25
+ ::Rails.logger.info("Error parse: #{response.body}")
26
+ ::Rails.logger.error(e)
27
+ return { }
28
+ end
29
+
30
+ end # end class <<self
31
+ end
32
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+ module Devise
3
+ module Models
4
+ module LoginzaAuthenticatable
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ ::Devise::Models.config(self, :loginza_auto_register)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+ ActionDispatch::Routing::Mapper.class_eval do
3
+ protected
4
+
5
+ def devise_loginza(mapping, controllers) #:nodoc:
6
+ resource :session, :only => [], :controller => controllers[:loginza_sessions], :path => "" do
7
+ match :loginza, :path => mapping.path_names[:loginza]
8
+ end
9
+
10
+ end
11
+
12
+ end
@@ -0,0 +1,59 @@
1
+ # encoding: utf-8
2
+ require 'devise/strategies/base'
3
+
4
+ module Devise
5
+ module Strategies
6
+ class LoginzaAuthenticatable < Base
7
+
8
+ def authenticate!
9
+ logger.debug("Authenticating with Loginza for mapping #{mapping.to}")
10
+ if provider_response
11
+ handle_response!
12
+ else
13
+ fail(:invalid_token)
14
+ end
15
+ end
16
+
17
+ protected
18
+
19
+ def handle_response!
20
+ logger.debug "Attempting Loginza auth: #{provider_response.inspect}"
21
+ loginza_result = ::DeviseLoginza::Loginza.auth(provider_response)
22
+
23
+ case loginza_result[:auth_state]
24
+ when :ok
25
+ resource = if Devise.loginza_auto_register && mapping.to.respond_to?(:create_from_loginza)
26
+ mapping.to.send(:create_from_loginza, loginza_result[:data])
27
+ else
28
+ mapping.to.find_by_email(loginza_result[:data]["email"])
29
+ end
30
+ if resource
31
+ success!(resource)
32
+ else
33
+ fail! I18n.t("devise.loginza.user_not_registered")
34
+ end
35
+
36
+ else
37
+ fail! "Loginza: #{loginza_result[:data]}"
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def provider_response?
44
+ !!provider_response
45
+ end
46
+
47
+ def provider_response
48
+ params[:token]
49
+ end
50
+
51
+ def logger
52
+ @logger ||= Rails.logger
53
+ end
54
+
55
+ end
56
+ end
57
+ end
58
+
59
+ Warden::Strategies.add :loginza_authenticatable, Devise::Strategies::LoginzaAuthenticatable
@@ -0,0 +1,4 @@
1
+ # encoding: utf-8
2
+ module DeviseLoginza
3
+ VERSION = "0.0.1"
4
+ end
@@ -0,0 +1,89 @@
1
+ # encoding: utf-8
2
+
3
+ module Devise #:nodoc:
4
+ module DeviseLoginza #:nodoc:
5
+ module Helpers
6
+ def loginza_js
7
+ javascript_include_tag("http://s1.loginza.ru/js/widget.js")
8
+ end
9
+
10
+ # Loginza button
11
+ #
12
+ # Simple example:
13
+ # <%= loginza_button_tag("Login via OpenID", clients_url) %>
14
+ #
15
+ # With set of providers:
16
+ # <%= loginza_button_tag("Login via OpenID", clients_url, :providers => [ :google, :facebook, :twitter ]) %>
17
+ #
18
+ # With default provider:
19
+ # <%= loginza_button_tag("Login via OpenID", clients_url, :provider => :google) %>
20
+ #
21
+ # With a block:
22
+ # <%= loginza_button_tag(clients_url, :provider => :google) do %>
23
+ # <div class='loginza'>
24
+ # ...
25
+ # </div>
26
+ # <% end %>
27
+ #
28
+ # Supported providers:
29
+ # google, yandex, mailruapi, mailru, vkontakte, facebook, twitter, loginza, myopenid, webmoney, rambler, flickr, lastfm, verisign, aol, steam, openid.
30
+ #
31
+ def loginza_button_tag(*args, &block)
32
+ if block_given?
33
+ callback_url = args.first
34
+ options = args.second || {}
35
+ html_options = args.third
36
+
37
+ loginza_button_tag(capture(&block), callback_url, options, html_options)
38
+ else
39
+ name = args[0]
40
+ callback_url = args[1]
41
+ options = args[2] || {}
42
+ html_options = args[3] || {}
43
+
44
+ html_options[:class] ||= "loginza"
45
+ url = generate_url(callback_url, options)
46
+
47
+ html = ""
48
+ html << javascript_include_tag("http://s1.loginza.ru/js/widget.js")
49
+ html << link_to(name, url, options, html_options)
50
+ html
51
+ end
52
+ end
53
+
54
+ # Loginza frame
55
+ #
56
+ # Simple example:
57
+ #
58
+ # <%= loginza_frame_tag(clients_url, { :providers => [:google, :yandex, :mailru] }, { :style => "width:400px;height:350px;" }) %>
59
+ #
60
+ def loginza_frame_tag(callback_url, options = {}, html_options = {})
61
+ html_options = {
62
+ :scrolling => "no",
63
+ :frameborder => "no",
64
+ :style => "width:359px;height:300px;",
65
+ }.merge(html_options)
66
+
67
+ html_options[:src] = generate_url(callback_url, options, { :overlay => 'loginza' })
68
+
69
+ content_tag(:iframe, nil, html_options)
70
+ end
71
+
72
+ private
73
+
74
+ def generate_url(callback_url, options = { }, params = { })
75
+ params[:token_url] = callback_url
76
+ params[:lang] ||= ::I18n.locale.to_s
77
+ @providers = options[:providers] ? options.delete(:providers).map(&:to_s).join(',') : options.delete(:provider)
78
+
79
+ query = ::Rack::Utils.build_query(params)
80
+ query << "&providers_set=#{@providers}" unless @providers.blank?
81
+
82
+ "https://loginza.ru/api/widget?#{query}"
83
+ end
84
+
85
+ end
86
+ end
87
+ end
88
+
89
+ ::ActionView::Base.send :include, Devise::DeviseLoginza::Helpers
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ require 'devise'
3
+
4
+ require 'devise_loginza/loginza'
5
+ require 'devise_loginza/strategy'
6
+ require 'devise_loginza/routes'
7
+ require 'devise_loginza/view_helpers'
8
+
9
+ module DeviseLoginza
10
+ class Engine < Rails::Engine
11
+ end
12
+
13
+ class ServerError < RuntimeError; end
14
+ class ServiceUnavailableError < ServerError; end
15
+ end
16
+
17
+ module Devise
18
+ mattr_accessor :loginza_auto_register
19
+ end
20
+
21
+ Devise.add_module :loginza_authenticatable,
22
+ :strategy => true,
23
+ :model => 'devise_loginza/model',
24
+ :controller => :loginza_sessions,
25
+ :route => :loginza
26
+
27
+
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "devise_loginza"
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: devise_loginza
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Maxim
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-26 00:00:00 +05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: json
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 1
30
+ segments:
31
+ - 1
32
+ - 5
33
+ - 1
34
+ version: 1.5.1
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: devise
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :runtime
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: httparty
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 11
60
+ segments:
61
+ - 0
62
+ - 7
63
+ - 4
64
+ version: 0.7.4
65
+ type: :runtime
66
+ version_requirements: *id003
67
+ description: Devise << Loginza
68
+ email:
69
+ - parallel588@gmail.com
70
+ executables: []
71
+
72
+ extensions: []
73
+
74
+ extra_rdoc_files: []
75
+
76
+ files:
77
+ - .gitignore
78
+ - Gemfile
79
+ - README.markdown
80
+ - Rakefile
81
+ - app/controllers/devise/loginza_sessions_controller.rb
82
+ - devise_loginza.gemspec
83
+ - lib/devise_loginza.rb
84
+ - lib/devise_loginza/loginza.rb
85
+ - lib/devise_loginza/model.rb
86
+ - lib/devise_loginza/routes.rb
87
+ - lib/devise_loginza/strategy.rb
88
+ - lib/devise_loginza/version.rb
89
+ - lib/devise_loginza/view_helpers.rb
90
+ - rails/init.rb
91
+ has_rdoc: true
92
+ homepage: ""
93
+ licenses: []
94
+
95
+ post_install_message:
96
+ rdoc_options: []
97
+
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ hash: 3
106
+ segments:
107
+ - 0
108
+ version: "0"
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ hash: 3
115
+ segments:
116
+ - 0
117
+ version: "0"
118
+ requirements: []
119
+
120
+ rubyforge_project: devise_loginza
121
+ rubygems_version: 1.5.0
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: Devise << Loginza
125
+ test_files: []
126
+